I've not used MFC as I don't have access to it, though I'm sure you can translate this C code to one that suits your needs. It only requires that you handle the WM_SETFOCUS message for your button and handle the (user defined WM_COMMAND) MM_OLDHWND message in your main window's windowProc.
#include <windows.h>
#include <commctrl.h>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK btnWndProc(HWND, UINT, WPARAM, LPARAM);
long OldBtnProc;
HWND btnWnd, edit1Wnd, edit2Wnd, edit3Wnd;
#define IDOK 1000
#define IDC_EDIT1 1001
#define IDC_EDIT2 1002
#define IDC_EDIT3 1003
#define MM_OLDHWND 1004
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HINSTANCE hInst = hThisInstance;
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Code::Blocks Template Windows App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
194,
128,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
HFONT hfont0 = CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, FALSE, 1, 400, 0, 0, 0, "Ms Shell Dlg 2");
btnWnd = CreateWindowEx(0, WC_BUTTON, "OK", WS_VISIBLE | WS_CHILD | WS_TABSTOP, 101, 63, 75, 23, hwnd, (HMENU)IDOK, hInst, 0);
SendMessage(btnWnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
edit1Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 8, 60, 20, hwnd, (HMENU)IDC_EDIT1, hInst, 0);
SendMessage(edit1Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
edit2Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 36, 60, 20, hwnd, (HMENU)IDC_EDIT2, hInst, 0);
SendMessage(edit2Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
edit3Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 63, 60, 20, hwnd, (HMENU)IDC_EDIT3, hInst, 0);
SendMessage(edit3Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
OldBtnProc = (long) SetWindowLong(btnWnd, GWL_WNDPROC, (long)btnWndProc);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_COMMAND:
switch (wParam)
{
case MM_OLDHWND:
if ((HWND)lParam == edit1Wnd)
MessageBox(NULL, "Editbox 1 had focus", "Attention", MB_ICONEXCLAMATION);
else if ((HWND)lParam == edit2Wnd)
MessageBox(NULL, "Editbox 2 had focus", "Attention", MB_ICONEXCLAMATION);
else if ((HWND)lParam == edit3Wnd)
MessageBox(NULL, "Editbox 3 had focus", "Attention", MB_ICONEXCLAMATION);
else
MessageBox(NULL, "Unknown control had focus", "Attention", MB_ICONEXCLAMATION);
return 1;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK btnWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
PostMessage(GetParent(hwnd), WM_COMMAND, MM_OLDHWND, (LPARAM)wParam);
default:
return CallWindowProc((WNDPROC)OldBtnProc, hwnd, message, wParam, lParam);
}
}
Thanks for the thought inspiring question.
|