I almost got it to work. I didn't add a new class for the button. Just do this:
To create the dialog box:
CNotFocusedDlg* dlg = new CNotFocusedDlg();
dlg->Create(IDD_NOT_FOCUSED);
dlg->ShowWindow(SW_SHOW);
In your dialog class, add the WM_ACTIVATE and WM_MOUSEACTIVATE handlers with the following code:
In the .h:
class CNotFocusedDlg : public CDialog
{
DECLARE_DYNAMIC(CNotFocusedDlg)
public:
CNotFocusedDlg(CWnd* pParent = NULL);
virtual ~CNotFocusedDlg();
enum { IDD = IDD_NOT_FOCUSED };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
afx_msg void OnBnClickedButton1();
};
Then in the .cpp:
BEGIN_MESSAGE_MAP(CNotFocusedDlg, CDialog)
ON_WM_ACTIVATE()
ON_WM_MOUSEACTIVATE()
ON_BN_CLICKED(IDC_BUTTON1, &CNotFocusedDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
void CNotFocusedDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
if (nState == WA_ACTIVE || nState == WA_CLICKACTIVE)
{
if (pWndOther != NULL)
pWndOther->SetActiveWindow();
}
CDialog::OnActivate(nState, pWndOther, bMinimized);
}
int CNotFocusedDlg::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
return MA_NOACTIVATE;
}
void CNotFocusedDlg::OnBnClickedButton1()
{
}
This code seems to work except for the first WM_ACTIVATE message. I tried to use the WM_EX_NOACTIVATE style but it seems I'm missing something there...
Anyway I hope it will help you a bit.