Solved

Modal non-child window causes mdiform application to lose focus

Posted on 2000-03-15
5
400 Views
Last Modified: 2006-11-17
Hi:

I'm running an MDI application.  I have a routine which requires a modal window to pop up.  After this modal window is closed, my entire application always loses focus and the next application in the MS Windows lineup gains focus instead.  Given that, here's my question...  how can I prevent or workaround this?  Examples or specifics are always nice.

Looking forward to your response.
Thank you.
0
Comment
Question by:ksm
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2621694
you may be able to use the api call SetWindowPos. Put this in a module:


Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_TOP = 0


and you could implement it in this way:

Function StayOnTop(Form As Form)
Dim lFlags As Long
Dim lStay As Long

lFlags = SWP_NOSIZE Or SWP_NOMOVE
lStay = SetWindowPos(Form.hwnd, HWND_TOP, 0, 0, 0, 0, lFlags)
End Function

you could call this in the Unload event of the form that is giving you problems
0
 

Author Comment

by:ksm
ID: 2621865
I actually wrote a "StayOnTop" function a while ago but didn't think to use it here.  I'm not really sure how this would work, though.  There's no use putting this in my modal form, since it's modal and would always be visible anyway.  If I put it in my mdiform, then it would stay on top of all other applications, which would not be good.

Maybe I can try some sort of Zorder call in my mdiform just after the modal window closes.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 2621877
yes that was what I was suggesting...HWND_TOP will just move it to the top of the Z Order whereas using HWND_TOPMOST leaves it on top permanently. You could implement it in a way that would do what you need it to do i believe by calling something like this after the modal window is closed
0
 

Author Comment

by:ksm
ID: 2621900
Ah, OK.  I was thinking "permanant" when I saw StayOnTop.  Anyhow, I just got done my ZOrder tests and it seems like it's the way to go in this case.  In fact, after I ran the tests, I found an MS article which indicates this...  "For an MDIForm or Form object, ZOrder sends the form to either the front or the back of the screen, depending on the value of position. As a result, forms can be displayed in front of or behind other running applications."

Thanks very much, though.  Thanks for the quick response.
0
 

Author Comment

by:ksm
ID: 2621906
Thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

15 Experts available now in Live!

Get 1:1 Help Now