| QUOTE |
| #include <stdio.h> #include <stdlib.h> #include "CppProtoWnd.h" #define IDC_EDIT_MULTI 103 CppProtoWnd::CppProtoWnd() { hInst=NULL; nExStyle=0; nID=0; } //------------------------------------------------------------------------------------------- CppProtoWnd::~CppProtoWnd() { } //------------------------------------------------------------------------------------------- //NON MSG FUNCTIONS //------------------------------------------------------------------------------------------- HWND CppProtoWnd::Create(HWND hParent) { HWND hwnd; //the wnd handle hwnd=CreateWindowEx(nExStyle, //more or 'extended' styles chClassName, //the 'class' of window to create chCaption, //the window title nStyle, //window style: how it looks Left, //window position: left Top, //window position: top Width, //window width Height, //window height hParent, //parent window handle (HMENU)nID, //handle to wnd menu or cntrl ID hInst, //application instance (VOID*)this); //store pointer to class return hwnd; } //------------------------------------------------------------------------------------------- TCHAR* CppProtoWnd::GetWndCaption() { return chCaption; } //------------------------------------------------------------------------------------------- TCHAR* CppProtoWnd::GetWndClassName() { return chClassName; } //------------------------------------------------------------------------------------------- void CppProtoWnd::SetInstance(HINSTANCE hInstance) { hInst=hInstance; } //------------------------------------------------------------------------------------------- void CppProtoWnd::SetWndCaption(TCHAR* chCap) { //check to make sure chCap is valid if ((chCap)&&(lstrlen(TEXT(chCap))<256)) { //it is so store it lstrcpy(chCaption,chCap); } } //------------------------------------------------------------------------------------------- void CppProtoWnd::SetWndClassName(TCHAR* chClass) { //check to make sure chClass is valid if ((chClass)&&(lstrlen(TEXT(chClass))<100)) { //it is so store it lstrcpy(chClassName,chClass); } } //------------------------------------------------------------------------------------------- //DEFINITIONS OF CppWnd //------------------------------------------------------------------------------------------- CppWnd::CppWnd() { //set a default value for the wnd caption SetWndCaption(TEXT("The Wizard - By Rui Li")); //set a default value for the wnd class name SetWndClassName(TEXT("WND_CLASS_NAME")); //nullify wnd handle to make later checking easier hAppWnd=NULL; //set wnd default dimensions Left=CW_USEDEFAULT; Top=CW_USEDEFAULT; Width=217; Height=150; //set default window style - don't make it resizable nStyle=WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME; //set default class style used in wnd registration nClassStyle=CS_HREDRAW|CS_VREDRAW; // nClrSelected=0; } //------------------------------------------------------------------------------------------- CppWnd::~CppWnd() { } //------------------------------------------------------------------------------------------- HWND CppWnd::Create(HWND hParent) { WNDCLASSEX wcx; //this structure is used for storing information about the wnd 'class' wcx.cbSize = sizeof(WNDCLASSEX); //byte size of WNDCLASSEX struct wcx.style = nClassStyle; //ensure wnd is always redrawn wcx.lpfnWndProc = (WNDPROC)MainWndProc; //pointer to the Window Procedure wcx.cbClsExtra = 0; //Extra bytes for 'class' wnds wcx.cbWndExtra = 0; //Extra bytes for this wnd wcx.hInstance = hInst; //Application instance wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION); //Application icon wcx.hCursor = LoadCursor(NULL,IDC_ARROW); //Cursor for wnd wcx.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); //Background wnd colour wcx.lpszMenuName = NULL; //Name of wnd menu wcx.lpszClassName = GetWndClassName(); //Name of this wnd 'class' wcx.hIconSm = NULL; //Icon in top-left corner of wnd //Register the wnd class with the Windows system if (!RegisterClassEx(&wcx)) { //Registration has failed so inform the user MessageBox( NULL, TEXT("Failed to register wnd class"), TEXT("ERROR"), MB_OK|MB_ICONERROR); return NULL; } //create wnd of the 'class' just registered by calling base class function HWND hwnd; //the wnd handle hwnd=CppProtoWnd::Create(); if (!hwnd) { //wnd creation has failed so inform the user MessageBox( NULL, TEXT("Failed to create wnd"), TEXT("ERROR"), MB_OK|MB_ICONERROR); return NULL; } //store the wnd handle if (!hAppWnd) { hAppWnd=hwnd; } //Display the wnd ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); return hwnd; } //------------------------------------------------------------------------------------------- LRESULT CALLBACK CppWnd::CppWndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam) { //all the msgs are routed through here. The msgs are 'cracked' ie the wParam and lParam are //converted into meaningful parameters which are then forwarded to specific handler functions. switch (Message) { case WM_COMMAND: return OnCommand((HWND)lParam,LOWORD(wParam),HIWORD(wParam)); case WM_CREATE: hAppWnd=hwnd; //store wnd handle here return OnCreate((CREATESTRUCT*)lParam); case WM_DESTROY: return OnDestroy(); default: return DefWindowProc(hwnd,Message,wParam,lParam); //let system deal with msg } } //------------------------------------------------------------------------------------------- //MESSAGE HANDLER FUNCTIONS //------------------------------------------------------------------------------------------- BOOL CppWnd::OnCommand(HWND hwndCtl,int nID,UINT uNotifyCode) { //check if msg has come from a control and the button has been clicked if ((hwndCtl)&&(uNotifyCode==BN_CLICKED)) { TCHAR chMsg[20]; BOOL bExitFired=FALSE; BOOL bShowMsgBox=FALSE; //check the control id switch (nID) { case IDOK: //OK button srand(GetTickCount()); int i; i=rand()%11; if (i==1) MessageBox(NULL, TEXT("Yes, you must do that!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==2) MessageBox(NULL, TEXT("Of course. What are you thinking?"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==3) MessageBox(NULL, TEXT("What are you thinking? Are you a fool? Of course you shouldn't do that!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==4) MessageBox(NULL, TEXT("Listen to me. I have lived many years on this planet. Never should you do such a thing!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==5) MessageBox(NULL, TEXT("Why, of course! You don't even need to ask me that. Go ahead!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==6) MessageBox(NULL, TEXT("That's an excellent idea!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==7) MessageBox(NULL, TEXT("No, you will suffer terrible consequences if you do that."), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==8) MessageBox(NULL, TEXT("Something tells me that that's not a good idea."), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==9) MessageBox(NULL, TEXT("Do it! If you don't do it for yourself, do it for the rest of us!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); if (i==10) MessageBox(NULL, TEXT("That's a marvelous idea! Of course you should do that!"), TEXT("The Wizard says ..."), MB_ICONEXCLAMATION|MB_OK); break; default: return 0; } } return 0; } //------------------------------------------------------------------------------------------- int CppWnd::OnCreate(CREATESTRUCT *cs) { //create an 'ok' default pushbutton btnOK.SetWndClassName(TEXT("BUTTON")); btnOK.SetWndCaption(TEXT("Ask Wizard")); btnOK.nStyle|=BS_PUSHBUTTON; btnOK.Top=80; btnOK.Left=22; btnOK.Width=163; btnOK.Height=25; btnOK.nID=IDOK; //IDOK=1 and is defined by windows btnOK.Create(hAppWnd); //Create multiline edit control editMulti.SetWndClassName(TEXT("EDIT")); editMulti.SetWndCaption(TEXT("Are you unsure about doing something? If so, ask the Wizard! Type in a question here about whether you should do something, then click the button below! Don't ask the same question more than once, because the Wizard will get confused.")); editMulti.nExStyle=WS_EX_CLIENTEDGE; editMulti.nStyle|=ES_MULTILINE|WS_VSCROLL; editMulti.Top=5; editMulti.Left=5; editMulti.Width=200; editMulti.Height=60; editMulti.nID=IDC_EDIT_MULTI; editMulti.Create(hAppWnd); return 0; //return -1 to fail creation } //------------------------------------------------------------------------------------------- BOOL CppWnd::OnDestroy() { PostQuitMessage(0); //signal end of application return 0; } //------------------------------------------------------------------------------------------- //DEFINITIONS OF CppCntrl //------------------------------------------------------------------------------------------- CppCntrl::CppCntrl() { //set default wnd style nStyle=WS_CHILD|WS_VISIBLE; //nullify control handle hCntrl=NULL; Width=100; Height=30; } //------------------------------------------------------------------------------------------- CppCntrl::~CppCntrl() { //if the handle is still valid then destroy the control if (hCntrl) { DestroyWindow(hCntrl); } } //------------------------------------------------------------------------------------------- HWND CppCntrl::Create(HWND hParent) { //get the application instance from the parent if (!hInst) { hInst=(HINSTANCE)GetWindowLong(hParent,GWL_HINSTANCE); } //create the control by calling the base class function hCntrl=CppProtoWnd::Create(hParent); return hCntrl; } //------------------------------------------------------------------------------------------- |
| QUOTE |
| #if !defined CppProtoWndH #define CppProtoWndH //------------------------------------------------------------------------------------------- //#define UNICODE //make active when compiling on WinNT/2000 #define WIN32_LEAN_AND_MEAN #define STRICT #include <windows.h> //------------------------------------------------------------------------------------------- //declare window procedure LRESULT CALLBACK MainWndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam); //------------------------------------------------------------------------------------------- //DECLARATION OF CppProtoWnd //------------------------------------------------------------------------------------------- class CppProtoWnd { private: //variables TCHAR chClassName[256]; //string to contain wnd class name TCHAR chCaption[256]; //string to contain wnd caption protected: //variables HINSTANCE hInst; //the application instance public: CppProtoWnd(); //constructor virtual ~CppProtoWnd(); //destructor virtual HWND Create(HWND hParent=NULL); //wnd creation, parent defaults to NULL TCHAR* GetWndCaption(); TCHAR* GetWndClassName(); virtual void SetInstance(HINSTANCE hInstance);//stores application instance virtual void SetWndCaption(TCHAR* chCap); //stores user choice of wnd caption virtual void SetWndClassName(TCHAR* chClass); //stores user choice of wnd class //variables int Left,Top,Width,Height; //wnd dimensions int nExStyle; //wnd extended style int nID; //control identifier or menu handle int nStyle; //wnd style }; //------------------------------------------------------------------------------------------- //DECLARATION OF CppCntrl. Class Hierarchy: CppProtoWnd<-CppCntrl //------------------------------------------------------------------------------------------- class CppCntrl:public CppProtoWnd { private: protected: public: CppCntrl(); virtual ~CppCntrl(); virtual HWND Create(HWND hParent=NULL); //wnd creation, parent handle param defaults to NULL //variables HWND hCntrl; //handle of the control }; //------------------------------------------------------------------------------------------- //DECLARATION OF CppWnd. Class Hierarchy: CppProtoWnd<-CppWnd //------------------------------------------------------------------------------------------- class CppWnd:public CppProtoWnd { private: int nClrSelected; //id of colour radiobutton last clicked int nClassStyle; //style passed to RegisterClassEx CppCntrl btnOK; //ok button cntrl object CppCntrl editMulti; //multiline edit cntrl protected: //messages virtual BOOL OnCommand(HWND hwndCtl,int nID,UINT uNotifyCode); //WM_COMMAND handler virtual int OnCreate(CREATESTRUCT *cs); //WM_CREATE handler virtual BOOL OnDestroy(); //WM_DESTROY handler public: CppWnd(); //constructor virtual ~CppWnd(); //destructor virtual HWND Create(HWND hParent=NULL); //wnd creation, parent defaults to NULL //class window procedure that global window procedure forwards msgs to virtual LRESULT CALLBACK CppWndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam); //variables HWND hAppWnd; //the main application window handle }; //------------------------------------------------------------------------------------------- #endif |
| QUOTE |
| #include "CppProtoWnd.h" CppWnd App; //declare instance of CppWnd object //------------------------------------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { //store application instance App.SetInstance(hInstance); //give the wnd a class name to register with os //App.SetWndClassName(TEXT("SimpleWndClass")); //give the wnd a caption //App.SetWndCaption(TEXT("SimpleWndClass")); //register and create wnd,checking the return value to ensure that a valid wnd handle has //been made. if (App.Create()) { MSG Msg; //a simple structure for storing message information //start message loop while (GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } return 0; } //------------------------------------------------------------------------------------------- LRESULT CALLBACK MainWndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam) { //just send the message and other parameters to the class function for handling return App.CppWndProc(hwnd,Message,wParam,lParam); } //------------------------------------------------------------------------------------------- |