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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
//Main.cpp //Program displays a main Form/Window/Dialog With Three Buttons on it to simulate
#include <windows.h> //a program started from a main Form with three modules/options/modalities within
#include <tchar.h> //which it operates. When you click the top button a new Form/Window/Dialog is
#include <stdio.h> //created with CreateWindow() of the "Form1" Window Class, and in the WM_CREATE
#include "Main.h" //handler of this new window it disables the main form with the three buttons and
#include "Form1.h" //therefore is an example of a modal dialog. When you dismiss this modal dialog
#include "Form2.h" //and click on Button/Option #2 on the Main Form, a CreateWindow() call creates a
#include "Form3.h" //window of "Form2" Window Class, and in the WM_CREATE handler...
long fnWndProc_OnCreate(lpWndEventArgs Wea) //...for this window it hides or makes invisible the main
{ //window. After dismssing this window you'll find that you
DWORD dwStyle=WS_CHILD|WS_VISIBLE; //can click on the Option #3 button as many times as you like
TCHAR szClassName[16]; //because the window-form-dialog it creates neither disables
WNDCLASSEX wc; //nor makes invisible the main window. Further note that these
//Option #3 windows can be interacted with regardless of what-
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance; //ever is going on with the other windows.
CreateWindow(_T("button"),_T("Option #1"),dwStyle,65,15,120,25,Wea->hWnd,(HMENU)IDC_BUTTON_FORM1,Wea->hIns,0);
CreateWindow(_T("button"),_T("Option #2"),dwStyle,65,55,120,25,Wea->hWnd,(HMENU)IDC_BUTTON_FORM2,Wea->hIns,0);
CreateWindow(_T("button"),_T("Option #3"),dwStyle,65,95,120,25,Wea->hWnd,(HMENU)IDC_BUTTON_FORM3,Wea->hIns,0);
//Register Window Classes For Form1, Form2 and Form3
wc.cbSize=sizeof(WNDCLASSEX), wc.style=CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra=0, wc.cbWndExtra=0;
wc.hInstance=Wea->hIns, wc.hIcon=LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm=0, wc.hCursor=LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH), wc.lpszMenuName=NULL;
_tcscpy(szClassName,_T("Form1")), wc.lpszClassName=szClassName;
wc.lpfnWndProc=fnForm1_WndProc;
RegisterClassEx(&wc);
_tcscpy(szClassName,_T("Form2"));
wc.lpfnWndProc=fnForm2_WndProc;
wc.lpszClassName=szClassName; //Note that a WM_CREATE call is akin to a constructor call in typical
RegisterClassEx(&wc); //C++ class architecture. When you receive this call/message Windows
//has finished doing what it needs to support the Window object, and
_tcscpy(szClassName,_T("Form3")); //is 'passing the ball' to you. In my apps with multiple windows I
wc.lpszClassName=szClassName; //typically use the WM_CREATE handler to register any window classes
wc.lpfnWndProc=fnForm3_WndProc; //I need in the app, so that I can make CreateWindow() calls when I
RegisterClassEx(&wc); //need to instantiate a window of some type.
return 0;
}
long btnForm1_Click(lpWndEventArgs Wea) //This is an 'Event Handler' for a click of the top button on the
{ //main Form. It uses CreateWindowEx() to instantiate a new Window
HWND hWnd; //of class "Form1" Note that the last parameter of the call is
//Wea->hWnd. That is the HWND of the main program Window. The
hWnd=CreateWindowEx //last parameter is the Creation Parameters parameter. It can be
( //retrieved (as can all the others) through the CREATIONSTRUCT a
0, //pointer to which is received in the lParam of the WM_CREATE
_T("Form1"), //message for the newly instantiating Window.
_T("Form1"),
WS_OVERLAPPEDWINDOW,
50,25,310,185,
Wea->hWnd,
(HMENU)0,
GetModuleHandle(0),
Wea->hWnd
);
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 0;
}
long btnForm2_Click(lpWndEventArgs Wea)
{
HWND hWnd;
hWnd=CreateWindowEx
(
0,
_T("Form2"),
_T("Form2"),
WS_OVERLAPPEDWINDOW,
200,250,310,185,
Wea->hWnd,
(HMENU)0,
GetModuleHandle(0),
Wea->hWnd
);
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 0;
}
long btnForm3_Click(lpWndEventArgs Wea)
{
HWND hWnd;
hWnd=CreateWindowEx
(
0,
_T("Form3"),
_T("Form3"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,300,260,
0,
(HMENU)0,
GetModuleHandle(0),
Wea->hWnd
);
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 0;
}
long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
switch(LOWORD(Wea->wParam))
{
case IDC_BUTTON_FORM1:
return btnForm1_Click(Wea);
case IDC_BUTTON_FORM2:
return btnForm2_Click(Wea);
case IDC_BUTTON_FORM3:
return btnForm3_Click(Wea);
}
return 0;
}
long fnWndProc_OnClose(lpWndEventArgs Wea) //Search And Destroy Mission For Any Form3
{ //Windows Hanging Around.
HWND hForm;
if(MessageBox(Wea->hWnd,_T("Do You Wish To Exit?"),_T("Exit App?"),MB_YESNO)==IDYES)
{
do //If FindWindow() returns something other
{ //than zero, it found a window matching
hForm=FindWindow(_T("Form3"),_T("Form3")); //the description of what you are looking
if(hForm) //for. In that case, send a WM_CLOSE
SendMessage(hForm,WM_CLOSE,0,0); //message. If NULL is returned then just
else //break out of the loop and terminate the
break; //app.
}while(TRUE);
DestroyWindow(Wea->hWnd);
PostQuitMessage(0);
}
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].iMsg==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("Multiple Forms");
WNDCLASSEX wc={};
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=hInstance, wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,250,500,260,170,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|