Simple ListBox subclassing example
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#define IDC_LBOX 4001
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
char *szAppName = "WinFrame";
char *szFrameClass = "cWinFrame";
HWND hFrame;
HANDLE hInst;
HWND hList;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC) WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground= (HBRUSH)COLOR_3DSHADOW;
wcx.lpszMenuName = NULL;
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = 0;
if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;
hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
260, 250,
NULL, NULL, hInst, NULL);
if(!hFrame) return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void OnCreate(HWND hWnd);
LRESULT CALLBACK SubclassWndProcLB(HWND hwnd, UINT wm, WPARAM wParam, LPARAM lParam);
WNDPROC wndProcOrigLB;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_CREATE:
OnCreate(hWnd);
return 0;
case WM_DESTROY:
SetWindowLongPtr(hList, GWLP_WNDPROC, (LONG_PTR)wndProcOrigLB);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
void OnCreate(HWND hWnd)
{
//InitCommonControls();
hList = CreateWindowEx(0, "ListBox", NULL,
WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VISIBLE | WS_VSCROLL
, 1, 50, 200, 100
, hWnd, (HMENU)IDC_LBOX, hInst, NULL);
for (int i = 1; i <= 10; i++) {
TCHAR szTmp[10];
wsprintf(szTmp, "row %d", i);
SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)szTmp);
}
wndProcOrigLB = (WNDPROC)SetWindowLongPtr(hList, GWLP_WNDPROC, (LONG_PTR)SubclassWndProcLB);
SetFocus(hList);
}
// Subclass Window Proc
LRESULT CALLBACK SubclassWndProcLB(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// debug it
TCHAR szTmp[100];
wsprintf(szTmp, TEXT("%Xh"), uMsg);
SetWindowText(hFrame, szTmp);
//
CallWindowProc(wndProcOrigLB, hwnd, uMsg, wParam, lParam);
}