windows编程的一个求助帖
#include<windows.h>
#include<tchar.h>
int x=0,y=0,mx,my;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//LRESULT 消息过程结果的签名
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR lpCmdLine,int nShowCmd)
{ ///
WNDCLASS wc;
TCHAR szName[] = TEXT("DoMyself");
MSG msg;
HWND hwnd;
//定义窗口类
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szName;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
//注册窗口类
if(!RegisterClass(&wc))
{
MessageBox(NULL,TEXT("注册窗口类失败"),TEXT("错误"),MB_ICONERROR|MB_OK);
return 1;
}
//创建窗口
hwnd=CreateWindow(szName,
TEXT("我自己做的第一个窗口"),
WS_OVERLAPPEDWINDOW,
200,
200,
600,
400,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
//进行消息循环
while(GetMessage(&msg,NULL,NULL,NULL))
{
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:
return 0;
case WM_SIZE:
GetClientRect(hwnd,&rect);
my=HIWORD(lparam);
mx=LOWORD(lparam);
return 0;
case WM_KEYDOWN:
switch(wparam)
{
case 'W':
if(y>=0)
{
y--;
}
break;
case 'S':
if(y<=my)
{
y+=my/10;
}
break;
case 'A':
if(x>=0)
{
x--;
}
break;
case 'D':
if(x<=mx)
{
x++;
}
break;
}
return 0;
UpdateWindow(hwnd);
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,x,y,TEXT("Flying"),wcslen(TEXT("Flying")));
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}