Results 1 to 7 of 7

Thread: Window/Form On top most

  1. #1


    Hi all:
    This was on the Genral Form ... but no one answers it... and thought this might be a API question, so... put it here again...
    How can we know which form in on the top most? and how can we minimize it or change the size of it..
    The question was from internet.... as there are so many free internet access with banner.... just wonder how can er "GET" teh form and see if we can "Change" the banner...

  2. #2
    Or is how to get the top most window:

    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    ' Returns the title of the active window.
    '    if GetParent = true then the parent window is
    '                   returned.
    Public Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
       Dim i As Long
       Dim j As Long
       i = GetForegroundWindow
       If ReturnParent Then
          Do While i <> 0
             j = i
             i = GetParent(i)
          i = j
       End If
       GetActiveWindowTitle = GetWindowTitle(i)
    End Function
    Public Function GetWindowTitle(ByVal hwnd As Long) As String
       Dim l As Long
       Dim s As String
       l = GetWindowTextLength(hwnd)
       s = Space(l + 1)
       GetWindowText hwnd, s, l + 1
       GetWindowTitle = Left$(s, l)
    End Function
    MsgBox GetActiveWindowTitle(True)
    And to minimize it, you can use the ShowWindow api function:

    Declare Function ShowWindow Lib "User32" (ByVal Hwnd _
    As Long, ByVal nCmdShow As Long) As Long
    Public Const SW_MINIMIZE = 6
    ShowWindow Hwnd, SW_MINIMIZE

  3. #3
    Add the following to a Form with a Timer and Label.
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    Private Sub Timer1_Timer()
        Dim TOP_HWND As Long
        Dim sName As String * 255
        Dim iLength As Integer
        TOP_HWND = GetForegroundWindow
        iLength = GetWindowText(TOP_HWND, sName, 255)
        Label1 = Left(sName, iLength)
    End Sub
    The Label will change to whatever window is at the top.

  4. #4
    New Member
    Join Date
    Oct 2000

    The Easiest way to do this!

    This is BEST and FASTEST and SHORTEST way to do this!
    Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    Public Const HWND_TOPMOST = -1
    Public Const SWP_SHOWWINDOW = &H40
    Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    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 SetWindowOnTop(frm As Form)
    Dim r As RECT
    GetWindowRect frm.hwnd, r
    SetWindowPos frm.hwnd, HWND_TOPMOST, r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top, SWP_SHOWWINDOW
    End Sub
    SetWindowOnTop Form1 'program code gets Hwnd from form.

    Thank you, thank you

  5. #5


    Matthew Gates:
    I know your codes are good... i try to put the msgbox under a command_click, i tried to log into internet (those with free access and a banner) and i click on our program , the message showed that our program in on top most..... but on the screen... my VB form is cover by the banner... it is clear that the banner is on top most... wonder what i did wrong.. hope you can give a hint

  6. #6
    That's because for the one little moment (second), your program is on top when you click that command button. But there is a solution.


    You can use the GetAsyncKeyState api function to do this.

    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer
    Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyA) Then Command1_Click: Exit Sub
    'If A key is pressed then click Command1
    End Sub
    And doing this, you will see the banner be on top.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    First thing you need to do is catch the banner classname, this isn't part of your prog, just to find out what classname they have so that you can get their handle with findwindow:
    'put all this in a module
    Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Function Classname(hwnd) As String: Dim temp As String * 255
        Classname = left(temp, GetClassName(hwnd, temp, 255))
    End Function
    Then in a timer on the form check for the forground window classname:
    Run it, click the banner, and jot down the classname you get in the caption. then you don't need that anymore, goto your app instead.
    'declare this somewhere, if you want to have it in a module make it public
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    'To get the handle of the banner, use findwindow:
    FindWindow(classname, vbnullchar)
    'where classname is the name you wrote down.
    Good luck!

    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width