Solved

Not receiving WM_KILLFOCUS in modeless dialog

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

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 30

Expert Comment

by:Zoppo
ID: 37838268
:o)

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

17 Experts available now in Live!

Get 1:1 Help Now