C语言编写的DOS程序,怎样转换为windows窗口程序?
解决时间 2021-03-05 17:19
- 提问者网友:那叫心脏的地方装的都是你
- 2021-03-05 01:51
下面这个C语言编写的DOS程序,如果想编写windows窗口程序,代码应该怎样写?
#include
main()
{
printf("char 数据类型长度:%d\n",sizeof((char)'a'));
printf("string 数据类型长度:%d\n",sizeof("a"));
}
最佳答案
- 五星知识达人网友:底特律间谍
- 2021-03-05 02:35
#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);
}
全部回答
- 1楼网友:酒醒三更
- 2021-03-05 04:05
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;
}
我要举报
大家都在看
推荐资讯