Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1748
  • Last Modified:

MessageBox focus problem

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
robworley
Asked:
robworley
  • 14
  • 7
  • 6
  • +9
1 Solution
 
DalinCommented:
What Language do you use?
0
 
nietodCommented:
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
 
nietodCommented:
Note, If the application window is not in the foreground to begin with, move it to the foreground before creating the message box.  
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
alexoCommented:
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
 
robworleyAuthor Commented:
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
 
nietodCommented:
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
 
NickRepinCommented:
Did you try this:

  MessageBox( ..., ... | MB_APPLMODAL | MB_TOPMOST | MB_SETFOREGROUND) ?
0
 
nietodCommented:
>> 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
 
NickRepinCommented:
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
 
nietodCommented:
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
 
chensuCommented:
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
 
chensuCommented:
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
 
robworleyAuthor Commented:
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
 
nietodCommented:
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
 
Answers2000Commented:
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
 
migelCommented:
Do you call DefWindowProc after WM_SYSCOMAND processing?
0
 
robworleyAuthor Commented:
Answers2000: No modeless dialog active
0
 
robworleyAuthor Commented:
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
 
nietodCommented:
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
 
robworleyAuthor Commented:
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
 
nietodCommented:
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
 
robworleyAuthor Commented:
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
 
nietodCommented:
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
 
xyuCommented:
what about MB_SYSTEMMODAL option of the MessageBox???
0
 
nietodCommented:
xyu, considering what the problem is, how is that going to help.
0
 
MikeP090797Commented:
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
 
nietodCommented:
xyu and mikep, are you guys reading the history?  Look at August 28 10:52 AM.
0
 
MikeP090797Commented:
I guess not :)
0
 
alexoCommented:
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
 
nietodCommented:
Agreed, that is what I was trying to suggest yesterday, but yours reads better.
0
 
duneramCommented:
nietod:  Sometimes its nice to have stuff re-iterated.
0
 
nietodCommented:
Yeah, but why does he always have to say it more clearly and more precisely.
0
 
alexoCommented:
Sorry.  A typo.  Should have been: "Catch WM_SYSKEYDOWN..."
0
 
alexoCommented:
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
 
robworleyAuthor Commented:
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
 
alexoCommented:
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
 
nietodCommented:
Thanks, Alex, and also rob.
0
 
linda101698Commented:
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
 
alexoCommented:
Thanx Linda & Rob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 14
  • 7
  • 6
  • +9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now