Methods for hiding the MFC dialog box

Source: Internet
Author: User

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 functions once.
Yes. SDI and MDI take several steps. In this case, the dialog box shows a lot of details hidden in the usage method, which is not required by SDI and MDI.
Showwindow (ncmdshow. 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: (the Windows API responds to the Message wm_in99vdialog)

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 running, the system sends (sendmessage) wm_ncpaint
At this time, the window border of the program should be displayed, but at this time we did not do any display operations, instead, the window is hidden, showwindow (sw_hide) will put the window
The ws_visible attribute is removed and continues execution. The program checks the ws_visible attribute. If no value exists, the window is displayed. Therefore, a wm_ncpaint message is sent. So I
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. Add the code modifystyleex (ws_ex_toolwindow,
Ws_ex_appwindow); changes the extended window style.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.