永发信息网

C语言编写的DOS程序,怎样转换为windows窗口程序?

答案:2  悬赏:10  手机版
解决时间 2021-03-05 17:19
下面这个C语言编写的DOS程序,如果想编写windows窗口程序,代码应该怎样写?
#include
main()
{
printf("char 数据类型长度:%d\n",sizeof((char)'a'));
printf("string 数据类型长度:%d\n",sizeof("a"));

}
最佳答案
#include
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ; //窗口句柄
MSG msg ; //消息结构
WNDCLASS wndclass ; //窗口类结构
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

//如果注册窗口失败,弹出错误对话框
if (!RegisterClass (&wndclass))
{
//在Windows 98中,大多数Unicode函数无法执行,MessageBoxW是个例外
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

//建立窗口
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ; //显示窗口
UpdateWindow (hwnd) ; //重画显示区域
//消息循环,用于从消息队列中取出消息,并做相应处理
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

//窗口消息处理程序
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
//GetClientRect函数检索一个窗口的客户区坐标rect
GetClientRect (hwnd, &rect) ;
char buf[80];

sprintf(buf,"char 数据类型长度:%d\nstring 数据类型长度:%d\n",sizeof((char)'a'),sizeof("a"));
DrawText (hdc, TEXT (buf), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
全部回答

 keys.cpp // 按键表索引发: // 首先建立key_table[]作为按键表,然后根据按键扫描码改变key_table[]中的状态, // 这样key_table[]中所有对应的索引按键即为需要的组合键。 // 以下程序仅对上、下、左、右四个光标键做了组合按键的处理。 // 本程序在borland c++ 3.1 中调试通过。 #include<dos.h> #include<conio.h> #include<stdio.h>

#define esc 1 #define up 72 #define _up 200 #define down 80 #define _down 208 #define left 75 #define _left 203 #define right 77 #define _right 205

#define num_keys 4

#define index_up 0 #define index_down 1 #define index_left 2 #define index_right 3 #ifdef __cplusplus #define __cppargs... #else #define __cppargs #endif

char * key_table_name[num_keys] = {"up","down","left","right" }; int key_scan_code; // 键盘扫描码 int key_table[num_keys]; // 按键表

class interrupt {// 定义中断类 int int; void far interrupt ( *old_int)(__cppargs); public: void begin_int(int int,void far interrupt(*new_int)(__cppargs)); void end_int(void); };

void interrupt::begin_int(int int,void far interrupt(*new_int)(__cppargs)) {// 开始自定义中断处理 int = int; old_int = getvect(int); setvect(int,new_int); }

void interrupt::end_int(void) {// 结束自定义中断处理 setvect(int,old_int); }

void far interrupt special_key(__cppargs) {// 自定义中断处理函数 int status; key_scan_code = inp(0x60); // 读键盘扫描码 status = inp(0x61); // 读键盘状态 outp(0x61,status and 0x80); // 置位7并写 outp(0x61,status); // 再写位7 outp(0x20,0x20); // 复位pic switch(key_scan_code){ // 记录光标键,设置key_table[]中的状态 // 1为按下键,0为松开键 case up: key_table[index_up] = 1; break; case _up: key_table[index_up] = 0; break; case down: key_table[index_down] = 1; break; case _down: key_table[index_down] = 0; break; case left: key_table[index_left] = 1; break; case _left: key_table[index_left] = 0; break; case right: key_table[index_right] = 1;break; case _right:key_table[index_right] = 0;break; } }

void init_key_table(void) {// 初始化键盘表 register i; for(i=0;i<num_keys;i++) key_table[i]=0; }

void main(void) { int i; interrupt key; init_key_table(); clrscr(); key.begin_int(9,special_key); printf("play with the arrow keys,press esc to quit.\n"); while(key_scan_code != esc){ gotoxy(5,5); printf("key scancode: %d",key_scan_code); gotoxy(5,10); printf("key name:"); for(i=0;i<num_keys;i++) if(key_table[i] != 0) printf("%s",key_table_name[i]); printf("%10c",' '); } printf("\n"); key.end_int(); return; }

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
现货原油销售怎么样?就是网路销售现货。
一定质量的Na2CO3和NaHCO3固体混合物分两分,
玉露可以晒太阳吗
甲乙丙丁物流怎么去啊,有知道地址的么
西安哪里有卖情侣戒指的
0,=0,<0分别是什么意思呀?还有,二阶导数图像
宁波VS温州是宁波好还是温州好,为什
别克车有限速按钮吗
金海湾酒店在什么地方啊,我要过去处理事情
游戏中有哪些好听的中文歌
二次供水到家(6楼),原来安装的增压泵如何处
已知曲线y=x3在点(a,a3)(a不等于0)处的切
刚安装的网络监控,有几个摄像头正常,我几个
红石岩我想知道这个在什么地方
【释怀的近义词】释怀的反义词或近义词,要好
推荐资讯
二尖瓣结构不包括A.瓣环B.瓣叶C.腱索D.乳头肌
田维举,出生于1979年农历11月16日,他的五行
苹果笔记本 键盘失灵 重启后好用一会儿 而后
2o17年湖北省高考录取分数线
当车床正在车削,需要停机时,应怎样进行正确
2006年农历5月5日上午10点30分出生的女孩命运
从IBOOK删除的图书,还能再从iTunes添加回来
盛鑫粮油批发在什么地方啊,我要过去处理事情
清朝康熙第十四个儿子的妃子
啡咖适应什么样的气候和土质种植?
该病的中医辨证分型是:A.肝气郁结,瘀血停着
feet是什么意思呀,谁能告诉我
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?