Skip to content

Instantly share code, notes, and snippets.

@JeffMill
Created May 31, 2024 19:56
Show Gist options
  • Save JeffMill/36bc49c55b2d3dce81291c55e0bb40b5 to your computer and use it in GitHub Desktop.
Save JeffMill/36bc49c55b2d3dce81291c55e0bb40b5 to your computer and use it in GitHub Desktop.
Using a CBT Hook to modify a Windows dialog box.
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <SDKDDKVer.h>
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK AboutProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM /*lParam*/)
{
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
HHOOK g_hHook = NULL;
LRESULT CALLBACK HookProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
switch (nCode)
{
case HCBT_ACTIVATE:
HWND hwnd = (HWND)wParam;
SetWindowText(hwnd, L"Hook Caption");
HWND hwndButton;
int x = 145;
const unsigned int cx = 40;
const unsigned int spacing = 2;
hwndButton = GetDlgItem(hwnd, IDYES);
SetWindowText(hwndButton, L"πŸ‘");
MoveWindow(hwndButton, x, 83, cx, 32, TRUE);
x += cx + spacing;
hwndButton = GetDlgItem(hwnd, IDNO);
SetWindowText(hwndButton, L"πŸ‘Ž");
MoveWindow(hwndButton, x, 83, cx, 32, TRUE);
x += cx + spacing;
hwndButton = GetDlgItem(hwnd, IDCANCEL);
MoveWindow(hwndButton, x, 83, cx, 32, TRUE);
SetWindowText(hwndButton, L"🀚");
return 0;
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE /*hPrevInstance*/,
_In_ PWSTR /*lpCmdLine*/,
_In_ int /*nCmdShow*/)
{
HWND hwndParent = NULL;
g_hHook = SetWindowsHookEx(WH_CBT, HookProc, NULL, GetCurrentThreadId());
//DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hwndParent, AboutProc);
MessageBox(hwndParent, L"Text", L"Caption", MB_YESNOCANCEL | MB_ICONINFORMATION);
UnhookWindowsHookEx(g_hHook);
g_hHook = NULL;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment