int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hInstancePrev,
LPSTR 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; // set instance handle
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = 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,
TEXT("Hello Windows"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
200,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
CenterWindow(hwnd);
while (GetMessage( &msg, NULL, 0, 0) > 0) {
TranslateMessage( &msg);
DispatchMessage( &msg);
}
return msg.wParam;
}
|
hwnd = CreateWindow(
szAppName,
TEXT("Window Caption"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, // initial X position
CW_USEDEFAULT, // initial Y position
400, // Width
200, // Height
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
CenterWindow(hwnd);
|
WS_BORDER WS_CAPTION WS_CHILDWINDOW WS_CLIPCHILDREN WS_CLIPSIBLINGS WS_DISABLED WS_DLGFRAME WS_GROUP WS_HSCROLL WS_ICONIC WS_MAXIMIZE WS_MAXIMIZEBOX WS_MINIMIZE WS_MINIMIZEBOX WS_OVERLAPPED WS_OVERLAPPEDWINDOW WS_POPUP WS_POPUPWINDOW WS_SIZEBOX WS_SYSMENU WS_TABSTOP WS_THICKFRAME WS_TILED WS_TILEDWINDOW WS_VISIBLE WS_VSCROLL |
| Message | Description |
|---|---|
| WM_CREATE | Sent when a window is being created. Good for initializing controls/resources. |
| WM_DESTROY | Sent when the window is being destroyed. Use PostQuitMessage(0) here. |
| WM_CLOSE | Sent when the user tries to close the window (e.g. clicks the X). |
| WM_QUIT | Posted by PostQuitMessage() to terminate the message loop. |
| WM_COMMAND | Sent when a control or menu sends a notification. LOWORD(wParam) gives the ID. |
| WM_PAINT | Sent when the window needs to be redrawn. Handle all drawing here. |
| WM_SIZE | Sent when the window is resized. lParam gives new width/height. |
| WM_MOVE | Sent when the window is moved. lParam gives new x/y position. |
| WM_TIMER | Sent after SetTimer() interval elapses. Used for periodic updates. |
| WM_KEYDOWN | Sent when a key is pressed. wParam contains the virtual key code. |
| WM_KEYUP | Sent when a key is released. |
| WM_CHAR | Sent when a character is typed. Takes into account keyboard state. |
| WM_MOUSEMOVE | Sent when the mouse moves within the client area. |
| WM_LBUTTONDOWN | Sent when the left mouse button is pressed. |
| WM_LBUTTONUP | Sent when the left mouse button is released. |
| WM_RBUTTONDOWN | Sent when the right mouse button is pressed. |
| WM_MOUSEWHEEL | Sent when the mouse wheel is scrolled. |
| WM_SETFOCUS | Sent when the window gains keyboard focus. |
| WM_KILLFOCUS | Sent when the window loses keyboard focus. |
| WM_ENABLE | Sent when the window is enabled or disabled. |
| WM_ERASEBKGND | Sent before WM_PAINT to erase background. Can be used to reduce flicker. |
| WM_SHOWWINDOW | Sent when the window is shown or hidden. |
| WM_ACTIVATE | Sent when the window is activated or deactivated. |
| WM_HSCROLL | Sent when a horizontal scrollbar is used. |
| WM_VSCROLL | Sent when a vertical scrollbar is used. |
| WM_NOTIFY | Sent by controls to notify the parent of events (e.g. list views). |
| WM_INITDIALOG | Sent before a dialog box is displayed. Used for initialization. |
| WM_SETCURSOR | Sent to set the cursor when it moves over the window. |
| WM_CONTEXTMENU | Sent when a user right-clicks or uses the context menu key. |
| WM_GETMINMAXINFO | Allows customizing min/max window size during resizing. |
| WM_SYSKEYDOWN | Sent for system key presses (e.g., Alt key combinations). |
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_CREATE:
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY :
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
|