How to make a program always on top with a click on a command button?
Printable View
How to make a program always on top with a click on a command button?
Here is a quick sample:
VB Code:
Option Explicit Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOSIZE = &H1 Const SWP_NOMOVE = &H2 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 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 Command1_Click() SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW _ Or SWP_NOMOVE Or SWP_NOSIZE End Sub
VB Code:
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 Private Const HWND_TOPMOST = -1 Private Const HWND_NOTOPMOST = -2 Private Const SWP_NOMOVE = 2 Private Const SWP_NOSIZE = 1 Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE Private Sub Form_Load() SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS 'To "turn off" always on top use 'SetWindowPos Me.Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS End Sub
ok i try
well how to make it not on top? i want to turn it on and off
use the constants provided:
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
I dont aunderstand how to bind that to a command button?
The same way the other const was used but don't forget to declare it first:
VB Code:
Private Sub Command2_Click() SetWindowPos Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW _ Or SWP_NOMOVE Or SWP_NOSIZE End Sub
whit this script my whole application disappear :(
Quote:
Originally Posted by [LGS]Static
did you read this post?
Yeah i did and now the turn on thing works but now it dissapears when i try the switch of button i have made it
VB Code:
Private Sub Command2_Click() SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS End Sub
Because of this right here:
Don't use HWND_TOPMOST again. That's a NoNoQuote:
Originally Posted by n00b scripter
Heck knows what you're doing, though ... ;)
The following sample simply toggles between "regular" and "topmost" modes - all you need is ONE command button so just copy and paste this code:
VB Code:
Option Explicit Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOSIZE = &H1 Const SWP_NOMOVE = &H2 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 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 Command1_Click() '============================ Static blnTopMost As Boolean Dim lngPos As Long blnTopMost = Not blnTopMost If blnTopMost Then lngPos = HWND_TOPMOST Command1.Caption = "Make Regular Window" Else lngPos = HWND_NOTOPMOST Command1.Caption = "Make Topmost Window" End If SetWindowPos Me.hWnd, lngPos, 0, 0, 0, 0, _ SWP_NOACTIVATE Or SWP_SHOWWINDOW Or _ SWP_NOMOVE Or SWP_NOSIZE End Sub Private Sub Form_Load() Command1.Caption = "Make Topmost Window" End Sub
Whata... that script made the application fill out the screen!
If you're talking about RhinoBull's script, it worked just fine for me ?????Quote:
Originally Posted by n00b scripter
hmm weird
Post your project maybe someone can see if there is something besides your code
Ok here it is. I dont think you will understand whats it is for :p
Can any one help me?
Your "problem" noob is setting form's windowstate to maximized, so simply comment it (as shown below):
VB Code:
Private Sub Command1_Click() 'InitializeRes '''[b]Me.WindowState = 2[/b] ...
Ty so much for helping me and excuse me for my noobness
Not at all, are you kidding ... Enjoy it. :p ;) :wave: