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
|
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE: //This message is only received one time at program start up. Think of it
{ //as a constructor call.
MessageBox(hwnd,_T("Window Procedure Received WM_CREATE Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_LBUTTONDOWN: //This message comes through when you click the form with your left mouse button.
{
MessageBox(hwnd,_T("Window Procedure Received WM_LBUTTONDOWN Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_PAINT: //This message comes through whenever any part of the window becoms 'invalid'.
{ //At program start up the whole window is invalid so must be drawn.
TCHAR szBuffer[]=_T("Click Anywhere On Form (And Oh Yeah...Hello, World!)");
//LPCTSTR szBuffer=_T("Click Anywhere On Form (And Oh Yeah...Hello, World!)"); // << can use this too
PAINTSTRUCT ps; // Necessary for BeginPaint(0 / EndPaint() calls
HDC hDC; // Handle (virtual memory pointer) to drawing characteristics
hDC=BeginPaint(hwnd,&ps); // Required protocol for WM_PAINT handler
int iBkMode=SetBkMode(hDC,TRANSPARENT); // Save Background Mode characteristic of drawing context
TextOut(hDC,40,20,szBuffer,(int)_tcslen(szBuffer)); // Draw Text
SetBkMode(hDC,iBkMode); // Return Drawing Context To Original State
EndPaint(hwnd,&ps); // End Of WM_PAINT protocol
return 0;
}
case WM_DESTROY: //This message comes through when you click the [x] close button in the upper right
{ //corner of your window.
MessageBox(hwnd,_T("Window Procedure Received WM_DESTROY Message!"),_T("Message Report!"),MB_OK);
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("Form1");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName = szClassName; //Important Field! Character string identifying window class
wc.lpfnWndProc = fnWndProc; //Important Field! Function Pointer. Address of Window Procedure
wc.cbSize = sizeof (WNDCLASSEX); //Those top two fields I just listed are very important. The
wc.style = 0; //others are of course necessary too, but fully understanding all
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); //the implications of the .szClassName and .lpfnWndProc fields will
wc.hInstance = hInstance; //go a long way to helping you understand Win32 coding. The
wc.hIconSm = 0; //.hBrushBackground field will be the color of the Window's
wc.hCursor = LoadCursor(NULL,IDC_ARROW); //background. The .cbWndExtra field is very useful as it allows
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; //you to associate object (Window) data to the instantiated Window's
wc.cbWndExtra = 0; //internal structure, i.e., accomodate member data.
wc.cbClsExtra = 0;
wc.lpszMenuName = NULL;
RegisterClassEx(&wc),
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,350,250,450,300,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0)) //All important message pump. This logic continually retrieves
{ //messages from the program's message queene and dispatches them
TranslateMessage(&messages); //to the Window Procedure for processing.
DispatchMessage(&messages);
}
return (int)messages.wParam;
}
|