Steps To Win32 Application
Steps To Win32 Application
9. #include <string.h> 10.#include <tchar.h> 11.In addition to the WinMain function, every Win32-based application must also have a window-procedure function. This function is typically named WndProc. WndProc has the following syntax. 12.LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); This function handles the many messages that an application receives from the operating system. For example, in an application that has a dialog box that has an OK button, when the user clicks the button, the operating system sends the application a message that the button was clicked. WndProc is responsible for responding to that event. In the example, the appropriate response might be to close the dialog box. For more information, see Window Procedures. To add functionality to the WinMain function In the WinMain function, create a window class structure of type WNDCLASSEX. This structure contains information about the window, for example, the application icon, the background color of the window, the name to display in the title bar, the name of the window procedure function, and so on. The following example shows a typical WNDCLASSEX structure. 2. WNDCLASSEX wcex; 3. 4. wcex.cbSize = sizeof(WNDCLASSEX); 5. wcex.style = CS_HREDRAW | CS_VREDRAW; 6. wcex.lpfnWndProc = WndProc; 7. wcex.cbClsExtra = 0; 8. wcex.cbWndExtra = 0; 9. wcex.hInstance = hInstance; 10. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 11. wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 12. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 13. wcex.lpszMenuName = NULL; 14. wcex.lpszClassName = szWindowClass;
1.
15.
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); For information about the fields of this structure, see WNDCLASSEX.
16. Now
that you have created a window class, you must register it. Use the RegisterClassEx function and pass the window class structure as an argument. 17. if (!RegisterClassEx(&wcex)) 18. { 19. MessageBox(NULL, 20. _T("Call to RegisterClassEx failed!"), 21. _T("Win32 Guided Tour"), 22. NULL); 23. 24. return 1; 25. }
26. Now
27.static TCHAR szWindowClass[] = _T("win32app"); 28.static TCHAR szTitle[] = _T("Win32 Guided Tour Application"); 29. 30.// The parameters to CreateWindow explained: 31.// szWindowClass: the name of the application 32.// szTitle: the text that appears in the title bar 33.// WS_OVERLAPPEDWINDOW: the type of window to create 34.// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) 35.// 500, 100: initial size (width, length) 36.// NULL: the parent of this window 37.// NULL: this application does not have a menu bar 38.// hInstance: the first parameter from WinMain 39.// NULL: not used in this application 40.HWND hWnd = CreateWindow( 41. szWindowClass, 42. szTitle, 43. WS_OVERLAPPEDWINDOW, 44. CW_USEDEFAULT, CW_USEDEFAULT, 45. 500, 100, 46. NULL, 47. NULL,
48. hInstance, 49. NULL 50.); 51.if (!hWnd) 52.{ 53. MessageBox(NULL, 54. _T("Call to CreateWindow failed!"), 55. _T("Win32 Guided Tour"), 56. NULL); 57. 58. return 1; 59.} This function returns an HWND, which is a handle to a window. For more information, see Windows Data Types. 60.Now, use the following code to display the window. 61.// The parameters to ShowWindow explained: 62.// hWnd: the value returned from CreateWindow 63.// nCmdShow: the fourth parameter from WinMain 64.ShowWindow(hWnd, 65. nCmdShow); 66.UpdateWindow(hWnd); At this point, the displayed window will not have much content because you have not yet implemented theWndProc function. 67.Now add a message loop to listen for the messages that the operating system sends. When the application receives a message, this loop dispatches it to the WndProc function to be handled. The message loop resembles the following code. 68. MSG msg; 69. while (GetMessage(&msg, NULL, 0, 0)) 70. { 71. TranslateMessage(&msg); 72. DispatchMessage(&msg); 73. } 74. 75. return (int) msg.wParam;
For more information about the structures and functions in the message loop, see MSG, GetMessage,TranslateMessage, and DispatchMessage. At this point, the WinMain function should resemble the following code. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); if (!RegisterClassEx(&wcex)) { MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Win32 Guided Tour"), NULL); return 1; } hInst = hInstance; // Store instance handle in our global variable // The parameters to CreateWindow explained:
// szWindowClass: the name of the application // szTitle: the text that appears in the title bar // WS_OVERLAPPEDWINDOW: the type of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) // 500, 100: initial size (width, length) // NULL: the parent of this window // NULL: this application dows not have a menu bar // hInstance: the first parameter from WinMain // NULL: not used in this application HWND hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hInstance, NULL ); if (!hWnd) { MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL); return 1; } // The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // Main message loop: MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } To add functionality to the WndProc function 1. To enable the WndProc function to handle the messages that the application receives, implement a switch statement. The first message to handle is the WM_PAINT message. The application receives this message when part of its displayed window must be updated. (When the window is first displayed, all of it must be updated.) To handle a WM_PAINT message, first call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint. For this application, the logic between the beginning call and the ending call is to display the string "Hello, World!" in the window. In the following code, notice that the TextOut function is used to display the string. PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hello, World!"); switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Here your application is laid out. // For this introduction, we just print out "Hello, World!" // in the top left corner. TextOut(hdc, 5, 5, greeting, _tcslen(greeting)); // End application-specific layout section.
3.
4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.}
An application typically handles many other messages, for example, WM_CREATE and WM_DESTROY. The following code shows a basic but complete WndProc function. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hello, World!"); switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Here your application is laid out. // For this introduction, we just print out "Hello, World!" // in the top left corner. TextOut(hdc, 5, 5, greeting, _tcslen(greeting)); // End application specific layout section. EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); break; } return 0;
Example To build this example 1. Create a Win32-based project as shown in "To create a Win32-based project" earlier in this walkthrough. 2. Copy the code that follows these steps and then paste it in the GT_HelloWorldWin32.cpp source file. 3. On the Build menu, click Build Solution. 4. To run the application, press F5. A window that contains the text "Hello World!" should appear in the upper-left corner of the display. Code // GT_HelloWorldWin32.cpp // compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c #include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> // Global variables // The main window class name. static TCHAR szWindowClass[] = _T("win32app"); // The string that appears in the application's title bar. static TCHAR szTitle[] = _T("Win32 Guided Tour Application"); HINSTANCE hInst; // Forward declarations of functions included in this code module: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); if (!RegisterClassEx(&wcex)) { MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Win32 Guided Tour"), NULL); return 1; } hInst = hInstance; // Store instance handle in our global variable // The parameters to CreateWindow explained: // szWindowClass: the name of the application // szTitle: the text that appears in the title bar // WS_OVERLAPPEDWINDOW: the type of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) // 500, 100: initial size (width, length) // NULL: the parent of this window // NULL: this application does not have a menu bar // hInstance: the first parameter from WinMain // NULL: not used in this application
HWND hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hInstance, NULL ); if (!hWnd) { MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL); return 1; } // The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // Main message loop: MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; }
// // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hello, World!"); switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Here your application is laid out. // For this introduction, we just print out "Hello, World!" // in the top left corner. TextOut(hdc, 5, 5, greeting, _tcslen(greeting)); // End application-specific layout section. EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); break; } return 0;