Article 1: http://blog.csdn.net/jax_lee/article/details/6771580
But I think this is more direct and clearer ..
Author: A Kun (a_kun@etang.com)
There are many applications that need to be hidden when they are moved together. These programs run as backend programs and do not affect other windows,
Only one icon is displayed in the tray area. These programs are usually dialog box programs, while the dialog box is initialized with SDI
The MDI Initialization is different. In the dialog box, you only need to call the domodule, createdialog, and other dialog box functions once.
Yes. SDI and MDI take several steps. In this case, many details are hidden in the usage of the dialog box.
There is no showwindow (ncmdshow) required by SDI or MDI. Therefore, the dialog box needs to be hidden once it is run.
Directly. There are some ways to do this. Let's take a look at several solutions.
1. Timer
The most intuitive and helpless way is to use a timer. Since we cannot use
Showwindow (sw_hide) to hide it, then give it a time to show it, we are hiding it.
Method:
1. Set the timer in the oninitdialog () function: (wm_initdialog in the Windows API) settimer (1, 1, null );
2. Add the message processing function ontimer for processing wm_timer and add the Code:
If (nidevent = 1)
{
Deletetimer (1 );
Showwindow (sw_hide );
}
The disadvantage of this method is obvious. Using a timer makes the program's stability seem to offer a discount; the window must first display
As shown, the window disappears.
2. Change the display status of the dialog box
You can hide a dialog box by changing its display attribute during initialization. The method is to call the setwindowplacement function:
Bool cdialogexdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
// Do something
Windowplacement WP;
WP. Length = sizeof (windowplacement );
WP. Flags = wpf_restoretomaximized;
WP. showcmd = sw_hide;
Setwindowplacement (& WP );
Return true;
}
When you need to display it (usually the mouse message that responds to the hot key or tray icon ):
Windowplacement WP;
WP. Length = sizeof (windowplacement );
WP. Flags = wpf_restoretomaximized;
WP. showcmd = sw_show;
Setwindowplacement (& WP );
This effect is not ideal: the window is displayed in the upper left corner of the screen, and only the title bar is displayed.
Code below:
Define a member variable crect rect;
In oninitdialog:
Getwindowrect (& rect );
In the desired area:
Setwindowpos (& wndnotopmost, wndrc. Left, wndrc. Top, wndrc. Right, wndrc. Bottom, swp_showwindow );
Centerwindow ();
Even so, the effect is still poor.
Another drawback of this method is that when the program starts to run and is hidden, the original activated window becomes inactive.
When the dialog box is displayed, the dialog box itself is not activated.
3. Do not draw a window
When the dialog box is displayed, the message wm_paint will be returned to draw the customer area, and the corresponding message wm_ncpaint will draw the window border. In
When the window is self-painted for the first time, it hides the window and can receive good results. Because the window is first painted window border, so I
You only need to process wm_ncpaint. The Code is as follows:
Add the wm_ncpaint handler.
Void cmydialog: onncpaint ()
{
Static int I = 2;
If (I> 0)
{
I --;
Showwindow (sw_hide );
}
Else
Cdialog: onncpaint ();
}
Here is a question: why do I need to define static variable I and set its value to 2?
As long as the window is hidden for the first time, we can define this variable to determine whether to display the window for the first time. When the program starts running
The system sends the (sendmessage) wm_ncpaint message. The window border of the program should be displayed, but we do not
For any display operation, the window is hidden. showwindow (sw_hide) removes the ws_visible attribute of the window,
Continue to run the program. The program will check the ws_visible attribute. If not, the window will be displayed, so wm_ncpaint is sent again.
. So we need to process the wm_ncpaint message twice.
Call showwindow (sw_show) when you need to display the window.
The execution result of the program is that the window in the activation status may flash twice and remain in the activation status. In this case
The processing method is much better than the above method.
4. Use the dialog box as a subwindow
This method uses the SDI framework. The main window is always hidden. the dialog box is used as a member variable of the main window. Add the following code in cmainframe: oncreate:
If (! DLG. Create (idd_mydialog, this ))
{
Return-1;
}
DLG. showwindow (sw_hide );
Use DLG. showwindow (sw_show); to display the dialog box. Note that the main window must be hidden; otherwise, the Dialog
The box may flash.
Hide the status bar window
The methods for checking the dialog box are described above. If you have tried it, you may have noticed that the program is started in the system status bar.
The icon of the program will flash when hiding the dialog box. The method is very simple:
Add modifystyleex (ws_ex_appwindow, ws_ex_toolwindow); To the oninitdialog () function. In
Add the code modifystyleex (ws_ex_toolwindow, ws_ex_appwindow) to display the window.
Show style changed back.
The above is a summary of my experience. If there are errors or imperfections, I hope you can correct them. You are welcome to contact me.