Hi there!

Following problem:

I have a Smartphone application which has main window which consists of a single (docked) TreeView. I need to perform certain tasks when the application is put in the background. That is when the Escape/Back key (KeyCode 27) is pressed.

However it looks like and TreeView / ListView eats up that KeyCode. I tried a number of things but i simply can't get the escape key event. This is probably because the escape key might also be used to collapse tree nodes.

So how on earth can the application figure out, when it is put in the background when neither the TreeView nor the Form fires the KeyUp event for the Escape/Back key

Do i have to do some dodgy constructs with the OnDeactivate / OnMinimise event
The MainForm is of course also minimised and deactivated when a sub-form is opened. So how to distinguish these kind of OnDeactivate / OnMinimise events from the ones where the application is really sent to the background Sprinkling the code all over the place with a status flag is not really what i want to do.

Is there some code in the event that tells me you get the OnDeactivate / On Minimise because of xyz perhaps

Any help is welcome!


Re: Smart Devices VB and C# Projects How to detect if a Smartphone application is put in the background?!

AM Varela


Verify if the application handle is active. It is a little sample. (C#)

using System.Runtime.InteropServices;

//Return the window handle using the name


private static extern IntPtr FindWindow(string className, string wndName);

static void Main(string[] args)


// When one instance of application it¡¯s running, activate it

IntPtr hwnd = FindWindow(null, "ScrInitialForm");

if (hwnd != IntPtr.Zero)


// Create the message to the handle UI application

Message msgWakeUp = Message.Create(getHandleUI(), (int)ClsConst.WM_PTT_UI_T.WM_PTT_UI_WAKEUP, IntPtr.Zero, IntPtr.Zero);

// Send message using Post Message

MessageWindow.PostMessage(ref msgWakeUp);




Re: Smart Devices VB and C# Projects How to detect if a Smartphone application is put in the background?!


Hi AM Varela!

Thanks for your reply. Maybe i miss the point, but i do not see how your example helps me detecting when my Smartphone Application is put in the background

Your example looks like an "avoid two instances of the same program to run" example. Find the window via PINVOKE. If it's there, send a message to the application. If it's not there... start the application.

So what i need is an event/message... That calls a function in my program to perform some cleanup tasks, before it is put in the background.

Think about a program which is password protected and holds some sensitive data. When the application is put in the background i want to lock it, so when it becomes active again (i could use your example here) it shows an enter password screen or something like that.


Re: Smart Devices VB and C# Projects How to detect if a Smartphone application is put in the background?!


Okay, new idea:

As i do not seem to be able to get the escape/back key event on a Form with a docked TreeView. (KeyPreview does not seem to work; At least not in the emulator).

What about this:

/// <summary>
/// Event handler Tick for mainTimer.
/// </summary>
private void mainTimer_Tick(object sender, EventArgs e)
// We only need to timeout, when a database is open
if (bDatabaseOpen)
// Check if treeView on MainForm has focus
if (!treeView.Focused)
// TODO: Check if child forms have focus
// If not, close database or end application

Start a timer; When it expires check the focus of the treeView and of all child forms...
Problem: How to check if any child form has focus, if the child forms are dynamically created within the subfunctions of the MainForm... Argh... *bang head on desk*

Isn't there a simple pinvoke that can tell me which application has the focus
Maybe somehow in combination with AM Varelas FindWindow PINVOKE
Can i check whether my application has the focus, when FindWindow succeeds


Re: Smart Devices VB and C# Projects How to detect if a Smartphone application is put in the background?!


Ok, after a few more hours of browsing and hacking i came up with this dodgy solution:

* The code within this region was created by using the example from:
* PostID=474529&SiteId=1
* How do I determine which program window is active

private static extern IntPtr GetForegroundWindow();

private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

public static bool IsForeground(string windowName)
IntPtr hWndCurrWindow = FindWindow(null, windowName);
IntPtr hWndActive = GetForegroundWindow();

if ( null != hWndCurrWindow && null != hWndActive )
uint CurrProgramThreadProcessId = 0;
uint ActiveThreadProcessId = 1;

GetWindowThreadProcessId(hWndCurrWindow, out CurrProgramThreadProcessId);
GetWindowThreadProcessId(hWndActive, out ActiveThreadProcessId);

if (KeePassSDThreadProcessId == ActiveThreadProcessId)
return true;

return false;

This function is called in the OnDeactivate event of the MainForm. If it returns false i assume the application was put in the background. If it returns true, then a childwindow triggered the OnDeactivate. What a mess....