Home | API | MFC | C++ | C | Up

Programming Windows API

Superclassing Example

#include <windows.h>
#define BUTTON1 1
#define BUTTON2 2
#define BUTTON3 3
WNDPROC OldWndProc;
HWND wndProcButton,superClassedButton,normalButton ;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SubClassProc(HWND, UINT,WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("superclassing"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 240, 240, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {

case WM_CREATE:
{
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
//create hidden button
wndProcButton = CreateWindow(TEXT("button"), NULL,WS_CHILD, 10, 10, 100, 80,hwnd,NULL, NULL, NULL);
//set button windows procedure address to address of subclass procedure and save original address
OldWndProc =(WNDPROC)SetClassLong (wndProcButton, GCL_WNDPROC, (DWORD)SubClassProc);
//create superclassed button
superClassedButton = CreateWindow(TEXT("button"), TEXT("superclass"),WS_CHILD|WS_VISIBLE, 10, 10, 100, 80,hwnd, (HMENU) BUTTON2, NULL, NULL);
//reset button superclassed procedure to original address
SetClassLong(wndProcButton, GCL_WNDPROC, (DWORD) OldWndProc);
//create non superclassed button
normalButton = CreateWindow(TEXT("button"), TEXT("normal"),WS_CHILD|WS_VISIBLE, 10, 100, 100, 80,hwnd, (HMENU) BUTTON3, NULL, NULL);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case BUTTON3:
MessageBeep(MB_ICONEXCLAMATION);
}
break;
case WM_DESTROY:
{

PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK SubClassProc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{

case WM_LBUTTONDOWN:
MessageBeep(-1);
default:
return CallWindowProc(OldWndProc,hwnd, uMsg, wParam, lParam);
}
return 0;
}

Home | API | MFC | C++ | C | Up
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated: 24 September 2022