VC Common Getting Started Summary (3)

zhaozj2021-02-16  54

1. How to change the icon of the window?

Send a WM_SECTION message to the window.

EXAMPLE:

HiCon Hicon = AFXGetApp () -> Loadicon (IDI_ICON);

Assert (hic);

AFXGETMAINWND () -> SendMessage (WM_SECTION, TRUE, (LPARAM) HICON;

2. How to change the default style of the window?

Overload CWnd :: PrecreateWindow and modify the CreateStruct structure to specify the window style and other creation information.

Example: delete "max" Button and set Original Window's Position andSize

Bool CMAINFRAME :: PrecreateWindow (CreateStruct & Cs)

{

CS.Style & = ~ WS_MAXINIZEMOX;

Cs.x = cs.y = 0;

Cs.cx = getSystemMetrics (SM_CXSCREEN / 2);

Cs.cy = getSystemMetrics (SM_CYSCREEN / 2);

Return CmdiframeWnd :: PrecreateWindow (CS);

}

3. How to display the window in the window?

Call Function CWnd :: Center Windows

Example (1): center window (); // relative to it's parent

// Relative to Screen

Example (2): center window (cWnd :: getDesktopWindow ());

// Relative to Application's Mainwindow

AFXGETMAINWND () -> center window ();

4. How to maximize and minimize windows and MDI windows?

Let's talk about the window first.

Set the value of m_ncmdshow in the initstance function.

m_ncmdshow = sw_showmaxmized; // Maximize

m_ncmdshow = sw_showminmized; // Minimize

m_ncmdshow = sw_shownormal; // Normal mode

MDI window:

If you are creating a new application, you can use the MFC AppWizard's Advanced button and maximize or minimize the MDI sub-window style group; you can also overload the MDI Window's PrecreateWindow function, set WS_MAXMIZE or WS_MINMIZE;

If you are derived from cmdichildwnd, call CWnd :: ShowWindow in the OnInitialUpdate function to specify the style of MDI Child Window.

5. How do I keep the program so small?

When the recovery program form is large, Windows will send a WM_QUERY-OPEN message to set the member function onQueryOpen () () () () () () (), add following code:

Bool cmainframe :: ONQUERYOPEN ()

{

Return False;

}

6. How to limit the size of the window?

That is, FixedDialog form. Windows sends a WM_GETMAXMINFO message to track, in response to it, write code in ONGETMAXMINFO:

7. How to make the window are invisible?

Hide the window with SW_HIDE, you can combine FindWindow, showWindow control.

8. How to make the window always in the fore?

Two ways.

Bringwindowtotop (Handle);

SetWindowPos function, the top style of the specified window, using the WS_EX_TOPMOST Extended Window Style Example:

Void ToggletopMost (CWND * PWND)

{

Assert_Valid (PWND);

PWnd -> SetwindowPos (PWND-> getStyle () & WS_EX_TOPMOST?

& WndnotopMost: & Wndtopmost, 0,0,0,0, ssp_nosize | wsp_nomove;

}

9. How to click the area other than the window title bar to move the window

When the window needs to determine the mouse position, Windows sends WM_NCHITTEST information to the window, which can handle this information to make Windows think the mouse is on the window title. For dialogs and dialog-based applications, you can use ClassWizard to process this information and call the base class function. If the function returns htclient, it indicates that the mouse is in the room area, and returns htcaption to indicate the mouse in the title bar of the Windows.

UINT CSAMPLEDIALOG:: Onnchittest (cpoint point)

{

UINT NHITTEST = CDIALOG:: Onnchittest (Point);

Return (nhittest = = htclient)? HTCAPTION: NHITTEST;

}

The above techniques have two points of adverse, one of which is a large window when the window's customer area is double-hit; two, it is not suitable for the main frame window of several windows. There is also a method, when the user presses the left mouse button to consider the mouse on its window title, use ClassWizard to handle WM_LBUTTODOWN information in the window and send a WM_NCLBUTTONDOWN information to the main box window and a click to test HTCAption.

Void Csampleview:: OnlButtondown (uint nflags, cpoint point)

{

CView:: Onlbuttondow (NFLAGS, PONT);

// Fool Frame Window Into Thinking Somene Clicked ON

ITS Caption Bar.

GetParentFrame () -> PostMessage

WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM (Poitn .x, Point .y);

}

This technology also applies to dialogs and pair-based applications, just do not have to call CWnd:: getParentFrame.

Void Csampledialog:: OnlButtondown (uint nflags, cpoint point)

{

CDIALOG:: ONLBUTTONDOW (NFLAGS, GOIN);

// Fool Dialog Into Thinking Simeone Clicked on Its Caption Bar.

Postmessage (WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARM (POINT.X, POINT. Y))

}

10. How to change the background color of the window

Windows Send a WM_ERASEBKGND message to the window Notifies the window erase background, you can use ClassWizard to overload the default handler of the message to erase the background (actually painting) and return TRUE to prevent Windows erase windows.

// Paint area there. Needs to be eRerated.

Bool Csampleview:: OneRaseBkGnd (CDC * PDC)

{

// CREATE A Pruple Brush.

Cbrush Brush (RGB (128, 0, 128)); // Select The Brush INTO The Device Context.

CBRUSH * POLDBRUSH = PDC-> SELCETOBJECT (& Brush);

// Get the area there. Needs to be eRerated.

CRECT RECLIP;

PDC-> GetCILPBOX (& RCLIP);

// Paint the area.

PDC-> Patblt (rcclip.Left, RcClip.top,

Rclip.width (), rcclip.height (), patcopy;

// Unselect Brush Out of Device Context.

PDC-> SELECTOBJECT (POLDBRUSH);

// Return Nonzero to Half Fruther Processing.

Return True;

}

11. How to change the window title

Call CWnd:: SetWindowText can change any window (including controls).

// set Title for Application's Main Frame WINDOW.

AFXGETMAINWND () -> setWindowText (_t ("Application Title");

// set Title for View's MDi Child Frame Window.

GetParentFrame () -> setWindowText ("_T ("MDi Child Frame New Title);

// set Title for Dialog's Push Button Control.

GetDigItem (IDC_Button) -> setWindowText ("Button New Title");

If you need to modify the title of the window (Note: The control is also a window), you should consider using the half-text files of Functions AFXSetWindowText. This function is illustrated in AfxPriv.h, implemented in Winutil.cpp, can't find it in online help, it is in the middle of AFXPriv.h, and documentation in the MFC issued later. The implementation of AfxSetWindowText is as follows:

VOIK AFXAPI AFXSETWINDOWTEXT (HWND HWNDCTRL, LPCTSTSTR IPSZNEW)

{

ITN nnewlen = istrlen (iPAZNew);

Tchar Szold [256];

// Fast Check to See if text really changes (Reduces Flash In the Controls)

IF (nnewlen> _contof (szold) ||

:: GetWindowText (Hwndcrtl, Szold, _countof (Szold)! = Nnewlen ||

Istrcmp (szold, ipsznew)! = 0

{

// Change IT

:: SetwindowText (HWndCtrl, Ipsznew);

}

}

11. How to prevent the main box window from displaying the document name in its description

Creating the main box window and the MDI sub-window to usually have a fws_addtotitle-style bit, if you do not want to automatically add a document name in the description, you must prohibit this style, you can use the ClassWizard to reset CWnd:: PrecreateWindow and close the fws_addtotitle style. Bool CMAINFRAME:: PrecreateWindow (CreateStruct & Cs)

{

// Turn Off FWS_ADDTTTITITE IN MAIN FRAME.

CS.Styel & = ~ fws_addtotitle;

Return CmdiframeWnd:: PrecreateWindow (CS);

}

Closing the FWS _Addtotitle style of the MDI sub-window creates a window with a empty title, you can call CWnd:: SetWindowText to set the title. Remember to set the title guide when you set your title.

12. How to get information about the current message being processed by the window

Call CWnd:: getCurrentMessage can get an MSG pointer. For example, you can use ClassWizard to map several menu items to a function and then call getCurrentMessage to determine the selected menu item.

VIOD CMAINFRAME:: OncommmonMenuHandler ()

{

// Display Selected Menu Item in Debug WINDOW.

"" Menu Item% u was selected. / N ",

GetCruuentMessage () -> wparam);

}

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

New Post(0)