Solved

Not receiving WM_KILLFOCUS in modeless dialog

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

696 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