Rise my winmo form, be above everything

image

DISCLAIMER: This was tested on Windows Mobile Standard (no-touch), not WinMo Professional (touch screen).  It *should* work still.

I’m creating a small application that will get me out of countless situations I don’t want to be in.  I had one key issue with Windows Mobile that I had to overcome.  The password screen, lock screen, whatever you want to call it.  Here is a screen shot so we’re all on the same page.

Now why would someone need to pop above this screen?  Well, if your application sleeps or does work in the background while other things happen.  Now due to corporate policy, my phone is forced to be locked with a password.  Prior to this, I would still have my phone locked if it wasn’t being used automatically to help prevent pocket dial.

This involves a few pinvoke calls to accomplish this.  I also suggest wrapping this into its own class and let other forms inherit from it.

If you want a quick download, here is the zip for the top most form that will appear above a lock screen.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace pNerd.Core.Mobile
{
	public class TopMostForm : Form
	{
		private const int GWL_STYLE = -20;
		private const int WS_EX_ABOVESTARTUP = 0x20000000;
		private const int WS_EX_TOPMOST = 0x00000008;

		[DllImport("coredll.dll", SetLastError = true)]
		private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
		[DllImport("coredll.dll", SetLastError = true)]
		private static extern bool UpdateWindow(IntPtr hWnd);
		[DllImport("coredll.dll", SetLastError = true)]
		private static extern bool IsWindow(IntPtr hWnd);
		[DllImport("coredll.dll", SetLastError = true)]
		private static extern bool SetForegroundWindow(IntPtr hWnd);

		public TopMostForm()
		{
            hWnd = Handle;
			myTimer = new System.Threading.Timer(FocusCallback, 1, 0, 100);
			TopMost = true;
		}


		readonly System.Threading.Timer myTimer;
		readonly IntPtr hWnd = (IntPtr)0;

		private void FocusCallback(object state)
		{
			if (!IsWindow(hWnd))
				return;

			SetWindowLong(hWnd, GWL_STYLE, WS_EX_ABOVESTARTUP | WS_EX_TOPMOST);
			SetForegroundWindow(hWnd);
			UpdateWindow(hWnd);

			// destroy timer
			myTimer.Dispose();
		}
	}
}

Since we need the window’s handle, we’ll store that in a global variable.  We’ll use a threading timer to call a function right after the form’s construction to do the rest.  First we’ll verify the handle is an actual window.  Then we’ll call a pinvoke that will set some windows styling to be the topmost and force it to start above.  Then we’ll force it to be the focused window.  To finish it up, we’ll update the window and then destroy the timer to save processing power and increase battery life.

If you don’t understand what I was talking about with inheriting the form you want to be topmost, do this.

Original code:

public partial class YourForm : Form
{ /* code */ }

Changed to this:

public partial class YourForm : TopMostForm // use to be Form
{ /* code */ }

Download: TopMostForm.zip on PeaceLoveCode.com.

Lee Sep 8, 2009 @ 7:40 AM

# re: Rise my winmo form, be above everything
I LOVE U, thanks a lot

Mikael Sep 5, 2013 @ 5:28 AM

# re: Rise my winmo form, be above everything
The code looks really useful for a noob developer, like me. I will download the source of it and I will test it on my server.

Post a Comment

Please add 7 and 2 and type the answer here: