#include <stdio.h>
#include <windows.h>
LRESULT CALLBACK WinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#define BUF_LEN 256
int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state
) { HWND hWnd ; WNDCLASS wcs ; MSG msg ; int r ;
char szbuf[BUF_LEN];
//注册窗口类 wcs.style = CS_HREDRAW | CS_VREDRAW ; wcs.lpfnWndProc = WinProc ; wcs.cbClsExtra = 0 ; wcs.cbWndExtra = 0 ; wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcs.hCursor = LoadCursor(hInstance,IDC_ARROW); wcs.hIcon = LoadIcon(hInstance,IDI_APPLICATION); wcs.hInstance = hInstance ; wcs.lpszMenuName = NULL ; wcs.lpszClassName ="WndClass"; r = RegisterClass(&wcs); if ( 0 == r ) {
sprintf(szbuf,"register class error:%d",GetLastError()); MessageBox(NULL,szbuf,"error",MB_OK); exit(0); return FALSE ; }
//创建窗口 hWnd = CreateWindow("WndClass","test it",WS_OVERLAPPEDWINDOW&~WS_THICKFRAME,100,100,600,600,NULL,NULL,hInstance,NULL); if ( NULL == hWnd ) { sprintf(szbuf,"createWindow error:%d",GetLastError()); MessageBox(NULL,szbuf,"error",MB_OK); exit(0); }
//显示窗口和更新窗口 ShowWindow(hWnd,SW_SHOW); UpdateWindow(hWnd);
//消息队列 while( (r = GetMessage(&msg,NULL,0,0)) != 0 ) { if( r < 0 ) { sprintf(szbuf,"GetMessage error:%d",GetLastError()); MessageBox(NULL,szbuf,"error",MB_OK); exit(0); } else { TranslateMessage(&msg); DispatchMessage(&msg);
} }
return 0 ;
}
LRESULT CALLBACK WinProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter
) { switch( uMsg) { case WM_CLOSE: //点击窗口关闭时的消息 { DestroyWindow(hwnd); break; } case WM_DESTROY: //destroywindow发送的消息 { PostQuitMessage(0); break ; } default: return ::DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0 ;
}
|