The purpose of the WndProc function is to process the messages received by the application. This is typically done using the Switch function.
The first message we will handle is a WM_PAINT message. The application receives this message when a portion of the application window must be updated. The first time you create a window, you must update the entire window and pass this message to indicate this action.
When dealing with WM_PAINT messages, the first thing to do is call BeginPaint, and the last thing you should do is call EndPaint. Between these two function calls, you can handle all the logic to arrange the text, buttons, and other controls in the window. For this application, we display the string "Hello, world!" in the window. To display the text, use the TextOut function, or you can use the DrawText function as follows:
Paintstruct PS; HDC hdc; TCHAR greeting[] = _t ("Hello, world!"); Switch (message) {case WM_PAINT: hdc = BeginPaint (hWnd, &ps); Here your application are laid out.//for this introduction, we just print out "Hello, world!" The top left corner. TextOut (HDC, 5, 5, greeting, _tcslen (greeting)); End application-specific Layout section. EndPaint (HWnd, &ps); break;}
Paintstruct PS; HDC hdc; TCHAR greeting[] = _t ("Hello, world!"); Switch (message) {case WM_PAINT: hdc = BeginPaint (hWnd, &ps); Here your application are laid out.//for this introduction, we just print out "Hello, world!" The top left corner. TextOut (HDC, 5, 5, greeting, _tcslen (greeting)); End application-specific Layout section. EndPaint (HWnd, &ps); break;}
Applications typically handle many other messages, such as Wm_create and Wm_destroy. A simple and complete WndProc function is as follows:
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 are laid out.//for this introduction, we just print out "Hello, world!" 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;}
Full code :
#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, 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)
, 100:initial size (width, length)
Null:the Parent of this window
Null:this application does not has 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 are laid out.//for this introduction, we just print out "Hello, world!"
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;
}