Wednesday, June 6, 2007

Programmatically Minimize an Application in .NET CF 2.0

I once made a solution that runs on full screen. The solution was written completely in managed code (except for the CE Setup and other small stuff..). Since I took over the screen completely, I don't have access to the (X) button in the upper right corner of the screen. I wanted the application running at all times, but I also wanted the user to be able to get in and out of the application. Since the user won't be able to access the Start button, I added a "Close" button to my application. This "Close" button won't exit the application, instead it will just Minimize the application.

In the .NET Compact Framework 2.0, you can't just set the form's WindowState to WindowState.Minimized since the WindowState enum only contains Normal and Maximized. Currently, the only way you can programmatically minimize an application is by doing a P/Invoke to ShowWindow and passing SW_MINIMIZE to specify how the window will be displayed. It is also required that your Form has the Taskbar visible, this is done by setting the following properties:

  FormBorderStyle = FormBorderStyle.FixedDialog;
  WindowState = FormWindowState.Normal;
  ControlBox = true;
  MinimizeBox = true;
  MaximizeBox = true;

Here's a small code snippet of how to minimize your application

[DllImport("coredll.dll")]
static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_MINIMIZED = 6;

void Minimize() {
  // The Taskbar must be enabled to be able to do a Smart Minimize
  this.FormBorderStyle = FormBorderStyle.FixedDialog;
  this.WindowState = FormWindowState.Normal;
  this.ControlBox = true;
  this.MinimizeBox = true;
  this.MaximizeBox = true;

  // Since there is no WindowState.Minimize, we have to P/Invoke ShowWindow
  ShowWindow(this.Handle, SW_MINIMIZED);
}

24 comments:

Neil C said...

You can just Form.Hide() instead of P/Invoking ShowWindow.

--Neil

Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC

Christian Resma Helle said...

The application won't be visible in the Memory - Running Programs if you call Hide()

Neil C said...

Ah, right. You didn't state that in your requirements :)

Anyways, whether it is listed in Running Programs or not is a moot point. The CF CLR does a mutex check when loading an app. If an instance is already running, the app loader brings the running instance to the foreground.

Christian Resma Helle said...

Yeah, I guess it was my fault that I didn't state the requirements so well. Maybe I should rename the title of this entry to "Programmatically Smart Minimize an Application in .NET CF 2.0".

Anyway, it's actually quite interesting how the application behaves if Form.Hide() was called on the main form. In my experience, I could never get the application back to the foreground, even when I try to re-launch the application. And I couldn't really find it in 'Memory - Running programs' to terminate it. I was just stuck with a heavy .NETCF application running in the background.

So what I do instead is to call ShowWindow() to smart minimize my application, and for bring it back up I handle the Activated event where I set the WindowState to Maximized.

Anonymous said...

I have the same situation... and your solution is working perfect for me :)

Gerardo

Anonymous said...

perfect code snippet! Thanx!

Anonymous said...

Perfect solution, works great!
I was just fighting with correct behaviour od Running Programs with multiple maximized forms... and that was just what I needed! Thanks a lot!

Anonymous said...

its working fine... good solution to run application in backgroung .

Anonymous said...

Thanks, great codesnippet. Now my app "minimizes" automatically on a timer if no user input for 2s. Great for an interactive popup...
//Gunnar

John Haynes said...

Anyone know how to start an app. minimised on Windows Mobile?

Ankzz said...

Use this ShowWindow function with value 6 in InitializeComponent function and the application will start minimized..

Anonymous said...

Doesn't work in initcomponent.

Anonymous said...

Hi,

This code works perfectly. It also works with:

this.FormBorderStyle = FormBorderStyle.None;

At least in Windows Mobile 6.1 VGA.

By the way, though my application appears in the list of minimized application, it has no icon, only its name. Can anyone tell me how to get an icon in there?

Thanks!

Anonymous said...

Hi,

To start minimized app I use a timer and set the tick event to use this Minimize function. Set the timer interval to 1000.
Enable timer in form_load and disable in tick event after Minimize.

To minimize to a tray icon use TrayIcon.cs class

Anonymous said...

Nice code. Till now what i found, there is no other way to programmatically minimize the application. this is the only way i can say.

I have one question:
During installation of the application I have registered it to launch at device boot time. My question, is there any way to get any notification that the app is launched by User or it is auto launched (i.e. Device boot time)?

Thanks
Sital

Nicolas Rafalowski said...

Thanks this was very helpfull

Arjen d'Ailly said...

Hi all,

All nice code. I have another challenge:
I want to use the Form.Hide() method (triggered by an Deactivate event) since it really hides the form so the app can not be closed from the memory or task manager. You probably guessed its behaving like a service :-). I have some code that puts an icon in the task tray and when tapped, it brings back up the form (Form.Show()). This all works fine.

However, I can't find/think of any code that Show()'s the form after relaunching it from the Start > Programs menu. Does anyone have any idea? Trying mutex detection didn't work, so I think that NetCF already has a mutex check done before I get to it in the Main() method in program.cs.

Thanks
Arjen

Christian Resma Helle said...

Hi Arjen,

I had the same issue as yours and that is why I used P/Invoke ShowWindow() to minimize the form, you can use ShowWindow() with a different parameter to show the form.

Hide() and Show() sets the visibility of the window, If you Hide() a window then it won't appear in the running tasks

Anonymous said...

i have similar kind of requirement for windows mobile 6 application. can you help me?

Rocvit said...

This works perfect.
I tried to maximize the form using a phisical button like UP.

Is it possible?

Thanks.

Anonymous said...

Great solution, thanks.
But, would somebody be so kind and tell me how can I get back form minimized by this way?

Thaks in advance.

Christian Resma Helle said...

Since you can't run multiple instances of the same app in Windows Mobile you can just start the program like you would normally do and it would be restored or you can resume it through the phones task manager (Control Panel - Memory - Running Programs)

Anonymous said...

Thank you very much!!!

Robert said...

True to a degree, but I think this is a great conversation starter! We use graphics like this and another related to Bloom's and apps in a workshop we lead.Cell Phone Tracking App Reviews