Solved

Not receiving WM_KILLFOCUS in modeless dialog

Posted on 2012-04-12
5
1,827 Views
Last Modified: 2013-12-03
My MFC app has a modeless dialog. I'd like to know when the user clicks outside of the dialog (at which time I'll hide, but not destroy, the dialog). I'm not receiving the WM_KILLFOCUS message.

I used the ClassWizard to handle the WM_KILLFOCUS message. It correctly added the ON_WM_KILLFOCUS() message map and handler function. However, when I click outside the dialog (and the dialog shading clearly changes to show that it no longer has the focus), OnKillFocus() isn't called. When I click an OK button in the dialog (which calls CDialog::OnOK();), OnKillFocus() is called.
0
Comment
Question by:sachelis
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 37837892
Can you try Spy++ (http://msdn.microsoft.com/en-us/library/dd460756.aspx) to verify that the message is actually being sent?
0
 

Author Comment

by:sachelis
ID: 37838056
Using Spy++, I only receive the Set/Kill focus messages when OnOK() and OnCancel() are called, not when the dialog simply gains/loses the focus via mouse clicks. I haven't drilled into OnOK/Cancel to see what actually results in the message being sent.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 37838105
Hi sachelis,

WM_KILLFOCUS is not the best to implement what you want, because the dialog itself cannot have the focus, only controls within the dialog can have the focus. The WM_KILLFOCUS is sent to the control which had the focus before the focus has changed, it's not sent to your dialog itself, so you can't handle it in the dialog's class.

Instead simply use WM_ACTIVATE message in your dialog - to do so add a OnActivate i.e. via ClassWizard and implement it somehow like this:
void CModelessTestDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CDialogEx::OnActivate(nState, pWndOther, bMinimized);

	if ( WA_INACTIVE == nState )
	{
		ShowWindow( SW_HIDE );
	}
}

Open in new window

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:sachelis
ID: 37838193
Truely outstanding. Zoppo didn't bog down into "why do you want to do that?" which often occurs, but instead explained why my approach wasn't working and provided an easy to implement solution. Thanks!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 37838268
:o)

thanks, nice to get such a feedback ...
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 85
Remove Office macro by VC++ 10 97
Build VS2015 project in bat command 3 87
DCT of  2D array using fftw in c++ 9 41
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question