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
|
// Form3.cpp
// cl Form3.cpp Kernel32.lib User32.lib /FeForm3 /O1 /Os /MT /GA - 88,064 Bytes With MS C Runtime Lib
// cl Form3.cpp /O1 /Os /GS- TCLib.lib kernel32.lib user32.lib - 5,120 bytes with my custom C Runtime
// g++ Form3.cpp -oForm3_MinGW64.exe -mwindows -m64 -s -Os
//#define DEBUG
//#define TCLib
#include <windows.h>
#ifdef TCLib
#include "stdio.h"
#include "tchar.h"
#else
#include <stdio.h>
#include <tchar.h>
#endif
#include "Form3.h"
#ifdef DEBUG
FILE* fp = NULL;
#endif
LRESULT CALLBACK fnBtnSubClass(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) // This procedure 'hooks' the
{ // internal Window Procedure for
HWND hParent=GetParent(hwnd); // the "button" class. It executes
int iCtrlId= GetDlgCtrlID(hwnd); // before executiion enters the
switch(msg) // "button" class Window Procedure.
{ // API Function CallWindowProc() in
case WM_LBUTTONDOWN: // return statement passes execution
{ // to internal "button" class WndProc.
#ifdef DEBUG // Note however that if the button
fprintf(fp," Entering fnBtnSubClass() With msg==WM_LBUTTONDOWN\n"); // with Control ID 1505 (BUTTON_2)
fprintf(fp," iCtrlId == %u\n",iCtrlId); // receives a WM_LBUTTONDOWN or
fprintf(fp," Leaving fnBtnSubClass() With msg==WM_LBUTTONDOWN\n\n"); // WM_LBUTTONDBLCLK, the WndProc for
#endif // the "button" class is not called -
if(iCtrlId==BUTTON_2) // this procedure 'eats' the message.
return 0; // the function exits or 'short circuits'
break; // with a return statement - and
} // CallWindowProc() doesn't get called.
case WM_LBUTTONDBLCLK:
{
#ifdef DEBUG
fprintf(fp," Entering fnBtnSubClass() With msg==WM_LBUTTONDBLCLK\n");
fprintf(fp," iCtrlId == %u\n",iCtrlId);
fprintf(fp," Leaving fnBtnSubClass() With msg==WM_LBUTTONDBLCLK\n\n");
#endif
if(iCtrlId==BUTTON_2)
return 0;
}
}
return CallWindowProc((WNDPROC)GetWindowLongPtr(hParent,0), hwnd, msg, wParam, lParam);
}
long fnWndProc_OnCreate(WndEventArgs& Wea)
{
WNDPROC btnWndProc=NULL; // Holds address of internal Window Procedure for "button" Class
HWND hBtn =NULL; // HWND of "button"
#ifdef DEBUG
fprintf(fp," Entering fnWndProc_OnCreate()\n");
#endif
Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
#ifdef DEBUG
fprintf(fp," Wea.hIns = %u\n",Wea.hIns);
fprintf(fp," Wea.hWnd = %u\n",Wea.hWnd);
#endif
hBtn=CreateWindowEx(0,_T("button"),_T("Button #1"),WS_CHILD|WS_VISIBLE,105,70,90,25,Wea.hWnd,(HMENU)BUTTON_1,Wea.hIns,0); // Create Button #1
btnWndProc=(WNDPROC)SetWindowLongPtr(hBtn,GWLP_WNDPROC,(LONG_PTR)fnBtnSubClass); // Subclass "button"
SetWindowLongPtr(Wea.hWnd,0,(LONG_PTR)btnWndProc);
hBtn=CreateWindowEx(0,_T("button"),_T("Button #2"),WS_CHILD|WS_VISIBLE,105,110,90,25,Wea.hWnd,(HMENU)BUTTON_2,Wea.hIns,0); // Create Button #2
btnWndProc=(WNDPROC)SetWindowLongPtr(hBtn,GWLP_WNDPROC,(LONG_PTR)fnBtnSubClass); // Subclass "button"
SetWindowLongPtr(Wea.hWnd,0,(LONG_PTR)btnWndProc); // Store address in Window's .cbWndExtra bytes
CreateWindowEx(0,_T("button"),_T("Exit"),WS_CHILD|WS_VISIBLE,105,150,90,25,Wea.hWnd,(HMENU)BUTTON_EXIT,Wea.hIns,0); // Exit Button Not Subclassed
#ifdef DEBUG
fprintf(fp," Leaving fnWndProc_OnCreate()\n\n");
#endif
return 0;
}
long fnWndProc_OnCommand(WndEventArgs& Wea)
{
#ifdef DEBUG
fprintf(fp," Entering fnWndProc_OnCommand()\n");
fprintf(fp," Wea.hWnd = %u\n",Wea.hWnd);
#endif
switch(LOWORD(Wea.wParam))
{
case BUTTON_1:
{
MessageBox(Wea.hWnd, _T("Got Click From Button #1."), _T("Click Report"), MB_OK);
break;
}
case BUTTON_2:
{
MessageBox(Wea.hWnd, _T("You'll Never See This Message. It Was Eaten By fnBtnSubClass!"), _T("Click Report"), MB_OK);
break;
}
case BUTTON_EXIT:
{
#ifdef DEBUG
fprintf(fp," Got Click Of Exit Button!\n");
#endif
PostMessage(Wea.hWnd, WM_CLOSE, 0, 0);
break;
}
}
#ifdef DEBUG
fprintf(fp," Leaving fnWndProc_OnCommand()\n\n");
#endif
return 0;
}
long fnWndProc_OnDestroy(WndEventArgs& Wea)
{
#ifdef DEBUG
fprintf(fp," Entering fnWndProc_OnDestroy()\n");
fprintf(fp," Wea.hWnd = %u\n",Wea.hWnd);
fprintf(fp," Leaving fnWndProc_OnDestroy()\n");
#endif
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 hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("Form3");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
#ifdef DEBUG
fp=fopen("Output.txt","w");
fprintf(fp,"Entering WinMain()\n");
#endif
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=0;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=NULL; wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra=sizeof(void*); // Extra bytes for button WNDPROC address
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,75,75,320,305,HWND_DESKTOP,0,hIns,0);
#ifdef DEBUG
fprintf(fp," hIns = %u\n",hIns);
fprintf(fp," hWnd = %u\n\n",hWnd);
#endif
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
#ifdef DEBUG
fprintf(fp,"Leaving WinMain()\n");
fclose(fp);
#endif
return (int)messages.wParam;
}
|