Solved

MessageBox focus problem

Posted on 1998-08-27
39
1,717 Views
Last Modified: 2013-12-03
When calling the message box function from my program it is not given focus immediately over the main program window.  I must first click on the main program window so that the message box appears from behind it.  I have tried numerous combinations of SetFocus(), SetForegroundWindow and so on on the main program window but it always loses focus when the message box is created leaving the message box invisible behind it.
0
Comment
Question by:robworley
  • 14
  • 7
  • 6
  • +9
39 Comments
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
What Language do you use?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Is your program in the foreground before the message box is displayed?
Are you specifying the window handle of the main window when you create the messagebox?

Post you code, that would help.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Note, If the application window is not in the foreground to begin with, move it to the foreground before creating the message box.  
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Do you use Win98?  In Win98 changes were made so that a window (including message boxes) cannot "steal" focus from other applications.  There are undocumented ways of overcoming that though...
0
 

Author Comment

by:robworley
Comment Utility
Dalin: We are using C (Watcom compiler)

Nietod: YES program is in foreground, YES specifying handle in main window.  Code is very large.  Already tried your next suggestion.

Alexo: We are using Win95 presently
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Your main window has the focus before the message box appears, does it then loose it?  or does it retain it sot he message box doessn't have it?   Does your main window process any of the focus/activation related events?

From you description it sounds like the message box appears behind your main window.  Is that right?  That' can't be if you are specifying the main window's handle to MessageBox().  

You don't have to post the whole program, just the relevant lines where you create the message box.
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
Did you try this:

  MessageBox( ..., ... | MB_APPLMODAL | MB_TOPMOST | MB_SETFOREGROUND) ?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> MessageBox( ..., ... | MB_APPLMODAL | MB_TOPMOST | MB_SETFOREGROUND)

None of those options should be necessary.  If the application is already in the foreground, then the message box should be.  There is a problem somewhere that needs to be fixed.
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
To nietod:
Of course, there is a problem somewhere. But program is very large, we don't see the source code, and we (experts) can only make some (many) guess.
MB_SETFOREGROUND may be easy enough solution.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
But if you suggest a patch that covers up a problem rather than fixing it, you should infomr the client that as they might not be able to tell that and  they might want that.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
I guess you passed NULL or a wrong window handle for the hWnd parameter of MessageBox. Pass the window handle of which you are calling MessageBox.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Pass the window handle from which you are calling MessageBox.

LRESULT WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    //...
    case WM_...:
        //...
        ::MessageBox(hWnd, ...);
        //...
        break;
    //...
}

0
 

Author Comment

by:robworley
Comment Utility
I reject the answer because I've already tried the given flag combo.

Now to address the other questions:

Nietod:  The main window does have focus before the message box is diaplayed.  The message box is invoked by a user's keyboard input of <F10> which I grab and then do the following message box call:

  MessageBox(hwnd (which is valid), text, title, MB_YESNO) and I've tried almost every
  possible combo of the other flags including MB_SETFOREGROUND alone and OR'd
  with MB_TOPMOST etc.

I first thought that the message box was being created behind the main window but I don't think so anymore.  Although the message box does not become visible until I click on the
main window ( which at this point does not have focus ).

So here is a recap.

The user presses a key in the main window which has focus.

I make the call to MessageBox with valid handles/text etc. and MB_YESNO.

The main window then goes out of focus and no MessageBox is visible.

I then click on the main window (which is out of focus) and the message box appears in focus.

NOTE: I am using <F10> as a keystroke in my program and I've intercepted the WM_SYSCOMMAND message to disable its use there.  There are other places in my program where I use <F10>  to signal a message box with no problems.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I wonder if when the message box appears (needs to, that is) it sends some messages to your main window that your main window doesn't handle and causes the whole process to get locked up.  You might want to use Spy or another utility to look at the messages your window is getting at that point and see how they are handled.  Or you could try paring down your window and window procedures to the the barest essentials and see if the problem goes away.  If so add back peices until it reappears.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
Do you have a modeless dialog active in your big program ?

I (think I might) know the answer if you do
(if you are, please explain a little on the modeless dialog(s) so I can attempt the answer)
0
 
LVL 12

Expert Comment

by:migel
Comment Utility
Do you call DefWindowProc after WM_SYSCOMAND processing?
0
 

