Results 1 to 9 of 9

Thread: BringWindowToTop API

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Can anyone explain how to use this?

    I am using the Winsock control and I want my app to pop to the top of the screen if in the background. Is this the right API to use? If so how do I use it?
    Thanks in advance.

    Here is some sample code that I was thinking of using.

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

    If Not Me.hwnd = GetActiveWindow Then
    BringWindowToTop hwnd
    'FlashWindow Me.hwnd, CLng(True)
    End If

    ........

  2. #2
    Guest
    Yes, your code should work perfectly fine.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    BringWindowToTop

    I can't seem to get it to work so I decided to try it with a timer. Here is my code:

    Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
    Private Declare Function GetActiveWindow Lib "user32" () As Long

    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long

    Private Sub Command1_Click()
    Timer1.Enabled = True
    End Sub

    Private Sub Form_GotFocus()
    FlashWindow Me.hwnd, CLng(False)
    End Sub

    Private Sub Timer1_Timer()
    If Not Me.hwnd = GetActiveWindow Then
    BringWindowToTop hwnd
    FlashWindow Me.hwnd, CLng(True)
    GetTopWindow hwnd
    End If
    End Sub

    ------
    my timer is set for 5 seconds. That gives me time to minimize everything. After the 5 seconds I see it on the bottom but nothing pops up. Am I doing something wrong?

    Thanks.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    solution

    For anyone that is interested to what I found out.

    To test it out I have a timer and command button:

    'minimize the window after you click the button.
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long

    Private Sub cmdGo_Click()
    Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Timer()
    FlashWindow hwnd, CLng(True)
    SetForegroundWindow hwnd
    Form1.WindowState = 0
    End Sub

  5. #5
    Guest
    Add the following to a Form with a CommandButton and a Timer.

    When you want the Form to minimize and flash, press the button.
    Code:
    Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
    Private bState As Boolean
    
    Private Sub Command1_Click()
       WindowState = vbMinimized
       DoEvents
       Timer1.Enabled = True
    End Sub
    
    Private Sub Form_Load()
        Timer1.Interval = 250
        Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
        bState = Not bState
        Static icount As Integer
        
        If bState = True Then
            FlashWindow hwnd, 1
        Else
            FlashWindow hwnd, 0
        End If
        
        icount = icount + 1
        If icount = 10 Then Timer1.Enabled = False
    End Sub

  6. #6
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Wink Keep Window On top

    Just use the SetWindowPos API ok?

    Code:
    option explicit
    'Keep onTop
    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 SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    
    Private Sub cmdTriggerOn_Click()
           SetWindowPos Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS
    
    end sub
    
    Private Sub cmdTriggerOff_Click()
          SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    
    end sub
    I think this is what you want.
    Hope it works
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  7. #7
    Addicted Member KrishnaSantosh's Avatar
    Join Date
    Feb 2001
    Location
    Coimbatore
    Posts
    210
    Hi,

    My Email Is : [email protected]

    You can use the more stable and widely used WinAPI function for this. Here Is The 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 SWP_NOSIZE = &H1
    private const SWP_NOMOVE =&H2
    private const HWND_TOPMOST=-1 'Always on top
    private const HWND_NOTOPMOST=-2 'no Always on Top

    public sub SetFormOnTop(myForm as object)
    SetWindowPos myForm.hWnd,HWND_TOPMOST,0,0, _
    0,0,SWP_NOMOVE or SWP_NOSIZE
    End Sub

  8. #8
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Thumbs up RE:: RE::????

    KrishnaSantosh,
    I'm not too sure, but I'm pretty sure. I took a look at both the code you wrote, and the code I wrote, just before you. And they look pretty damn similar, if not the same. What gives?? I'm not angry, but I see stax of ppl, sending useless messages just to get their posts up.




    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  9. #9
    Guest
    When you use SetWindowPos in this respect, you don't need to declare all the constants, because you only need to '3' as the flag. This can shorten your by a lot.
    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 Sub Command1_Click()
        SetWindowPos hWnd, -1, 0, 0, 0, 0, 3
    End Sub

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