CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2001
    Location
    Tennessee
    Posts
    33

    On top of things

    Is there any code to make a form always appear on top of other programs running (ex. Program A is on top of Internet Explorer even after you click Explorer).


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: On top of things

    You can use the SetWindowPos API to do this, paste this in a module. Then you can call it from all over the place.

    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
    'constants for SetWindowPos API
    private Const SWP_NOMOVE = 2
    private Const SWP_NOSIZE = 1
    private Const HWND_TOPMOST = -1
    private Const HWND_NOTOPMOST = -2

    public Sub SetOnTop(byval hwnd as Long, byval bSetOnTop as Boolean)

    Dim lR as Long
    If bSetOnTop then
    lR = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or _SWP_NOSIZE)
    else
    lR = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or _SWP_NOSIZE)
    End If

    End Sub

    ' Somewhere else, like in the form load event
    private Sub Form_Load()

    SetOnTop me.Hwnd, true

    End Sub




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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

Featured