Hide dialog box in VC

Source: Internet
Author: User

 
 

 

Bytes ----------------------------------------------------------------------------------------------------
Hide dialog box in VC
Author: Admin Source: www.swxz88.cn Release Date: 0:51:44
Decrease font to increase font size
 
 
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: (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. When initializing the display status of the change dialog box, it is not recommended to use it, and there will be a small bug.

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 (% 26; amp; 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 (% 26; amp; 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 (% 26; amp; rect );

In the desired area:

Setwindowpos (% 26; amp; 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.

The above methods are transferred from the network. I tried it myself and thought the following method is good.

Fill in the following sentence in oninitdialog ():

// Hide the interface

Movewindow (0, 0, 0 );

Showwindow (sw_hide );

Modifystyleex (ws_ex_appwindow, ws_ex_toolwindow );
 
Bytes ----------------------------------------------------------------------------------------------------------------------------
The first step is to declare a response function afx_msg void onncpaint (); used to block the painting dialog box operation. Put this function into the declaration of the DLG class as a member of the DLG class.
Step 2: register the message wm_ncpaint (), that is, add on_wm_ncpaint () between begin_message_map (DLG, cdialog) and end_message_map ().

Step 3: The onncpaint Function Code is as follows:
Void cmydialog: onncpaint ()
{
Static int I = 2;
If (I> 0)
{

I --;
Showwindow (sw_hide );
}
Else
Cdialog: onncpaint ();
}
Onncpaint function explanation: the program needs to define static int I = 2 as long as the window is hidden during initial operation;
Variables can be used to determine whether the window is displayed for the first time. It is defined as 2 because when the program starts running, the system sends the (sendmessage) on_wm_ncpaint message, and the window border of the process sequence should be displayed, but no display operation is performed at this time, instead, the window is hidden. showwindow (sw_hide) removes the ws_visible property of the window and continues to execute. The program checks the ws_visible property. If not, the window is displayed, therefore, an on_wm_ncpaint message is sent. Therefore, we need to process the on_wm_ncpaint message twice. Second, you can also define static int I = 1. If I is defined as 1, you must remove the visible attribute of the window, that is, right-click the attribute in the window and select more styles, remove the check box before visible.

With the above three steps, you can hide the dialog box, but when the program starts, the program icon will flash in the system status bar, which requires some work:

First, add modifystyleex (ws_ex_appwindow, ws_ex_toolwindow) to the oninitdialog () function so that the dialog box is not displayed in the system taskbar.

Second, add modifystyleex (ws_ex_toolwindow, ws_ex_appwindow) to the program to be displayed in the dialog box, so that the dialog box is displayed in the system taskbar.

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.