noidea
noidea

Reputation: 184

how to put winform behind all open programs vb.net

I'm calling a vb.net exe from a program in java, and i want it to stay behind all of the already opened programs.

I read that you can kinda bring to front with TopMost set to true , but i find no way to put it in the back.

There's some workarounds, like setvisible=false the previous form and setting to true after that, but that isn't quite good with my application. Thank you!

Upvotes: 1

Views: 261

Answers (1)

RoyalPotato
RoyalPotato

Reputation: 525

Calling the form's SendToBack (Inherited from Control) method from the Shown event handler is the key. Calling it from load for example, won't do anything since the window isn't "ready" yet anyway.

Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    SendToBack()
End Sub

Alternatively, use the SetWindowPos native function as defined in User32.dll.

Imports System.Runtime.InteropServices

...

'Signature source: http://pinvoke.net/default.aspx/user32/SetWindowPos.html
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function

...

Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    SetWindowPos(Handle, New IntPtr(1), Location.X, Location.Y, Width, Height, 0)
End Sub

The only thing I changed from the signature provided by PInvoke.net was that I changed the "uFlags" parameter type from "SetWindowPosFlags" (Also defined on the PInvoke.net) to an Integer.

Upvotes: 2

Related Questions