Create a Top-Most form

The following code can be used to force your form to the top of the z-order. Use this functionality with care since it’s bad practice to hog the topmost position which could obscure other important functionality.


private void Form1_Load(object sender, System.EventArgs e)


{


      //get handle


      this.Capture = true;


      IntPtr hwnd = OpenNETCF.Win32.Win32Window.GetCapture();


      this.Capture = false;


 


      //set foreground


      OpenNETCF.Win32.Win32Window.SetWindowPos(hwnd, Win32Window.SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, Win32Window.SetWindowPosFlags.SWP_NOMOVE | Win32Window.SetWindowPosFlags.SWP_NOSIZE | Win32Window.SetWindowPosFlags.SWP_SHOWWINDOW);


 


}


Note that this code cannot be placed in the form constructor, since the native form has yet to be created so a valid window handle won’t be returned. Here the code is placed into the Form_Load method.


In .NETCF 2.0 you can set the TopMost property of your form which removes the need for any of this interop (Thanks Daniel Moth for correcting this).

Advertisements

One thought on “Create a Top-Most form

Comments are closed.