Several methods hidden in dialog

zhaozj2021-02-11  201

Several methods hidden in dialog

Author: A Kun (a_kun@etang.com)

Date: 2001/12/15

There are many applications to hide together, these programs are more running as a background program, and I hope that there is no impact on other windows, often display an icon in the tray area. These programs are usually dialog box programs, and dialog boxes are different from SDI, MDI in the process of initialization, and the dialog only needs Domodule or CREATEDIALOG, and the dialog function calls once, SDI, MDI is good A few steps. In this way, the dialog box is hidden in many details, with this step of SDI, MDI as required by SDI, MDI. Therefore, the dialog box wants to hide it, not very straightforward. There are some ways to do this. Let's take a look at several programs.

1. Timer

The most intuitive, and the most helpless method is to use the timer. Since we can't hide it before the dialog starts displaying, then give it a time to make it displayed, after we hide it.

method:

1. Set the timer in the OnInitDialog () function: (Windows API response message WM_INITDIALOG)

Settimer (1, 1, null);

2. Add a message handler ONTIMER to handle WM_TIMER, add code:

IF (nidevent == 1)

{

Deletetimer (1);

ShowWindow (SW_HIDE);

}

The disadvantage of this method is obvious, using the timer, so that the stability of the program seems to play a discount; the window is to be displayed first, then the effect is that the window flashes.

2. Change dialog box display status

Change it when the dialog is initialized to make it hidden. The method is to call the SETWINDOWPLACEMENT function:

Bool cDialoGexdlg :: oninitdialog ()

{

CDIALOG :: OnInitdialog ();

// do something

WindowPlacement WP;

WP.Length = SizeOf (Windowplacement);

WP.FLAGS = WPF_RESTORETOMAXIMIMIZED;

WP.SHOWCMD = sw_hide;

SetwindowPlacement;

Return True;

}

When you need to display (usually a mouse message responsive to the hotkey or pallet icon):

WindowPlacement WP;

WP.Length = SizeOf (Windowplacement);

WP.FLAGS = WPF_RESTORETOMAXIMIMIZED;

WP.SHOWCMD = SW_SHOW;

SetwindowPlacement;

Such an effect is very unsatisfactory: the window is displayed on the top left corner of the screen, and it is only the title bar, to be normal display, but also the following code:

Define a member variable CRECT Rect;

In OnInitDialog ():

GetWindowRect (& Re);

Places you need to display:

SetWindowPos (& Wndnotopmost, WNDRC.LEFT, WNDRC.TOP, WNDRC.RIGHT, WNDRC.BOTTOM, SWP_SHOWWINDOW);

CenterWindow ();

Even if it is, the effect is still very poor.

In this way, there is another drawback that when the program starts running and hides, the original activated window has become an inactive state, and when the dialog is displayed, the dialog is also inactive. 3. Not drawn window

When the dialog is displayed, the client is to draw the client area, and the WM_NCPAINT draws the window border accordingly. We hide the window in the window for the first time, and we can receive a relatively good effect. Since the window is drawn first, we only need to handle WM_NCPAINT. code show as below:

Add a WM_NCPAINT handler.

Void CMYDIALOG :: Onncpaint ()

{

Static int i = 2;

IF (i> 0)

{

I-;

ShowWindow (SW_HIDE);

}

Else

CDIALOG :: OnncPaint ();

}

Here is a problem: Why do you want to define static variables I and set it to 2?

We only need the window to hide the first time, so defined this variable to determine if the window is first displayed. When the program starts running, the system sends a WM_NCPAINT message, the window border of the program should be displayed, but we did not make any displayed operation, but hide the window, showwindow (sw_hide) will put the window WS_Visible The property is removed, continue to execute, the program will check the ws_visible property, if no window is displayed, so a WM_NCPAINT message is sent again. So we have to handle two WM_NCPAINT messages.

When you need a window display, call ShowWindow (SW_SHOW).

The result of the program execution is that the window that is in an active state may flash two times and then still active. This method of processing is much more advantageous than above.

4. Put the dialog as a child window

This method is to use the SDI framework, the main window is hidden, the dialog box as the member variable of the main window, add the following code inside CMAINFRAME :: OnCreate ():

IF (! DLG.CREATE (IDD_MYDIALOG, THIS))

{

Return -1;

}

Dlg.showwindow (sw_hide);

Use dlg.showWindow (sw_show) where the dialog is to be displayed; Note that the main window must be hidden, otherwise the dialog may flash.

Hide status bar window

The method of several checkup dialogs is introduced. If you tried it, you may have noticed that there will be a program's icon flashing in the system status bar, and this is also hidden when you hide the dialog. simple:

In the oninitdialog () function, add ModifyStyleex (WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); In the place where you want to display the window, add code modifystyleex (WS_EX_TOOLWINDOW, WS_EX_APPWINDOW); so that the extension style of the window will be changed back.

The above is my experience summary, where there is a mistake or imperfect place, I hope that everyone will be proposed. Welcome everyone to contact me.

转载请注明原文地址:https://www.9cbs.com/read-3711.html

New Post(0)