How do I make a VB.Net (Visual Studio 2010) form topmost to a specific parent window (an Hwnd)?
In VB.Net, Form.TopMost makes a form topmost to every window on the desktop.
I need my form to be topmost to only Excel as the form is part of an Excel COM Addin.
What's the best way to do this in .NET?
I will be targeting x64 once Office 2010 is released and I'm not sure if I can continue to use the 32-bit SetWindowLong API call as I used to do in VB6 as follows:
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
'frm is a Form with a Public ParentWindow as Long property.
'xlApp contains a reference to Excel.Application
Public Sub AlwaysOnTopOfExcel (frm as Object, bAlwaysOnTopOfExcel as Boolean)
If bAlwaysOnTopOfExcel Then
' set parent to xlApp.Hwnd and save current
frm.ParentWindow = SetWindowLong(frm.hwnd, GWW_HWNDPARENT, xlApp.Hwnd)
If frm.ParentWindow <> 0 Then
' restore original parent
Call SetWindowLong(frm.hwnd, GWW_HWNDPARENT, frm.ParentWindow)
This is related to another question that I asked about convernting an Hwnd to an IWin32Window.
I can make frm.TopMost do what I want if I set its parent window when I show it with frm.Show(Win32Handle) but I can't figure out how to get the form on top of Excel after it has been shown. I need to toggle the state of always on top based on an "Always On Top" checkbox that resides on the form itself.