![]() |
|
QA: How can I close my application by clicking the smart minimize (X) button?
By Joao Paulo Figueira, August 19, 2003.
QuestionWhen I press the smart minimize (X) button the application is minimized instead of beeing closed. How can I handle smart minimize button click in my application to close it? AnswerThe button at the right of the Task Bar (the bar at the top of your Pocket PC) is called the "Smart Minimize Button". It is used to remove your application from the foreground, but keeps it running. If you open the Settings Memory applet, you can see all running applications, and close them if you want. Newcomers to Pocket PC development, and users alike, find this a strange behaviour. After all they are used to close their desktop applications using a similar button, so why shouldn't this one work like that? Microsoft designed this button to work like that to add comfort to the user experience: users shouldn't care about closing their own applications. This is a task for the system. There is a less-known method of closing well behaved Pocket PC applications: Ctrl-Q. If you issue this key combination (possibly using the SIP), your application should shut down. Thankfully, MFC does this for us, so you don't have to bother implementing it. Some of us (and even Microsoft in the SQL CE 2.0 Query Analyzer) have implemented an "Exit" menu option in our applications. The handler simply sends a WM_CLOSE message to the main frame window and we are done. But how do we manage to have the "Smart Minimize Button" close the application for us? We have to hook it. Here's how it works: First, subclass the task bar window. The new window procedure must be looking for WM_LBUTTONUP messages in the rectangle of the button. When you intercept the message, post a WM_CLOSE message to your main frame. You will have to be careful when subclassing the task bar window procedure: you have to make sure that you will un-subclass it when your application loses the focus (deactivated) or when it is closed (essentially the same thing). The Recipe for MFCAll the work you need to do is restricted to the CMainFrame class. First, let's look at the definitions you need to add to the header file: The static variables will store the task bar and main frame's window handles. The WNDPROC will store the task bar's original window proc. Now, go to the implementation file (CPP) and add these declarations: Now, add the following protected methods to CMainFrame: Add an OnActivate handler to the class (WM_ACTIVATE message): Finally, here is the new task bar window proc. Why the WM_MOUSEMOVE? It simulates the user dragging the stylus out of the button, and thereby restoring it to the normal state. And that's it. Related resources:
DiscussDiscuss this article. Here you can write your comments and read comments of other developers. |
|||||||||||||||||||||