500, 100, NULL, NULL,
hInstance, NULL );
if (!hWnd) {
MessageBox(NULL,
_T(\ _T(\ 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; }
向 WndProc 添加功能
1. WndProc 函数的用途是处理应用程序接收的消息。通常使用 Switch 函数实现此操作。
我们将处理的第一个消息是 WM_PAINT 消息。当必须更新应用程序窗口的一部分时,应用程序会收到此消息。首次创建窗口时,必须更新整个窗口,并传递此消息以指示此操作。 当处理 WM_PAINT 消息时,首先应做的是调用 BeginPaint,最后应做的是调用 EndPaint。在这两个函数调用之间,您可以处理所有的逻辑,以在窗口中排列文本、按钮和其他控件。
对于此应用程序,我们在窗口中显示字符串“Hello, World!”。若要显示文本,请使用 TextOut 函数,如下所示:
复制
PAINTSTRUCT ps; HDC hdc;
TCHAR greeting[] = _T(\
switch (message) {
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// Here your application is laid out.// For this introduction, we just print out \ // in the top left corner.TextOut(hdc, 5, 5,
greeting, _tcslen(greeting));
// End application-specific layout section.EndPaint(hWnd, &ps); break; }
2. 应用程序通常会处理许多其他消息,如 WM_CREATE 和 WM_DESTROY。一个简单而完整
的 WndProc 函数如下:
复制
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps; HDC hdc;
TCHAR greeting[] = _T(\
switch (message) {
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// Here your application is laid out.// For this introduction, we just print out \ // 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; }
示例
说明
完成所有步骤之后,代码应该与下面的内容类似:若要生成应用程序,请从“生成”菜单选择“生成解决方案”。如果应用程序编译时没有任何错误,您可以通过按 F5 来运行该应用程序。在屏幕的左上角附近将显示带有文本“Hello, World!”的简单窗口。
代码
复制
// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#include
// Global variables
// The main window class name.static TCHAR szWindowClass[] = _T(\
// The string that appears in the application's title bar.static TCHAR szTitle[] = _T(\
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(\ _T(\ 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(\ _T(\ 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;