Many applications require them to be hidden when they are moved together. These programs run as backend programs and tend to display only one icon in the tray area if they do not affect other windows. These programs are usually dialog box programs, and the initialization process of the dialog box is different from that of SDI and MDI. In the dialog box, you only need to call domodule, createdialog, and other dialog box functions once, SDI and MDI take several steps. In this case, the dialog box shows a lot of details hidden in the usage method, without the showwindow (ncmdshow) step required by SDI and MDI. Therefore, it is not very straightforward to hide the dialog box if it is to be run on the fly. 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 showwindow (sw_hide) cannot be used to hide it before the dialog box starts to display it, you can give it a time to show it. When it is finished, we can hide 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 needs to be displayed first, so the effect is that 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 has a title bar. to display the window normally, add the following code:
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, and after the dialog box is displayed, the dialog box itself is also inactive.
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. We can hide the window when the window is self-painted for the first time, and we can enjoy good results. Since the window draws the window border first, we 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 to run, the system sends the (sendmessage) wm_ncpaint message. The window border of the program should be displayed, but we did not perform any display operations, but hidden the window, showwindow (sw_hide) will remove the ws_visible attribute of the window and continue the execution. The program will check the ws_visible attribute. If not, the window will be displayed, so a wm_ncpaint message is sent. 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. This 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 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 icon of the program will flash when the program starts in the system status bar, this is also to be hidden when hiding the dialog box. The method is simple:
Add modifystyleex (ws_ex_appwindow, ws_ex_toolwindow); To the oninitdialog () function. Add the code modifystyleex (ws_ex_toolwindow, ws_ex_appwindow) to the window to be displayed, and change the extended window style back.