VC programming experience summary (1)

zhaozj2021-02-16  55

1. The window maximizes, minimizes the implementation

When we cannot maximize, minimize and recover buttons in the title bar, you need to implement these features in other places, you can add:

WINDOWPLACEMENT WNDPL;

Windowplacement * PWNDPL;

PWNDPL = & WNDPL;

GetWindowPlacement (PWNDPL);

PWNDPL-> showcmd = sw_showminmized; // Implement the window minimization

SetwindowPlacement (PWNDPL);

Where the getWindowplacement () function gets the structure variable pointer of the Structure WindowPlacement of the current window layout, the structure of Windowplacement is defined as:

Typedef struct tagwindowplacement {

Uint Length;

Uint flags;

Uint showcmd;

Point PtminPosition;

Point ptmaxposition;

Rect rcNORMALPSITION;

WINDOWPLACEMENT;

The member variable showcmd determines the state of the current window, and the value is generally:

· SW_HIDE: Hide Window

· SW_MINIMIZE: Minimize the specified window

· SW_RESTORE: Restore the original size

· SW_SHOW: Activated and displayed in the original size

· SW_SHOWMAXIMIZED: Activate and maximize windows

The setWindowPlacement () function is to display the window according to the settings of WindowPlacement.

2. Why use a getsafehwnd () function

When we want to get a window object (CWnd derived object) pointer's handle (HWnd), the safest way is to use the getsafehwnd () function, to see the reason by the following example:

CWND * PWND = FindWindow ("ExplorewClass", NULL; // I hope to find the resource manager

HWND HWND = PWND-> m_hwnd; // Get it HWnd

This code will appear "General Protection Error" when the PWND started is empty, and closes the application because it is generally not accessing a NULL pointer, if using the following code:

CWND * PWND = FindWindow ("ExplorewClass", NULL; // I hope to find the resource manager

HWND HWND = PWND-> getsafehwnd (); // Get it HWnd

There will be no problem, because although when the PWND is NULL, getSafehWnd is still available, just returns NULL, and cleares the code through getSafehWnd (). Clear:

_AFXWIN_INLINE HWND CWND :: GetsafehWnd () Const

{

Return this == NULL? NULL: M_HWND;

}

3. How to make the program in a very small state

If we don't want the program's window to be seen by others, you can keep it in a very small state: When the recovery program window, Window sends a WM_QUERYOPEN message as long as false is returned in its message handler.

Bool cmainframe :: ONQUERYOPEN ()

{

Return False;

}

4. How to prohibit and use the shutdown button

CMenu * pmenu = AFXGETMAINWND () -> GetSystemMenu (false);

IF (PMenu)

{

PMenu-> EnableMenuItem (sc_close, mf_bycommand | mf_grayed);

}

Simply change MF_GRAYED to MF_ENABLED when recovering

5. How to delay in the program

method one:

Use the SLEEP function, such as delay 2 seconds, with SLEEP (2000);

Method Two:

Using the SLEEP function is unfavorable when delayed periods cannot handle other messages, if time is too long, it is like a crash, which does not have such problems with the COLEDATETIME class and the COLEDATETIMESPAN class:

COLEDATETIME Start_time = COLEDATETIME :: getcurrenttime ();

COLEDATETIMESPAN END_TIME = COLEDATETIME :: getcurrenttime () - start_time;

While (end_time.gettotalseconds () <= 2)

{

MSG msg;

GetMessage (& MSG, NULL, 0, 0);

PretranslateMessage (& MSG);

End_time = COLEDATETIME :: getcurrenttime-start_time;

}

In this way, we can also handle other messages when delayed.

I have seen some summary articles in the forum very well. I have written a little out of it. I don't know if there is any repetition. I hope I can have some micro role.

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

New Post(0)