Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Z-Order AlwaysOnBottom

104| Sun, 02 Dec 2007 03:02:00 GMT| yrwyddfa| Comments (2)
Has anyone ever written a function that ensures that a form is always on the bottom of the Z-Order stack?

Keywords & Tags: z-order, alwaysonbottom, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/560831/
 
«« Prev - Next »» 2 helpful answers below.
This is the only way I could figure out how to do it:

Option Explicit

Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Const HWND_BOTTOM = 1

Private 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)

Private Sub Form_Paint()
SetWindowPos Me.hWnd, HWND_BOTTOM, 0, 0, 0, 0, _
SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

Maybe a little tacky, but it works.

thehobo | Mon, 03 Dec 2007 21:22:00 GMT |

if you want to get fancy, subclass. this will of course be way better than uing the form's paint.

put this in a module:

Option Explicit

Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Const HWND_BOTTOM = 1

Private 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)

'in module
Declare Function SetWindowLong& Lib "User32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GWL_WNDPROC = (-4)
'These are the messages

Const WM_NCPAINT = &H85
Global WndProcOld As Long

Public Function WindProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'checks the messages
If wMsg = WM_NCPAINT Then
SetWindowPos Form1.hWnd, HWND_BOTTOM, 0, 0, 0, 0, _
SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End If
WindProc = CallWindowProc(WndProcOld&, hWnd&, wMsg&, wParam&, lParam&)
End Function

Sub SubClassWnd(hWnd As Long)
WndProcOld& = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindProc)
End Sub

Sub UnSubclassWnd(hWnd As Long)
SetWindowLong hWnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub

put this in a form.

Private Sub Form_Load()
SubClassWnd hWnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hWnd
End Sub

MOST IMPORTANT THING:!!!!!!!!!!!!!!!!!! do not use the Stop button in the IDE or use End to end your program when subclassing, because it will crash your program, and your IDE if you are debugging it. you will lose your work if you didn't save it. you should be turning on he option that asks you everytime you run if ou want to save it.

you also can't use the PAUSE button, or use the STOP command (which does the same thing as the pause button, except it does it by code, sorta like a breakpoint)

buggyprogrammer | Mon, 03 Dec 2007 21:23:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories