1 2 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <windows.h>
// FUNCTION PROTOTYPES =========================================================
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// entry point for a Windows application =======================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nWinMode)
{
// define some variables
char szWinName[] = "Win32App";
char szAppTitle[] = "Win32 API Skeletal Application";
HWND hwnd;
MSG msg;
WNDCLASS wc;
// define a window class
wc.hInstance = hInstance; // handle to this instance
wc.lpszClassName = szWinName; // window class name
wc.lpfnWndProc = WndProc; // pointer to window proc
wc.style = 0; // default style
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // predefined icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined cursor
wc.lpszMenuName = NULL; // no class menu
wc.cbClsExtra = 0; // no extra info needed
wc.cbWndExtra = 0; // no extra info needed
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // predefined color brush
// register the defined window class
if (RegisterClass(&wc) == 0)
{
// an error occurred, abort the program
MessageBox(NULL, "Couldn't Register the Window Class!", "ERROR", MB_OK | MB_ICONERROR);
return 0;
}
// now that a window class has been registered, create the main window
hwnd = CreateWindow(szWinName, // name of window class to create
szAppTitle, // window title bar caption
WS_OVERLAPPEDWINDOW, // window style - normal
CW_USEDEFAULT, // X coordinate - let Windows decide
CW_USEDEFAULT, // Y coordinate - let Windows decide
CW_USEDEFAULT, // width - let Windows decide
CW_USEDEFAULT, // height - let Windows decide
NULL, // no parent window
NULL, // no menu
hInstance, // handle to this instance
NULL); // no additional arguments
// check to see if window was successfully created
if (hwnd == NULL)
{
// an error occurred, abort
MessageBox(NULL, "Couldn't Create the Main Window!", "ERROR", MB_OK | MB_ICONERROR);
return 0;
}
// display (and update) the newly created window
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
// create the message loop
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); // translate keyboard messages
DispatchMessage(&msg); // return control to Windows
}
return msg.wParam;
}
// processes the messages that Windows sends to the application ================
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// choose which Windows messages you want to use
switch(message)
{
// the window is being destroyed, so terminate the application
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
// let Windows process any messages not specified in the switch statement
return DefWindowProc(hwnd, message, wParam, lParam);
}
|