Superclassing Demo

#include <windows.h> #define BUTTON1 1 #define BUTTON2 2 WNDPROC lpfnOldWndProc; HWND hWnd; HWND SuperClassedButton; HWND normalButton; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK SuperClassedButtonProc(HWND, UINT, WPARAM, LPARAM); INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG Msg; WNDCLASSEX WndClsEx; // Register main window WndClsEx.cbSize = sizeof(WNDCLASSEX); WndClsEx.style = CS_HREDRAW | CS_VREDRAW; WndClsEx.lpfnWndProc = WndProc; WndClsEx.cbClsExtra = 0; WndClsEx.cbWndExtra = 0; WndClsEx.hInstance = hInstance; WndClsEx.hIcon = LoadIcon(NULL,IDI_APPLICATION); WndClsEx.hCursor = LoadCursor(NULL,IDC_ARROW); WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClsEx.lpszMenuName = NULL; WndClsEx.lpszClassName = TEXT("WndMsg"); WndClsEx.hIconSm = LoadIcon(NULL,IDI_APPLICATION); RegisterClassEx(&WndClsEx); hWnd = CreateWindowEx( 0, TEXT("WndMsg"), TEXT("Superclass Example"), WS_OVERLAPPEDWINDOW, 10, 10, 240, 140, NULL, NULL, hInstance, NULL); ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); // Normal button normalButton = CreateWindow( TEXT("BUTTON"), TEXT("Normal"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 100, 80, hWnd, (HMENU)BUTTON1, hInstance, NULL); // Create superclass WNDCLASS wc; // Get existing BUTTON class GetClassInfo( NULL, TEXT("BUTTON"), &wc); // Save original button procedure lpfnOldWndProc = wc.lpfnWndProc; // Modify class information wc.lpfnWndProc = SuperClassedButtonProc; wc.lpszClassName = TEXT("MyButtonClass"); wc.hInstance = hInstance; // Register new class RegisterClass(&wc); // Create superclassed button SuperClassedButton = CreateWindow( TEXT("MyButtonClass"), TEXT("Super"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 120, 10, 100, 80, hWnd, (HMENU)BUTTON2, hInstance, NULL); while(GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_COMMAND: switch(LOWORD(wParam)) { case BUTTON1: MessageBeep(MB_ICONEXCLAMATION); break; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc( hwnd, Msg, wParam, lParam); } return 0; } // Superclassed button procedure LRESULT CALLBACK SuperClassedButtonProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_LBUTTONUP: MessageBeep(MB_OK); break; } // Pass message back to BUTTON control return CallWindowProc( lpfnOldWndProc, hwnd, Msg, wParam, lParam); }