It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

form always on top

Moderator: dafunkymunky

form always on top

Postby El Don on Fri Mar 06, 2009 9:28 pm

Hi, I'm working on a program to wish my girlfriend a happy birthday, my idea is to show a label like a notification, similar to the one used in messenger, but I want it to be on top of whatever she is working on the computer at the time, even games (she's a hardcore gamer). Is this possible??

what I'm asking for is a form on top of everything, not stealing focus, and if she presses a key, for example "q", the message activates no matter what she is doing. I'm a beginner, and I know how to do the label, the key press, but I don't know how to do the above mentioned, if anyone can help me, I'll be very grateful.

Thanks in advance.
El Don
 
Posts: 2
Joined: Thu Mar 05, 2009 7:21 pm

Re: form always on top

Postby Keithuk on Sat Jul 11, 2009 1:27 am

El Don wrote:
what I'm asking for is a form on top of everything, not stealing focus, and if she presses a key, for example "q", the message activates no matter what she is doing. I'm a beginner, and I know how to do the label, the key press, but I don't know how to do the above mentioned, if anyone can help me, I'll be very grateful.


Well to make a Form stay on top of other Forms you need to use a bit of API coding. It maybe easier to put this code into a Module then it can be called from any Form.
Code: Select all
Option Explicit

Public 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

Public Sub FormOnTop(Handle As Long, OnTop As Boolean)

Dim wFlags As Long
Dim PosFlag As Long

Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
Const SWP_SHOWWINDOW = &H40
Const SWP_NOACTIVATE = &H10
Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1

wFlags = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOACTIVATE

Select Case OnTop
    Case True
        PosFlag = HWND_TOPMOST
    Case False
        PosFlag = HWND_NOTOPMOST
End Select
SetWindowPos Handle, PosFlag, 0, 0, 0, 0, wFlags

End Sub


Now which ever Form you want to stay on top you just add this line into the Form_Load()
Code: Select all

Private Sub Form_Load()

Call FormOnTop(Me.hwnd, True)

End Sub


True is on top and False is not. :wink:
User avatar
Keithuk
 
Posts: 11
Joined: Fri Jun 01, 2007 11:16 am
Location: Staffordshire, England


Who is online

Users browsing this forum: No registered users and 4 guests