Link to home
Start Free TrialLog in
Avatar of cachedVB
cachedVB

asked on

Form load help

I have a form (called form2) which loads form1 and then form2 unloads itself.  How do I make form1 be in the same position as form2... i mean so it doesnt steal the attention away from another program when it is loaded unless it was form2 which had the attention in the first place.
Avatar of PaulHews
PaulHews
Flag of Canada image

Private Sub Command1_Click()

    Load Form2
    Form2.Move Me.Left, Me.Top, Me.Width, Me.Height
    Form2.Show
    Unload Me
   
End Sub
Avatar of cachedVB
cachedVB

ASKER

what about it being on top of other progs.
You should have asked for that at the beginning.

Say Form2 is the one you want on top:
'Form1 Code
Option Explicit

Private Sub Command1_Click()
    Form2.Show
End Sub

'Form2 Code
Option Explicit



' Declare functions obtained from the API Text Viewer
Private Declare Function SetWindowPos Lib _
    "user32" (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
' Constants for SetWindowPos hWndInsertAfter Parameter
Private Const HWND_TOP = 0
Private Const HWND_BOTTOM = 1
Private Const HWND_NOTOPMOST = -2
Private Const HWND_TOPMOST = -1
' Constants for SetWindowPos wFlags Parameter
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED
Private Const SWP_HIDEWINDOW = &H80
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_NOCOPYBITS = &H100
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOOWNERZORDER = &H200
Private Const SWP_NOREDRAW = &H8
Private Const SWP_NOREPOSITION = SWP_NOOWNERZORDER
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_SHOWWINDOW = &H40

Private Sub Form_Activate()
    Dim ret As Long
    Dim FormLeft As Long, FormTop As Long, FormWidth As Long, FormHeight As Long
   
    FormWidth = ScaleX(Form1.Width, vbTwips, vbPixels)
    FormHeight = ScaleY(Form1.Height, vbTwips, vbPixels)
    FormLeft = ScaleX(Form1.left, vbTwips, vbPixels)
    FormTop = ScaleY(Form1.top, vbTwips, vbPixels)
    Unload Form1
    ret = SetWindowPos(Me.hWnd, HWND_TOPMOST, FormLeft, FormTop, FormWidth, FormHeight, SWP_SHOWWINDOW)


End Sub
I did specify it in the question:

"so it doesnt steal the attention away from another program when it is loaded"

How do I do that... so it goes to the exact same position as form2 was.
oops, sorry you are right.  So you want the new form to be in the foreground if the first form is in the foreground, and in the background if the first is in the background, minimized, maximized, etc...  Sorry I misunderstood, but I'll have a go at that.
It's ok :).  Changing the point value to 500.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ret = SetWindowPos(Me.hWnd, HWND_NOTOPMOST, FormLeft, FormTop, FormWidth, FormHeight, SWP_SHOWWINDOW Or SWP_NOOWNERZORDER)

not great but it seems there is nothing better than NOTOPMOST.

thanks!

cachedVB
You're welcome.  There doesn't seem to be a way to get the exact zorder, let alone set it.  If the existing form is not on top, then the new form is created NOTOPMOST, with the flag SWP_NOOWNERZORDER which is supposed to preserve the owner's zorder.
Actually re-reading the MSDN entry, it would probably be better to use the SWP_NOZORDER flag instead.