Several methods of MFC Hide main window running

Source: Internet
Author: User
There are many applications that require moving together to hide, these programs run as a background program, hope that does not affect other windows, often only in the tray display an icon. These programs are usually dialog box program, and dialog box in the process of initialization and SDI, MDI initialization is different, dialog box only need Domodule or createdialog and so on dialog function call once, SDI, MDI is a few steps. In this case, the dialog box in the use of the method is hidden a lot of details, there is no SDI, MDI required ShowWindow (ncmdshow) This step. Therefore, it is not straightforward to hide the dialog box as soon as it runs. There are some ways to do this, let's take a look at several scenarios.
1. The most intuitive timer, but also the most helpless one way is to use the timer.   Now that we're alone with ShowWindow (Sw_hide) before the dialog starts to show up, it's time to let it show, and we're hiding it.   Method: (1). Set the timer in the OnInitDialog () function: (Inside the WINDOWS API Response message Wm_in99vdialog) SetTimer (1, 1, NULL); (2). Add the message handler function OnTimer that handles Wm_timer, add code: if (nidevent = = 1)
{
Deletetimer (1);
ShowWindow (Sw_hide);
The disadvantage of this method is obvious, the use of the timer, so that the stability of the program seems to be a discount; the window is to be displayed first, then the effect is the window flashed away.
2. Changing the display status of a dialog box changes its display properties when the dialog is initialized to hide it. method is to call the SetWindowPlacement function: Code 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 (usually a mouse message in response to a hotkey 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 is only the title bar, to normal display, but also the following code: Define a member variable CRect rect; Inside OnInitDialog (): GetWindowRect (%; amp;rect);


Where it needs to be shown:
SetWindowPos (%; amp;wndnotopmost, Wndrc.left, Wndrc.top, Wndrc.right, Wndrc.bottom, Swp_showwindow);
CenterWindow ();


Even so, the effect is still poor.   Another drawback of this approach is that when the program starts running and hides, the original activated window becomes inactive, and when the dialog box is displayed, the dialog itself is inactive. 3. Do not draw the window when the dialog box is displayed to respond to the message WM_PAINT draw the client area, the corresponding message Wm_ncpaint draw the window border. We can get a good result when the window is hidden for the first time since it was painted itself. Since the window is drawn with a window border, we only need to deal with Wm_ncpaint. The code is as follows: Add Wm_ncpaint handler function.
void Cmydialog::onncpaint ()
{
static int i = 2;
if (i > 0)
{
I--;
ShowWindow (Sw_hide);
}
Else
Cdialog::onncpaint ();
}


Here's a question: Why define a static variable I and set its value to 2. As long as we hide the window for the first time, we define this variable to determine if the window is displayed 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 at this point we do not do any display operation, but instead of hiding the window, ShowWindow (sw_hide) will be the window Ws_ The Visible property is removed, the execution continues, the program checks the Ws_visible property, and if not, the window is displayed, so a wm_ncpaint message is sent.   So we have to deal with the wm_ncpaint message two times.   Call ShowWindow (sw_show) When a window display is required. The result of the program execution is that the original active window may blink twice and then remain active.   This approach is much better than the way it is done above. 4. Using the dialog as a sub-window This method uses the SDI framework, the main window is always hidden, and the dialog box is the member variable of the main window, and the code is added to the CMainFrame::OnCreate ():
if (! dlg.   Create (idd_mydialog,  this))
{
return  –1;
}
Dlg. ShowWindow (Sw_hide);


Use DLG in the place where you want the dialog box to appear. ShowWindow (Sw_show);   Note that the main window must be hidden, otherwise the dialog box may flash. Hide the status bar window above describes a few ways to check the dialog box, if you have tried, you may have noticed that the system status bar in the program when you start the program icon Flash, in the Hidden dialog box This is to be hidden, the method is simple: in the OnInitDialog () function, add ModifyStyleEx (Ws_ex_appwindow, Ws_ex_toolwindow);   Add code ModifyStyleEx (Ws_ex_toolwindow, Ws_ex_appwindow) where you want the window to appear, and change the extended style of the window back. All the above methods are transferred from the network. I did my own experiment and thought the following method was good. Fill in the following sentences in OnInitDialog ()
Hide interface
MoveWindow (0, 0, 0, 0);
ShowWindow (Sw_hide);
ModifyStyleEx (Ws_ex_appwindow, Ws_ex_toolwindow);


5. Set the window to 0 pixels and move the coordinates to the corner of the screen

Hide interface
I nt Nfullwidth = GetSystemMetrics (Sm_cxscreen);
int nfullheight = GetSystemMetrics (Sm_cyscreen);
SetWindowPos (NULL, Nfullwidth, nfullheight, 0, 0, Swp_nozorder); Set 0 pixels, move to the corner or: MoveWindow (0,0,0,0);
ShowWindow (Sw_hide);
ModifyStyleEx (Ws_ex_appwindow, Ws_ex_toolwindow); Remove the taskbar icon display



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.