#include<Windows.h>
#include<stdio.h>
const char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message){
case WM_COMMAND:// 菜单选择:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
// case IDM_ABOUT:
// ... ...
// break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//在此添加任意绘图代码...,使用hDC=GetHDC(hWnd)
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0L;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.style=CS_HREDRAW | CS_VREDRAW;
wnd.lpszClassName=className;
wnd.lpszMenuName =NULL;
wnd.hInstance =hInst;
wnd.lpfnWndProc =myproc;
wnd.hIcon =LoadIcon(NULL, IDI_APPLICATION);
wnd.hCursor =LoadCursor(NULL,IDC_ARROW);
wnd.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
ATOM reg;
if((reg=RegisterClassEx(&wnd))==0){
MessageBox(NULL,"RegisterClassEx失败\n","有错!",MB_OK);
return 1;
}
char dip[200];
HWND hWnd=CreateWindowEx(0,className,"窗口名称",
WS_OVERLAPPEDWINDOW|WS_HSCROLL |WS_VSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL,NULL,hInst,NULL);
if(!IsWindow(hWnd)){
sprintf(dip,"CreateWindowEx失败:%d\n",GetLastError());
MessageBox(NULL,dip,"有错!",MB_OK);
return 1;
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0 && GetMessage(&msg, (HWND) NULL, 0, 0) != -1) {
TranslateMessage(&msg);//例如WM_KEYDOWN和WM_KEYUP组合产生一个WM_CHAR,可以没有这句
DispatchMessage(&msg); //调用myproc以DefWindowProc结束,清理队列。
}
return (int)msg.wParam;
}
|