Chapter III: Hellowin Code
In the original book, the parameters of the CreateWindow function have not been clear. In order to make it easier for beginners, I have rewritten the program and add a comment, I hope to help with friends who learn Win32.
/ ************************************************** ********** Written by Hack-chul kang 2003.2.5 display "Hello Windows" Program ********************************* ******************************************************************************************* LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); // main function Windows applications int WINAPI WinMain (hINSTANCE hInstance, hINSTANCE, PSTR lpszCmdLine, int iCmdShow) {// definition screen TCHAR tcClassName [] = TEXT ( " My window "); // window class name string
WNDCLASS WC; // Window Class Properties Description Structure Wc.lpszclassName = TcClassName; // Window Class Name wc.lpsz GeneName = NULL; // Window Menu Resource Name Wc.lpfnWndProc = WNDPROC; // Window Object Process WC. hInstance = hInstance; // current process object handle wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); // window background brush objects wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); // icon object wc.hCursor = LoadCursor (NULL , IDC_ARROW; // cursor object wc.cbclsextra = 0; // Co-class window object public data area wc.cbWndextra = 0; // Current window object Private Data Area size wc.style = cs_hredraw | cs_vredraw; // window classes style
If (! RegisterClass (& WC)) // Register window {// window class registration Uncommon error message MessageBox (Null, Text ("RegisterClasserror!"), Text ("Error", MB_ICONERROR);
Return 0;}
// Create a window object TCHAR TCHAR TCHAR TCHAR TCHAR TCHAR TCWINDOWCAPTIONNAME [] = text ("win32 API"); // Window Object Title Name CreateStruct CS; // Window Object Property Description Structure CS.Lpszclass = TcClassName; // Window Class name cs.lpszname = TcWindowCAPTIONNAME ; // Window Object Title Name CS.Style = WS_OVERLAPPEDWINDOW; // Window Object Style Cs.x = 100; // Window Objects X Coordinate CS.Y = 100; // Window Objects Y coordinate on the screen CS.cx = 400; // Window object width cs.cy = 300; // Window object's height cs.hwndparent = null; // Window object's parent window handle cs.hmenu = NULL; // window object menu Sector or sub-window number cs.hinstance = hinstance; // Current process instance handle cs.lpcreateParams = null; // Create window object hwnd hWnd = CreateWindow (cs.lpszclass, cs.lpszname, cs.style, cs.x, Cs.y, cs.cx, cs.cy, cs.hwndparent, cs.hmenu, cs.hinstance, cs.lpcreateparams); if (hwnd == null) {// window object Create an unsuccessful error message MessageBox (NULL , Text ("CreateWindowError!"), Text ("Error!"), MB_ICONEROR);
Return 0;} // Display window object showWindow (hwnd, icmdshow); UpdateWindow (hwnd); // Immediately refresh window object // message retrieval MSG MSG; While (GetMessage (& MSG, NULL, 0, 0)) {// Retrieve the message; when you retrieve the WM_QUIT message, exit TranslateMessage (& MSG) from the message loop; // Transfer DISPATCHMESSAGE (& MSG); // Send a message, resulting in the corresponding window process processing message} // Main window Returns Return Msg.wParam;} // Window Object Process Process Lresult Callback WndProc (HWND HWND, UINT IMSG, WPARAM WPARAM, LPARAM LPARAM) {// Message Classification Switch (IMSG) {// Customer Area Drawing Message Case WM_Paint: {PAINTSTRUCT PS; // Draw Properties Description HDC HDC = BeginpAint (HWND, & PS); // Get Display Device Objects and Draw Description Properties Rect Rect;
GetClientRect (hwnd, & review); // Get the current window object client area rectangular setBkmode (HDC, Transparent); // Set background mode setTextColor (HDC, RGB (255, 0))); // Set text color // Draw Text DrawText (HDC, Text ("Hello, Win32!"), -1, & Rect, DT_Center | DT_VCenter | DT_SIINGLINE); EndPaint (HWND, & PS); // Customer District Drawing end, return display device object Return 0;} Case WM_DESTROY: {// Send a WM_QUIT message, notify the thread message retrieval loop, the main program can exit postquitMessage (0);
RETURN 0;}} // The process of the last process of the program is handed over to the process processing function of the default window object for the Window system.
Return DefWindowProc (HWND, IMSG, WPARAM, LPARAM);