Keep form on top

K

Kath

I would like to display a modeless form, so that my app can continue
to run in the background. However, my application can open new forms,
which will appear on top of a the modeless form. I wish to prevent
this from happening, so that when my modeless form opens, it stays on
top until the user signals they have read it (by clicking a button on
the form, which destroys it).

I've found the function 'SetWindowPos', which I'm assuming will work
in the CF (I've changed longs to integers).

Private Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Integer, _
ByVal hWndInsertAfter As Integer, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal cx As Integer, _
ByVal cy As Integer, _
ByVal wFlags As Integer) As Integer

However, in the CF you cannot easily get the handle to the form, but I
found another function to do this - FindWindow.

Public Declare Function FindWindow Lib "coredll.dll" (ByVal
lpClassName As String, ByVal lpWindowName As String) As IntPtr

This works fine, except that it returns the handle as an IntPtr, but
the SetWindowPos function requires the handle as an integer. I've
tried converting the IntPtr to an integer using
System.Convert.ToUInt32(hwnd), but it fails on this line. I've also
tried System.Runtime.InteropServices.Marshal.PtrToStructure(hwnd,
hstruct), but with no luck.

Does anyone know how to convert an IntPtr to an integer, or know of a
good way to keep a form on top?

Thanks,
Katherine
 
P

Peter Foot [MVP]

Change your P/Invoke declaration to receive an IntPtr. IntPtr and Int32 are
both 32 bit integer types and you can cast between them, however you may as
well just change your SetWindowPos function to accept your IntPtr directly.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top