Summary of VC FAQs (III)

Source: Internet
Author: User

1. How to change the window icon?

Send the wm_section message to the window.

Example:

Hicon = afxgetapp ()-> loadicon (idi_icon );

Assert (hicon );

Afxgetmainwnd ()-> sendmessage (wm_section, true, (lparam) hicon );

2. How to change the default window style?

Reload 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 center the window display?

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 can I maximize and minimize windows and MDI windows as soon as they are started?

Let's talk about the window first.

Set the m_ncmdshow value in the initstance function.

M_ncmdshow = sw_showmaxmized; // maximize

M_ncmdshow = sw_showminmized; // minimized

M_ncmdshow = sw_shownormal; // Normal Mode

MDI window:

If you want to create a new application, you can use the advanced button of the MFC Appwizard and check to maximize or minimize it in the MDI child window style group. You can also reload the precreatewindow function of the MDI window, set ws_maxmize or ws_minmize;

If it is derived from cmdichildwnd, call cwnd: showwindow in the oninitialupdate function to specify the style of the MDI child window.

5. How to keep the program in a very small state?

When the Recovery Program form is large, Windows sends a WM_QUERY-OPEN message using classwizard to set the member function onqueryopen (), add following code:

Bool cmainframe: onqueryopen ()

{

Return false;

}

6. How do I limit the window size?

That is, the fixeddialog format. In Windows, wm_getmaxmininfo is sent to track and respond to it. In ongetmaxmininfo, write the following code:

7. How to Make the window invisible?

Use sw_hide to hide the window. You can use findwindow and showwindow to control the window.

8. How to keep the window at the forefront?

There are two ways.

Bringwindowtotop (handle );

Setwindowpos function, which specifies the top style of the window. Use ws_ex_topmost to extend the window style.

Example:

Void toggletopmost (cwnd * pwnd)

{

Assert_valid (pwnd );

Pwnd-> setwindowpos (pwnd-> getstyle () & ws_ex_topmost )?

& Wndnotopmost: & wndtopmost, 0, 0, 0, ssp_nosize | wsp_nomove );

}

 

9. How to move the window by clicking an area other than the window title bar

When the window needs to determine the mouse position, Windows sends the wm_nchittest information to the window. This information can be processed so that Windows considers the mouse over the window title. For dialog box and dialog-based applications, you can use classwizard to process the information and call the basic functions. If the function returns htclient, the cursor is in the room area, the returned htcaption indicates that the mouse is in the title bar of windows.

Uint csampledialog: onnchittest (cpoint point)

{

Uint nhittest = cdialog: onnchittest (point );

Return (nhittest = htclient )? Htcaption: nhittest;

}

The above two technologies have two disadvantages: one is that double-clicking in the customer area of the window will greatly increase the window; the other is that it is not suitable for the main window that contains several windows. Another method is to place the cursor over the title of the window when you press the left mouse button to make the window in the main frame think that the mouse is on the title of the window, use classwizard to process wm_lbuttodown information in the window and send a wm_nclbuttondown message and a click test htcaption message to the main window.

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 technique is also applicable to dialog boxes and peer-based applications, but does not need to call cwnd: getparentframe.

Void csampledialog: onlbuttondown (uint nflags, cpoint point)

{

Cdialog: onlbuttondow (nflags, goint );

// 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 a window

Windows sends a wm_erasebkgnd message to the window to notify the window to erase the background. You can use classwizard to reload the default handler of the message to erase the background (actually painted ), returns true to Prevent Windows from erasing windows.

// Paint area that needs to be erased.

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 that needs to be erased.

Crect reclip;

PDC-> getcilpbox (& rcclip );

// Paint the area.

PDC-> patblt (rcclip. Left, rcclip. Top,

Rcclip. 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

You can call cwnd: setwindowtext to change the title of 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 (_ T ("button New title "));

If you need to modify the title of the window frequently (note: the control is also a window), you should consider using the semi-document function afxsetwindowtext. This function is available in afxpriv. h indicates that in winutil. CPP implementation, it cannot be found in online help, it is in afxpriv. H is half-documented and will be documented in later releases of MFC. Afxsetwindowtext is implemented as follows:

Voik afxapi afxsetwindowtext (hwnd hwndctrl, lpctstr 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 active document name in its description

The fws_addtotitle style bits are usually used to create the primary box window and the MDI child window. If you do not want to add a document name in the description, you must disable this style bits. You can use classwizard to reset the cwnd :: precreatewindow and close the fws_addtotitle style.

Bool cmainframe: precreatewindow (createstruct & CS)

{

// Turn off fws_addtotitle in main frame.

CS. styel & = ~ Fws_addtotitle;

Return cmdiframewnd: precreatewindow (CS );

}

Close the FWS _ addtotitle style of the MDI subwindow to create a window with an empty title. You can call cwnd: setwindowtext to set the title. Remember to follow the interface style guide when setting the title.

 

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

Call cwnd: getcurrentmessage to obtain a MSG pointer. For example, you can use classwizard to map the processing procedures of several menu items to a function, and then call getcurrentmessage to determine the selected menu items.

Viod cmainframe: oncommmonmenuhandler ()

{

// Display selected menu item in debug window.

Trace ("menu item % u was selected./N ",

Getcruuentmessage ()-> wparam );

}

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.