Tuesday, July 8, 2008

How to set window topmost in .net 2003 for CE?

this.BringToFront and this.Focus() do not work for CE. Have to use C++ way of TOPMOST and SetWindowPos(..) as following:

[DllImport("coredll.dll", EntryPoint="GetCapture", SetLastError=true)]
private static extern IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint="SetWindowPos", SetLastError=true)]
private static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
uint uFlags);

public IntPtr GetControlHandle(Control ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
return hWnd;
}



///
/// Set a form to the top most position.
///

/// The form to set topmost.
/// Returns true if successful, false otherwise.
public bool SetWindowTopMost(Form frm)
{
return SetWindowTopMost(frm, true);
}

///
/// Set a form to the top most position.
///

/// The form to set topmost.
/// True to set the form topmost, false to set the form to no topmost.
/// Returns true if successful, false otherwise.
public bool SetWindowTopMost(Form frm, bool bTopMost)
{
// Get the handle to the form entered.
IntPtr hWnd = GetControlHandle(frm);
IntPtr hWndInsertAfter = IntPtr.Zero;

if (bTopMost)
{
// Set the topmost flag.
hWndInsertAfter = new IntPtr(HWND_TOPMOST);
}
else
{
// Set the no topmost flag.
hWndInsertAfter = new IntPtr(HWND_NOTOPMOST);
}

return SetWindowPos(hWnd, hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}

No comments: