Results 1 to 9 of 9

Thread: Keep On Top when Shelled

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Keep On Top when Shelled

    I'm shelling to a VB6 program from Excel. If (when) Excel is clicked on the VB6 Form
    goes into the task bar. Is there anyway I can avoid that and keep the VB6 Form on top
    of Excel regardless, until it's closed?

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Keep On Top when Shelled


  3. #3
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Keep On Top when Shelled

    Code:
    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 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
    
    Call SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
    You can run this on a timer to restore any window to the forefront. See the link below for an implementation.
    http://www.vbforums.com/showthread.p...on-quot-0-quot

    J.A. Coutts

  4. #4
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: Keep On Top when Shelled

    Setting the VB6 Form always on top isn't really the most ideal solution (the Form will remain on top of every other non-topmost window even when Excel is behind other windows or is minimized). What you should do is make the VB6 Form owned by the Excel window. This example shows how you might achieve that (if you don't want the VB6 Form to be modal to the Excel window, simply omit the EnableWindow calls).

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Keep On Top when Shelled

    Quote Originally Posted by Victor Bravo VI View Post
    Setting the VB6 Form always on top isn't really the most ideal solution (the Form will remain on top of every other non-topmost window even when Excel is behind other windows or is minimized). What you should do is make the VB6 Form owned by the Excel window. This example shows how you might achieve that (if you don't want the VB6 Form to be modal to the Excel window, simply omit the EnableWindow calls).
    The hWnd of the C# form is hardcoded??

  6. #6
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: Keep On Top when Shelled

    Quote Originally Posted by Eduardo- View Post
    The hWnd of the C# form is hardcoded??
    Well, it was just a simple example. Obviously, for those who are aware that hWnds are dynamically assigned (the &H12345678 value is a hint that it's just a dummy hWnd), they will need to supply the code that retrieves that other window's hWnd. For those who aren't (and I'm sure you're not one of them), welcome to Windows programming! (where handles like hWnd aren't set in stone)

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Keep On Top when Shelled

    Quote Originally Posted by Victor Bravo VI View Post
    Well, it was just a simple example. Obviously, for those who are aware that hWnds are dynamically assigned (the &H12345678 value is a hint that it's just a dummy hWnd), they will need to supply the code that retrieves that other window's hWnd. For those who aren't (and I'm sure you're not one of them), welcome to Windows programming! (where handles like hWnd aren't set in stone)
    Yes, I know.

    For the OP: here there is code to get the hWnd of the main window of the shelled program.
    That's one, the other one is the hWnd of Excel, perhaps Application.hWnd.

    Those are ment to be used along with the code linked by Victor.

  8. #8
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    706

    Re: Keep On Top when Shelled

    I would shell the application with command line option supplying Excel hWnd, it's less code, and it accounts for the possibility that there might be multiple Excel instances running.

    Shell "C:\VB6App.exe 12345678"

    The VB6 app would use the Command() function to retrieve it. Example:

    Debug.Print Command()

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Keep On Top when Shelled

    One question for the OP: are you going to program in Excel VBA or in the VB6 shelled program?

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