Solved

Not receiving WM_KILLFOCUS in modeless dialog

Posted on 2012-04-12
5
1,814 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 78
Arduino EDI - Programming language 3 80
Why is "packages element is not declared"? 2 135
How do ASP.NET and MVC work together? 4 29
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now