调用WINAPI 创建一个窗口并显示一些东西(create a window and display something)

来源:互联网 发布:windows高级编程 pdf 编辑:程序博客网 时间:2024/06/05 19:19
#include <windows.h>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))//登记窗体{MessageBox ( NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;return 0 ;}hwnd = CreateWindow( szAppName,//windows classnameTEXT ("The Hello Progame"), //window captionWS_OVERLAPPEDWINDOW,//window styleCW_USEDEFAULT,//inital x positionCW_USEDEFAULT,//inital y positionCW_USEDEFAULT,//inital x sizeCW_USEDEFAULT,//inital y sizeNULL,//parent windows hadleNULL,//windows menu handlehInstance,//program instance handleNULL) ;//creation paramentersShowWindow(hwnd, iCmdShow) ;//show the windowUpdateWindow(hwnd) ;//Update window itselfwhile (GetMessage (&msg, NULL, 0, 0))//Get a Message from window's message manage{TranslateMessage (&msg) ;//Translate some message from keyboardDispatchMessage (&msg) ;//Send massage to massage process program}return msg.wParam;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){HDC hdc;//device contest handlePAINTSTRUCT ps;//print structRECT rect ;//rect structswitch(message){case WM_CREATE://if the window create,this function will accept this massage//(WM_CREATE)//PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;return 0;case WM_PAINT://window redrawhdc = BeginPaint (hwnd, &ps) ;//begin paint windowGetClientRect (hwnd, &rect);//get rect of client DrawText (hdc, TEXT ("Hello, Windows XP!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;EndPaint (hwnd, &ps);//draw the text in centre of windowreturn 0 ;case WM_DESTROY://when the window destroy ,this function will accept this massage,then process it:PostQuitMessage (0);//Quit the programreturn 0 ;}return DefWindowProc (hwnd, message, wParam, lParam);}


原创粉丝点击