21. describes how to process messages when a function exits with any key.
1. Set the timer to make the: getmessage (...) function always get the message quickly.
2. Add:
After each step of the function execution, execute the following code.
If (: getmessage (& MSG, 0, 0, 0 ))
{
If (msg. Message> = wm_keyfirst & MSG. Message <= wm_keylast) return;
: Translatemessage (& MSG );
: Dispatchmessage (& MSG );
}
Else: postquitmessage (0 );
22. How to hide the toolbar
Add the following two functions:
Hide:
Void cmainframe: onhide ()
{
If (m_wndtoolbar.iswindowvisible ())
M_wndtoolbar.modifystyle (ws_visible, 0 );
Sendmessage (wm_size );
}
Display:
Void cmainframe: onshow ()
{
If (! M_wndtoolbar.iswindowvisible ())
M_wndtoolbar.modifystyle (0, ws_visible );
Sendmessage (wm_size );
}
23. How can I dynamically obtain the toolbar pointer and add a title to the toolbar?
[Question]
The toolbar is also a window with a title. How can I add a title to the toolbar?
[Program Implementation]
You do not want to dynamically change the toolbar title in cmainframe: oncreate:
Int cmainframe: oncreate (maid)
{
......
M_wndtoolbar.setwindowtext (_ T ("standdard "));
Return 0;
}
To dynamically change the title of a toolbar, follow these steps:
Declare a menu and respond to events, such as the onmytoolbar () function.
Void cmainframe: onmytoolbar ()
{
// Todo: add your command handler code here
Ctoolbar * ptoolbar = (ctoolbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_toolbar );
Ptoolbar-> setwindowtext (_ T ("standdard "));
}
Do not make onmytoolbar () errors when toobar is suspended.
By the way, how can we get the pointer of a status bar:
Cstatusbar * pstatusbar = (cstatusbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_status_bar );
24. display the device coordinates and logical coordinates of the mouse in the status bar
The origin of the device coordinate system of the monitor is in the upper left corner of the customer zone. The X axis increases to the right, and the Y axis increases downward. The origin of the logical coordinate system we want to set is in the center of the customer zone, the X axis increases to the right, and the Y axis increases upwards, such as a Cartesian coordinate system.
Add a member function void onpreparedc (CDC * PDC, cprintinfo * pinfo = NULL) to cchildview );
Void onpreparedc (CDC * PDC, cprintinfo * pinfo ){
Crect rect;
// Set the loing mode to lometric (0.1mm) and the upper right to the growth direction.
PDC-> setmapmode (mm_lometric );
// Set the coordinate origin to the center of the customer Zone
Getclientrect (rect );
PDC-> setviewportorg (rect. Width ()/2, rect. Height ()/2 );
}
Responds to the mouse movement message for cchildview and displays the coordinates of the mouse in the status bar. M_ptmouse data members are originally intended to be used for cross-line, which has no practical significance.
Void cchildview: onmousemove (uint nflags, cpoint point ){
Cclientdc DC (this );
Cstring STR;
Onpreparedc (& DC );
// To upload a class cmainframe, You need to introduce the mainfrm. h file
Cmainframe * pframe = (cmainframe *) afxgetapp ()-> m_pmainwnd;
// To access the data member m_wndstatusbar of the cmainframe, You need to manually modify the data member mainfrm. h and public.
Cstatusbar * pstatus = (cstatusbar *) & pframe-> m_wndstatusbar;
M_ptmouse = point;
Str. Format ("device coordinate X = % I pixel, y = % I pixel", m_ptmouse.x, m_ptmouse.y );
Pstatus-> setpanetext (1, STR );
DC. dptolp (& m_ptmouse );
Str. Format ("logical coordinate X = % I * 0.1mm, y = % I * 0.1mm", m_ptmouse.x, m_ptmouse.y );
Pstatus-> setpanetext (2, STR );
}
25. How to Use VC ++ to dynamically modify the application menu
[Question]
This article describes how to use cmenu, such as searching for a specified menu and adding a menu item before the specified menu .....
[Solution]
Use cwnd: getmenu () to access the main menu. getmenu () returns a pointer to the cmenu object. It has some member functions that allow us to modify a menu.
1) how to find a menu item:
The procedure is as follows:
{
// Dynamic modification menu:
// Get the main menu
Cmenu * pmainmenu = afxgetmainwnd ()-> getmenu ();
Cmenu * psubmenu = NULL;
Int I;
For (I = 0; I <(INT) pmainmenu-> getmenuitemcount (); I ++)
{
Psubmenu = pmainmenu-> getsubmenu (I );
If (psubmenu & psubmenu-> getmenuitemid (0) = id_file_new)
Break;
}
Cstring S;
S. Format ("% d", I); // number of digits of the menu item.
Afxmessagebox (s );
Assert (psubmenu );
}
2) Dynamic editing menu:
Follow these steps .):
1) Add a menu command named wzd2 with the command ID idc_name_new1 to the menu. You can use:
Psubmenu-> appendmenu (0, idc_name_new1, "New & 1 ");
2) Insert new2 before new1. You can use:
Psubmenu-> insertmenu (idc_name_new1, mf_bycommand, idc_name_new2, "New & 2 ");
3) Change new1 to new3. You can use:
Psubmenu-> modifymenu (idc_name_new1, mf_bycommand, idc_name_new3, "New & 3 ");
4) Delete the second item in the menu. You can use:
Psubmenu-> removemenu (1, mf_byposition );
26. Programming of 3D buttons in VC ++
Run Appwizard to generate a test project based on the dialog box, and add a cbutton control to the dialog box. On the general properties page of The cbutton control
The ID is changed to idc_3dtextbtn, the caption is changed to "who is crazy", and the ownerdraw is selected on the styles property page of the control. The remaining settings are kept by default.
Use classwizard to create a new class: c3dtextbutton. The base class is cbutton. Add
Protected function void draw (CDC * PDC, const crect & rect, uint
State ). Write the Code as follows:
Void c3dtextbutton: Draw (CDC * PDC, const crect & rect, uint state)
{
Cstring text; getwindowtext (text );
Int L = text. getlength ();
Crect rectclient = rect;
// Obtain the control font
Cfont * pfont = getfont ();
// Determine the valid font height and width
Logfont;
Pfont-> GetObject (sizeof (logfont), & logfont );
If (logfont. lfheight = 0) logfont. lfheight = 20;
Logfont. lfwidth = 0; // The width is set to 0, and the width value is determined by the height.
Logfont. lfweight = 1000;
Logfont. lfescapement = logfont. lforientation = 0;
Cfont tryfont; Verify (tryfont. createfontindirect (& logfont ));
Cfont * pfontold = PDC-> SelectObject (& tryfont );
// Adjust the font height based on the widget size to coordinate the text with the widget
Csize textsizeclient = PDC-> gettextextent (text, L );
If (rectclient. Width () * textsizeclient. Cy> rectclient. Height () * textsizeclient. CX)
{
Logfont. lfheight =: muldiv (logfont. lfheight, rectclient. Height (), textsizeclient. Cy );
}
Else {
Logfont. lfheight =: muldiv (logfont. lfheight, rectclient. Width (), textsizeclient. CX );
}
// Create and select the coordinated font
Cfont font; font. createfontindirect (& logfont );
PDC-> SelectObject (& font );
Textsizeclient = PDC-> gettextextent (text, L );
// Determine the distance between the text and the control boundary Minx and miny
Int Minx = rectclient. Left + (rectclient. Width ()-textsizeclient. CX)/2;
Int miny = rectclient. Top + (rectclient. Height ()-textsizeclient. Cy)/2;
Int oldbkmode = PDC-> setbkmode (transparent );
Colorref textcol =: getsyscolor (color_btntext );
Colorref oldtextcolor = PDC-> settextcolor (textcol );
Int Cx = Minx;
Int Cy = miny;
Int S = (State & ods_selected )? -1: + 1;
Cx + = 3; CY + = 3;
// 3D effect
PDC-> settextcolor (: getsyscolor (color_3ddkshadow ));
PDC-> textout (CX-S * 2, Cy + S * 2, text );
PDC-> textout (cx + S * 2, cy-S * 2, text );
PDC-> textout (cx + S * 2, Cy + S * 2, text );
PDC-> settextcolor (: getsyscolor (color_3dhilight ));
PDC-> textout (cx + S * 1, cy-S * 2, text );
PDC-> textout (CX-S * 2, Cy + S * 1, text );
PDC-> textout (CX-S * 2, cy-S * 2, text );
PDC-> settextcolor (: getsyscolor (color_3dshadow ));
PDC-> textout (CX-S * 1, Cy + S * 1, text );
PDC-> textout (cx + S * 1, cy-S * 1, text );
PDC-> textout (cx + S * 1, Cy + S * 1, text );
PDC-> settextcolor (: getsyscolor (color_3dlight ));
PDC-> textout (CX, cy-S * 1, text );
PDC-> textout (CX-S * 1, Cy, text );
PDC-> textout (CX-S * 1, cy-S * 1, text );
PDC-> settextcolor (textcol );
// Output the title
PDC-> textout (CX, Cy, text );
// Restore the device description table
PDC-> settextcolor (oldtextcolor );
PDC-> setbkmode (oldbkmode );
PDC-> SelectObject (pfontold );
}
Use classwizard to overload the drawitem function of the c3dtextbutton class. Write the Code as follows:
Void c3dtextbutton: drawitem (lpdrawitemstruct)
{
CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
Assert_valid (PDC );
Crect rectclient = lpdrawitemstruct-> rcitem;
Draw (PDC, rectclient, lpdrawitemstruct-> itemstate );
}
Use classwizard to create a c3dtextbutton control variable m_3dtextbutton1 for idc_3dtextbtn.
Add "3dtextbutton. H" to the header file testdlg. Compile and test the application.
27. How to get the combox pointer correctly
Ccombobox * mcomb = (ccombobox *) getdlgitem (idc_duancb );
Ccombobox * mcomb = (ccombobox *): getdlgitem (m_hwnd, idc_duancb );
28. How to Make the cedit control class in the dialog box receive messages in the dialog box
//////////////////////////////////////// ////////
// How to make the cedit control class in the dialog box receive messages in the dialog box
//////////////////////////////////////// ////////
1. Add a cedit1 control with ID idc_edit1 in the dialog box.
2. Use classwizard to generate a new cedit-based cmyedit class,
Cmyedit m_wndedit;
3. In the oninitdialog () dialog box, subclass m_wndedit so that it can accept messages in the dialog box.
M_wndedit.subclassdlgitem (idc_edit1, this );
29. Use the wm_ctlcolor message to change the text and background color of the Edit Control.
First, you must understand that wm_ctlcolor is a notification message sent to its parent window by control ).
Steps:
Generate a standard single-document application framework, assuming that the Application name is color. I will use its about dialog box for demonstration. Add two edit controls to the about dialog file and set the ID to idc_edit1 and idc_edit2.
Method 1 (corresponding to idc_edit1): according to standard windows programming, the message processing function of its parent window is responsible for processing wm_ctlcolor messages.
1. Add a data member in caboutdlg: hbrush m_brmine;
2. Map the wm_ctlcolor message of aboutdlg using the Wizard to generate the function: hbrush caboutdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );
PDC is the device context of aboutdlg, pwnd is the control pointer for sending the message in aboutdlg, And the type encoding of control in nctlcolor. Modify it as follows:
Hbrush caboutdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
If (pwnd-> getdlgctrlid () = idc_edit1) & (nctlcolor = ctlcolor_edit ))
{
Colorref CLR = RGB (255, 0, 0 );
PDC-> settextcolor (CLR); // sets the red text.
CLR = RGB (0, 0 );
PDC-> setbkcolor (CLR); // sets the black background.
M_brmine =: createsolidbrush (CLR );
Return m_brmine; // return the brush handle corresponding to the background color.
}
Else
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
}
}
Method 2 (corresponding to idc_edit2 ):
Use Message reflection, a new feature of MFC 4.0.
1. Use the Wizard to add a new class: ccoloredit. The base class is cedit;
2. Add a data member hbrush m_bkbrush in ccoloredit;
3. Use the Wizard to map the "= wm_ctlcolor" message of ccoloredit to generate the function:
Hbrush ccoloredit: ctlcolor (CDC * PDC, uint nctlcolor );
Modify it as follows:
Hbrush ccoloredit: ctlcolor (CDC * PDC, uint nctlcolor)
{
Colorref CLR = RGB (0, 0 );
PDC-> settextcolor (CLR); // sets the black text.
CLR = RGB (255, 0, 0 );
PDC-> setbkcolor (CLR); // sets the red background.
M_bkbrush =: createsolidbrush (CLR );
Return m_bkbrush; // return the brush handle corresponding to the background color.
}
4. Use the Wizard to generate a data member ccoloredit m_coloredit for idc_edit2;
5. Add: # include "coloredit. H" to the color. cpp file that defines caboutdlg"
30. How to prevent unauthorized access to passwords?
[Question]
In the past two days, the company has been focusing on obtaining the password in the edit Password box. How can we prevent theft?
[Solution]
This method is used to send the wm_gettext or em_getline message to this window through sendmessage to obtain the password. Come with me.
[Program Implementation]
The method is simple. You can use the cwnd: defwindowproc function to intercept the message (sent to edit ).
Create a dialog project named my. Create an edit control id = idc_edit1. Create a new class named cmyprotectedit, derived from cedit.
Declare the global variable bool g_bidentity in mydlg. cpp;
Bool g_bidentity;
In myprotecedit. cpp:
Extern bool g_bidentity;
Response to the defwindowproc function of cmyprotectedit:
Lresult cmyprotectedit: defwindowproc (uint message, wparam, lparam)
{
// Todo: add your specialized code here and/or call the base class
// To obtain the edit content, you must use one of the following two messages. Do not use the default message:
If (Message = wm_gettext) | (Message = em_getline ))
{// Check whether it is legal
If (! G_bidentity)
{// Illegal access, illegal information displayed
Afxmessagebox (_ T ("You cannot view my password ,:(! "));
Return 0;
)
G_bidentity = false; // obtain it legally
}
Return cedit: defwindowproc (message, wparam, lparam );
}
In mydlg. cpp
Void cmydlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
// {Afx_data_map (cgetpassworddlg)
// Note: The classwizard will add DDX and DDV cballs here
If (PDX-> m_bsaveandvalidate)
{
G_bidentity = true;
}
//} Afx_data_map
}
You can find a program to steal.