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 127 128
|
//Main.cpp //C++ Object Oriented Program using native
#define UNICODE //Windows C Api to display the dimensions
#define _UNICODE //and volume of a class CBox object. Note
#include <windows.h> //that this app contains no global variables.
#include <tchar.h>
#include <stdio.h>
#include "CBox.h"
#define PBOX_BUTTON 1
HWND hWndlength;
HWND hWndheigth;
HWND hWndwidth;
LRESULT CALLBACK WndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE: //This message is received one time as a
{ //result of the CreateWindow() call in
CBox* pBox=NULL; //WinMain(). The CreateWindow() call in
pBox=new CBox(2.0,3.0,4.0); //WinMain will not return until this case
/*hWndwidth = CreateWindow(TEXT("EDIT"), TEXT("width"),
WS_VISIBLE | WS_BORDER | WS_CHILD,
25,150,80,20,
hWnd, HMENU(NULL), NULL ,NULL);
hWndlength = CreateWindow(TEXT("EDIT"), TEXT("length"),
WS_VISIBLE | WS_BORDER | WS_CHILD,
110,150,80,20,
hWnd, HMENU(NULL), NULL ,NULL);
hWndheigth = CreateWindow(TEXT("EDIT"), TEXT("height"),
WS_VISIBLE | WS_BORDER | WS_CHILD,
195,150,80,20,
hWnd, HMENU(NULL), NULL ,NULL);*/
CreateWindow(TEXT("BUTTON"), TEXT("PBOX"),
WS_VISIBLE | WS_BORDER | WS_CHILD,
290,150,80,20,
hWnd, HMENU(PBOX_BUTTON), NULL ,NULL);
if(pBox) //handler returns. A new CBox is created
SetWindowLongPtr(hWnd,0,long(pBox)); //here on the heap. A pointer to it is
else //stored in the instantiated window's class
return -1; //structure. This is how the CBox object
return 0; //is persisted across invocations of the
//Window Procedure without using globals.
}
case WM_COMMAND:{
CBox *pBox;
if (LOWORD(wParam) == PBOX_BUTTON){
pBox=(CBox*)GetWindowLong(hWnd,0);
pBox->SetHeight(6.6);
SetWindowLongPtr(hWnd,0, reinterpret_cast<LONG_PTR>(pBox));
UpdateWindow(hWnd);
};
break;
}
case WM_PAINT:
{
CBox* pBox=NULL; //The Window Procedure is called with the msg parameter
HFONT hFont,hTmp; //of WM_PAINT any time any part of the Window's client
PAINTSTRUCT ps; //area becomes invalid. This will be true right after
HDC hDC; //processing of the WM_CREATE handler when the window
TCHAR szBuffer[128]; //is not yet visible. Since this window's purpose in
hDC=BeginPaint(hWnd,&ps); //life is to display the specs on a CBox object, it has
SetBkMode(hDC,TRANSPARENT); //a pointer to one of these stored as part of its instan-
hFont=CreateFont //tiated Window Structure (i.e., an 'object' with members).
(
28,0,0,0,FW_HEAVY,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS, //The GetWindowLong()
CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_PITCH,_T("Courier New") //call just below-left
); //retrieves the pointer
hTmp=(HFONT)SelectObject(hDC,hFont); //to the CBox object
pBox=(CBox*)GetWindowLong(hWnd,0); //from the object struct
swprintf(szBuffer,_T("Box.GetLength() = %6.2f"),pBox->GetLength()); //and does a lot of
TextOut(hDC,25,30,szBuffer,_tcslen(szBuffer)); //fancy GDI (Graphics
_stprintf(szBuffer,_T("Box.GetWidth() = %6.2f"),pBox->GetWidth()); //Device Interface) stuff
TextOut(hDC,25,60,szBuffer,_tcslen(szBuffer)); //to format the CBox's
_stprintf(szBuffer,_T("Box.GetHeight() = %6.2f"),pBox->GetHeight()); //specifications, i.e.
TextOut(hDC,25,90,szBuffer,_tcslen(szBuffer)); //dimensions and volume
_stprintf(szBuffer,_T("Box.Volume() = %6.2f"),pBox->Volume()); //to the screen, i.e.
TextOut(hDC,25,120,szBuffer,_tcslen(szBuffer)); //window. All in all, its
SelectObject(hDC,hTmp); //one mean logical machine! Note down in WinMain() when I filled
DeleteObject(hFont); //out the Window Class struct I set the wc.cbWndExtra bytes to 4.
EndPaint(hWnd,&ps); //It is in these four exta bytes that I'm storing the pointer to
return 0; //the CBox object, whose 'duration' is the lifetime of the program.
//It remains alive until the user clicks the [x] button to close
} //the app, at which point delete is called on the CBox object.
case WM_DESTROY: //Try to think about this whole application as a C++ object. It
{ //is created an allocates memory; it hides all its internal details
CBox* pBox=NULL; //through hidden private members; and it cleans up after itself at
pBox=(CBox*)GetWindowLong(hWnd,0); //termination.
if(pBox)
delete pBox;
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("CBox3");
MSG messages;
WNDCLASS wc;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=WndProc;
wc.style=0, wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=hIns, wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra=4;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClass(&wc);
hWnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW^WS_MAXIMIZEBOX,100,100,400,300,0,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|