Author Comment

by:robworley
Comment Utility
Answers2000: No modeless dialog active
0
 

Author Comment

by:robworley
Comment Utility
migel: I removed the WM_SYSCOMMAND processing just to test the message box again with no luck.  So the answer to your question is yes.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Try to remove anything else you possible can.  Comment out any message handler that is not completely essential for this test.  Does the problem still occur.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:robworley
Comment Utility
nietod:  I did that with no luck.

Another tidbit:  When the message box call is made and the main window loses focus I can make the message box visible by clicking on the main window OR ANY OTHER WINDOW which I found strange.  So basically if I click on any window the message box appears.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
We really need to see the code.  If you can't post it here, can you e-mail it to me at nietod@theshop.net?
0
 

Author Comment

by:robworley
Comment Utility
I have identified the culprit as the usage of the F10 key.  We need to capture the F10 key for backward compatability with our app.  We were previously just catching WM_SYSKEYDOWN messages and passing them along to our app to handle.  If the user pressed F10 and our app then generates a MessageBox we get the weird behavior described above.  We were able to observe that the system menu on the MessageBox had focus when the weird stuff was happening...as if we had pressed F10 after the MessageBox appeared.

The problem is somewhat solved with our current workaround: we are registering F10 as a hotkey.  This makes the weird behavior go away.  However, the undesirable side-effect is that no other apps have access to the F10 key now.

Any suggestions as to how we can process the F10 key without registering it as a hotkey and without experiencing the weirdness with the MessageBox.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Actually, it probably is the SYSKEYUP (not down) that is the problem.  That is harder to handle because that message doesn't make it to your message loop.  It goes to the message loop that windows has inside the MessageBox() procedure.  

One sort of fix would be to intercept the key down message and ignore it.  Then when the key up message comes in (to your app), bring the message box up.
0
 
LVL 3

Expert Comment

by:xyu
Comment Utility
what about MB_SYSTEMMODAL option of the MessageBox???
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
xyu, considering what the problem is, how is that going to help.
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
You may want to try this workaround:
Before MessageBixm create a second thread. Use FindWindow from inside it in a loop to get MessageBox's hWnd. Once you get it, use SetForegroundWindow on the messagebox itself
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
xyu and mikep, are you guys reading the history?  Look at August 28 10:52 AM.
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
I guess not :)
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Catch WM_SYSCOMMAND and swallow VK_F10.  Catch WM_SYSKEYUP and do your stuff on VK_F10.  Alternatively, catch WM_SYSCOMMAND and do your stuff on SC_KEYMENU.  In both cases, do not pass the processing to the default window proc afther handling the F10 key.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Agreed, that is what I was trying to suggest yesterday, but yours reads better.
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
nietod:  Sometimes its nice to have stuff re-iterated.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Yeah, but why does he always have to say it more clearly and more precisely.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Sorry.  A typo.  Should have been: "Catch WM_SYSKEYDOWN..."
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Since F10 is the equivalent of tapping the ALT key in the windows UI, I fired up Spy++ and checked the generated messages.  I suggest you do the same.

Todd, most functions associated with keystrokes or mouse clicks work on RELEASE.  Raymond Chen explained the rationale sometime in one of MS newsgroups.  Don't remember the details.
0
 

Author Comment

by:robworley
Comment Utility
NIETOD or ALEXO: You two get the points.  NIETOD was first with the answer, ALEXO's clarification helped.  You two decide who can post the solution and we'll give you the points.

Thanks!
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Give them to Todd since he was first.

If you *really* feel that my clarification helped, post a 0-point question in the customer support area and ask Linda to give me points "for help on Q.10075837".
0
 
LVL 22

Accepted Solution

by:
nietod earned 510 total points
Comment Utility
Thanks, Alex, and also rob.
0
 
LVL 7

Expert Comment

by:linda101698
Comment Utility
alexo,

Look for the question to award you some points.  Often a lot of the solutions to problems are a troubleshooting process and ideas from one or more  experts can lead to the solution of difficult to solve problems.  Thanks for your help on this question.

Linda Gardner
Customer Service @ Experts Exchange
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Thanx Linda & Rob
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

7 Experts available now in Live!

Get 1:1 Help Now