Solved

MessageBox focus problem

Posted on 1998-08-27
39
1,735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 7
  • 6
  • +9
39 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1413967
What Language do you use?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413968
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
ID: 1413969
Note, If the application window is not in the foreground to begin with, move it to the foreground before creating the message box.  
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:alexo
ID: 1413970
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
ID: 1413971
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
ID: 1413972
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
ID: 1413973
Did you try this:

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

Expert Comment

by:nietod
ID: 1413974
>> 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
ID: 1413975
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
ID: 1413976
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
ID: 1413977
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
ID: 1413978
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
ID: 1413979
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
ID: 1413980
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
ID: 1413981
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
ID: 1413982
Do you call DefWindowProc after WM_SYSCOMAND processing?
0
 

Author Comment

by:robworley
ID: 1413983
Answers2000: No modeless dialog active
0
 

Author Comment

by:robworley
ID: 1413984
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
ID: 1413985
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
 

Author Comment

by:robworley
ID: 1413986
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
ID: 1413987
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
ID: 1413988
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
ID: 1413989
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
ID: 1413990
what about MB_SYSTEMMODAL option of the MessageBox???
0
 
LVL 22

Expert Comment

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

Expert Comment

by:MikeP090797
ID: 1413992
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
ID: 1413993
xyu and mikep, are you guys reading the history?  Look at August 28 10:52 AM.
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1413994
I guess not :)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1413995
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
ID: 1413996
Agreed, that is what I was trying to suggest yesterday, but yours reads better.
0
 
LVL 2

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:alexo
ID: 1414000
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
ID: 1414001
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
ID: 1414002
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
ID: 1414003
Thanks, Alex, and also rob.
0
 
LVL 7

Expert Comment

by:linda101698
ID: 1414004
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
ID: 1414005
Thanx Linda & Rob
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
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 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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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