41, how to create a three-state lower pressure button
You can create a three-state lower press button using the new BS_PushButton style bit and the detection box and buttons. This is easy, just drag the detection box and buttons to the dialog and specify the property push-like. You can become a three-state lower pressure button without any additional programs.
42, how to dynamically create controls
Assign an instance of a control object and call its CREATE member function. Developers easily ignore two things: Forget to specify the WS_VISBLE tag and assign control objects in the stack. The following example is dynamically created a downstream button control:
// in class declaration (.h file).
Private:
CButton * m _pButton;
// in class mailmentation (.cpp file).
m_pbutton = new cbutton;
Ask_VALID (M_PButton);
m_pButton-> Create (_T ("Button Title"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON.
CRECT (0, 0, 100, 24), this, IDC _MYBUTTON)
43, how to limit the allowable characters in the edit box
If the user only allows reception numbers in the editing control, you can use a standard editing control and specify a new creation flag ES_NUMBERS. It is a newly added flag of Windows 95, which restricts the editing control only to record simplicity characters. If the user needs a complex editing control, you can use Microsoft's mask editing control, which is a very useful OLE custom control.
If you want to use the OLE custom control to handle the character, you can derive a CEDIT class and process the WM_CHAR message, and then filter out specific characters from the editing control. First, create a CEDIT derived class using ClassWizard, followed by specifying a member variable in the dialogue class to call the Edit Control in the OnNitDialog to call CWnd:: Subclassdlgitem.
// in Your Dialog class declaration (.h file)
Private:
CMYEDIT M_WNDIT; // Instance of Your New Edit Control.
// in you dialog class importation (.cpp file)
Bool Csampledialog:: OnInitdialog ()
{
...
// Subclass the Edit Lontrod.
m_wndedit .subclassdlgitem (IDC_EDIT, this);
...
}
Use the classwizard to process the WM_CHAR message, calculate the nchar parameter and determine the operation performed, and the user can determine if the modification is modified, and the character can be configured. The following example shows how to display alphanumeric characters, if the character is alphabetic characters, call CWnd; onchar, otherwise I will not call OnChar.
// ONLY Display Alphabetic Dharacters.
Void CMYEDIT:: OnChar (uint nchar, uint nrepcnt, UITN NFLAGS)
{
// determine if nchar is an alphabetic character.
IF (: ischaralpha) Nchar)
CEDIT:: Onchar (nchar, nrepcnt, nflags);
}
If you want to modify the character, you cannot simply call Cedit: onchar, then CEDIT: onchar call CWnd:: Default Gets the value of the original WPARAM and LPARAM, which is not. To modify a character, you need to modify NCHAR first, then call CWnd:: DefWindowProc with modified NCHAR. The following example shows how to transform characters to uppercase: // Make All Characters Uppercase
Void CMYEDIT:: OnChar (uint nchar, uint nrepcnt, uint nflags)
{
// make Sure Character is Uppercase.
IF (: ischaralpha (. (tchar) nchar)
Nchar =:: charupper (nchar);
// bypass default onchar processing and directly call
// Default window proc.
DefWindProc (WM_CHAR, NCHAR, MAKELPARAM (NREPCNT, NFLAGS);
}
44, how to change the color of the control
There are two ways. First, you can specify the color of the control in the parent class, or use the MFC4.0 new message to specify the color in the control class. When the control needs to be reatched, the Workflow calls the parent window (usually the dialog) CWnd:: OnCrtLColor, you can reset the function in the parent window class and specify the new painting properties of the control. For example, the following code changes all edit controls in the conversation to red:
Hbrush Caboutdig:: OnctLcolor (CDC * PDCM, CWND * PWND, UINT NCTLCOLOR)
{
Hbrush Hbr = CDIALOG:: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);
// Draw Red Text for All Edit Controls.
IF (nctlcolor == ctlcolor_edit)
PDC-> SetTextColor (RGB (255, 0, 0,);
Return Hbr;
}
However, since each parent window must handle the notification message and specify the painting attribute of each control, this method is not a fully object-oriented method. The control handles the message and specifies that the painting attribute is more reasonable. Message reflections allow users to do so. The notification message is first sent to the parent window, and if the parent window is not processed, it is sent to the control. Creating a custom color list box control must follow the steps below.
First, create a CLISTBOX derived class using ClassWizard and add the following data to this class.
Class CmylistBox; Publilc Clistbox
{
...
PRIVATE;
ColorRef M_Clrfor; // ForeGround Color
ColorRef M_clrback; // Background Color
CBRUSH M_Brush; // Background Brush
...
}
Second, in the constructor of the class, the data is initialized.
CMYListBox:: cmylistbox ()
{
// Initialize Data Members.
m_clrfore = RGB (255, 255, 0); // Yellow Text
m_clrback = RGB (0, 0, 255); // blue background
M_brush. CreateSolidbrush (M _Clrback);
}
Finally, use the ClassWizard to process the reflected WM_CTLCOLOR message and specify the new painting attribute. Hbrush Cmylistbox:: CTLCOLOR (CDC * PDC, UINT NCTLCOLOR)
{
PDC-> SetTextColor (M_CLRFORE);
PDC-> SetBkcolor (m_clrback);
Return (hbrush) m_brush.getsafehandle ()
}
Now, the control can determine how to paint itself and have nothing to do with the parent window.
45, how to prevent flashing when adding multiple items to the list box
Call CWnd :: SetredRaw Clear Heavy Picture Sign can prohibit CLISTBOX (or window) redraw. When you add a few items to the list box, the user can clear the heavy-in flag, then add the item, and finally restore the heavy painting flag. To ensure the new item of the redraw list box, call SetREDRAW (TRUE) to call CWnd :: Invalidate.
// disable redrawing.
PListbox-> SetredRaw (false);
// Fill in The List Box Gere
// Enable DRWING AND Make Sure List Box is RedRaw.
PListbox-> SetredRaw (TRUE);
PListbox-> invalidate ();
46, how to add text to the editor control
Since there is no CEDIT :: AppendText function, users have to do this job. Call CEDIT :: Setsel Move to the end of the edit control, then call Cedit :: ReplaceSel to add text. The following example is an implementation method of appendtext:
Void CMYEDIT :: appendtext (lpcstr ptext)
{
INT Nlen = getWindowTextLength ();
Setfocus ();
Setsel (Nlen, NLEN);
ReplaceSel (PTEXT);
}
47, how to access predefined GDI objects
Several predefined objects of Windows can be used by calling CDC :: SlectStockObject, such as a brush, pen, and font. The following example uses the Windows predefined pen and the brush GDI object to draw an ellipse in the window.
// Draw Ellipse Using Stock Black Pen and Gray Brush.
Void CsampleView :: OnDraw (CDC * PDC)
{
// determine size of view.
CRECT RCVIEW;
GetClientRect (rcview);
// Use stock Black Pen and stock gray brush to draw elipse.
PDC-> SelectStockObject (Black_pen);
PDC-> SelectStockObject (gray_brush)
// Draw the Ellipse.
PDC-> Ellipse (REVIEW);
}
You can also call the new SDK function getSysColorbrush to get a system color brush. The following example uses the background color to draw an ellipse in the window:
Void CsampleView :: OnDraw (CDC * PDC)
{
// determine size of view.
CRECT RCVIEW;
GetClientRect (rcview);
// Use Background Color for Tooltips Brush.
CBRUSH * PORGBRUSH = PDC-> SelectObject (
CBRUSH :: fromHandle (:: getsyscolorbrush); // Draw the Ellipse.
PDC-> Ellipse (RCVIEW);
// restore Original Brush.
PDC-> SELECTOBJECT (PORGBRUSH);
}
48, how to get the properties information of GDI objects
You can call GDIOBJECT :: getObject. This function writes the message of the specified chart device to the buffer. The following example creates several useful auxiliary functions.
// determine if font is bold.
Bool IsFontBold (Const CFont & Font)
{
Logfont stfont;
Font.getObject (sizeof (logfont), & stfont;
Return (stfont.lfbold)? True: false;
}
// Return the size of a bitmap.
CSIZE GETBITMAPSIZE (Const CBitmap & Bitmap)
{
Bitmap stbitmap;
BitMap.GetObject (Sizeof (Bitmap), & Stbitmap;
Return CSIZE (stbitmap.bmwidth, stbitmap. bmheight);
}
// Create a Pen with The Same Color as a brush.
Bool Createpenfrombrush (CPEN & PEN, COST CBRUSH & BRUSH)
{
Logbrush stbrush;
Brush.getObject (Sizeof (Logbrush), & Stbrush;
Return Pen. Createpen (ps_solid, 0, stbrush.ibcolor);
}