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
|
//Main.cpp //C++ Object Oriented Program using native
#include <windows.h> //Windows C Api to display the dimensions
#include <stdio.h> //and volume of a class CBox object.
#include "Main.h"
#include "CBox.h" //Note that this program contains no global
EVENTHANDLER EventHandler[3]; //variables. EventHandler is an array of
//function pointers.
long fnWndProc_OnCreate(lpWndEventArgs Wea) //When the window object is created, create
{ //a CBox object and store/set a pointer to
CBox* pBox=NULL; //in the Window Class struct. If the
//memory allocation (new) fails, return -1
pBox=new CBox(2.0,3.0,4.0); //and program initialization will halt and
if(pBox) //WinMain() will exit. A WM_CREATE message
SetWindowLong(Wea->hWnd,0,(long)pBox); //is received one time on Window creation.
else //The CreateWindow() call in WinMain() won't
return -1; //return until this procedure returns.
return 0;
}
long fnWndProc_OnPaint(lpWndEventArgs Wea) //Windows will send a WM_PAINT message to
{ //a window when any part of the window
CBox* pBox=NULL; //becomes 'invalid' and needs to be
HFONT hFont,hTmp; //repainted. This will occur at program
PAINTSTRUCT ps; //start up and any time the window is
HDC hDC; //uncovered by another window. A pointer
//to the window's CBox object is stored
char szBuffer[128]; //in the window's Class Extra Bytes
hDC=BeginPaint(Wea->hWnd,&ps); //( .cbWndExtra ), and is here accessed
SetBkMode(hDC,TRANSPARENT); //using the Api function GetWindowLong().
hFont=CreateFont //To draw to a window in a WM_PAINT...
(
28,0,0,0,FW_HEAVY,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS, //handler one may use
CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_PITCH,"Courier New" //TextOut(), but that
); //function requires a
hTmp=(HFONT)SelectObject(hDC,hFont); //handle to a device
pBox=(CBox*)GetWindowLong(Wea->hWnd,0); //context obtainable
sprintf(szBuffer,"Box.GetLength() = %6.2f",pBox->GetLength()); //with BeginPaint().
TextOut(hDC,25,30,szBuffer,strlen(szBuffer)); //The C Runtime func
sprintf(szBuffer,"Box.GetWidth() = %6.2f",pBox->GetWidth()); //sprintf can be used
TextOut(hDC,25,60,szBuffer,strlen(szBuffer)); //to output a text string
sprintf(szBuffer,"Box.GetHeight() = %6.2f",pBox->GetHeight()); //to an output buffer,
TextOut(hDC,25,90,szBuffer,strlen(szBuffer)); //and then TextOut()
sprintf(szBuffer,"Box.Volume() = %6.2f",pBox->Volume()); //will draw the text.
TextOut(hDC,25,120,szBuffer,strlen(szBuffer)); //A FONT is a GDI object
SelectObject(hDC,hTmp); //and needs to be
DeleteObject(hFont); //released to prevent
EndPaint(Wea->hWnd,&ps); //memory leaks. Finally
//EndPaint() needs to be
return 0; //called to terminate use
} //of the device context.
long fnWndProc_OnDestroy(lpWndEventArgs Wea) //When you click the 'x' button in the
{ //window's title bar Windows will send
CBox* pBox=NULL; //WM_CLOSE and WM_DESTROY messages (among
//others) to the window's Window Procedure.
pBox=(CBox*)GetWindowLong(Wea->hWnd,0); //This program needs to delete the CBox
if(pBox) //object a pointer to which is stored in
delete pBox; //the program's .cbWndExtra bytes. Then
PostQuitMessage(0); //PostQuitMessage() needs to be called so
//the message pump in WinMain() will exit
return 0; //and the program will end.
}
void AttachEventHandlers(void) //This procedure maps windows messages to the
{ //procedure which handles them.
EventHandler[0].Code=WM_CREATE, EventHandler[0].fnPtr=fnWndProc_OnCreate;
EventHandler[1].Code=WM_PAINT, EventHandler[1].fnPtr=fnWndProc_OnPaint;
EventHandler[2].Code=WM_DESTROY, EventHandler[2].fnPtr=fnWndProc_OnDestroy;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
WndEventArgs Wea; //This procedure loops through the EVENTHANDER array
//of structs to try to make a match with the msg parameter
for(unsigned int i=0; i<3; i++) //of the WndProc. If a match is made the event handling
{ //procedure is called through a function pointer -
if(EventHandler[i].Code==msg) //(EventHandler[i].fnPtr). If no match is found the
{ //msg is passed onto DefWindowProc().
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
char szClassName[]="CBox4";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
AttachEventHandlers();
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_DBLCLKS;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra=4;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,100,400,300,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|