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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// http://www.winprog.org/tutorial/simple_window.html
#include <windows.h>
const wchar_t g_szClassName[] = L"myWindowClass"; // global string
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
// in WndProc, program is defined.
// made up of a switch statement
// control variable is msg
// wParam - word parameter (size - smallest unit that a CPU can fetch in one block)
// lParam - long parameter (same size as wParam)
// if you're using a 32 bit operating system, then it will be 32 bits.
{
HDC hDC; // handle to the device context (permission to draw) // the place where you are drawing
PAINTSTRUCT Ps; // a variable that you need
HPEN hRedPen; // the handle to the red pen
switch (msg)
{ // each case is an event handler
// think of every possible event and write a handler for all of them.
// WM = Windows Message
case WM_PAINT:
// when one window covers "This Dumb Window", the program will redraw whatever follows when the window is moved.
// same thing happens when the window is moved away from the screen.
// it is an illusion!
hDC = BeginPaint(hwnd, &Ps); // kind of like the left curly brace
// 3 different tools: pen, brush, font
hRedPen = CreatePen(PS_SOLID, 10/*pen size*/, RGB(0/*red amount*/, 0/*green amount*/, 0/*blue amount*/));
// red, green, and blue have values of up to 255
// to get a random color type the following code instead of a number: rand()%256
SelectObject(hDC, hRedPen);
// must use this before using redPen
Ellipse(hDC,100,/*left*/100,/*top*/300,/*right*/200/*bottom*/);
Ellipse(hDC, 50,/*left*/50,/*top*/250,/*right*/150/*bottom*/);
DeleteObject(hRedPen);
// before you leave program, you should delete the object, otherwise it will use up a lot of memory and eventually fail
EndPaint(hwnd, &Ps); // kind of like the right curly brace
break;
case WM_CHAR: // event handler that occurs when a character arrives.
if (wParam == 'X')
{
MessageBox(NULL, L"An X has arrived for the program.",/*<-- message from the window*/ L"WM_CHAR",// <-- title of window
MB_ICONQUESTION/*icon that appers (there are different ones)*/ | MB_OKCANCEL/*button names: OK and CANCEL*/);
//MB = Message Box - returns a value that tells the program what the user pressed
}
else
{
MessageBox(NULL, L"A character has arrived for the program.", L"WM_CHAR",
MB_ICONQUESTION | MB_OKCANCEL);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0); // will send 0 to the loop in step 3.
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam); // will pass the same parameter to the default windows proc
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc; // registration form for windows class (or type)
HWND hwnd; // H = handle - pointer to a pointer - handle to a window
MSG Msg; // message
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc; // lpfn = long pointer to function - very important
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
L"This Dumb Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);//having op sys send your window paint msg
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
// if message returns value greater than zero, then it will continue loop.
// when loop stops, program stops
// if msg queue is empty, it will wait for a message
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
// most important part of this loop
}
return Msg.wParam;
}
|