the master
01-18-2008, 07:25 AM
Hi. Ive have some code which keeps a window ontop of everything using an API
Public Declare Sub 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)
Public Const HWND_TOPMOST = -1
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_SHOWWINDOW = &H40
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOMOVE = &H2
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
That works fine except i want the window to be ontop of all windows in my app only and allow it to be behind other apps. Im trying to make it work exactly asif i was showing the form modally except i cant show it modally because that stops code execution in the origional window
tHuNd3r
01-18-2008, 08:48 AM
Hmm... there's a way to make it a "Modeless" Window...
Example consists of 2 forms + 1 commandbutton on form2.
'* Form1 code
Private Sub Form_Load()
Me.Show
Form2.Show vbModal
End Sub
'* Form2 code
Private Declare Function ShowWindow Lib "user32" ( _
ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Const GWL_HWNDPARENT = (-8)
Private hwndOldOwner As Long
Private Sub Command1_Click()
Dim frm As Form2
Set frm = New Form2
frm.ShowMe Me.hwnd
End Sub
Public Sub ShowMe(hwndOwner As Long)
ShowWindow Me.hwnd, SW_SHOWNORMAL
hwndOldOwner = SetOwner(Me.hwnd, hwndOwner)
End Sub
Function SetOwner(ByVal hWndToUse, ByVal hWndOfOwner) As Long
SetOwner = SetWindowLong(hWndToUse, GWL_HWNDPARENT, hWndOfOwner)
End Function
Private Sub Form_Unload(Cancel As Integer)
SetOwner Me.hwnd, hwndOldOwner
End Sub
didn't check the syntax... just copied from:
http://www.eggheadcafe.com/forumarchives/VisualBasicwinapi/Nov2005/post24538597.asp
the master
01-18-2008, 09:03 AM
Ive tested it out and it doesnt seem to work. It does do everything it claims to do on that other site but any loops i have running on the main form are paused when the modal form is shown.
One thing i have noticed though is that the timer control will still run when a modal form is displayed and if a loop is called within the timer then the loop will run fine while the modal form is still open. I cant do it this way though because the loop will be running all the time and its not really possible to stop and start it whenever the new form is displayed.
I do still need windows to be always ontop for just my app too for a similar case. Still only ontop of my apps windows though
Flyguy
01-18-2008, 09:14 AM
Have you tried this:
' Form1
Option Explicit
Private Sub Command1_Click()
Form2.Show , Me
End Sub
tHuNd3r
01-18-2008, 09:20 AM
Form2.Show , Me
lol... be simple :)
didnt even pass through my head...
the master
01-18-2008, 09:26 AM
Lol. That was simple. I thought that just set the owner of the form. I didnt know it made the form always appear ontop of the owner. Im already disabling the main form when the second one is shown so if i add ", me" into .show then it should work just how i want.
Thanx:)