Write colorful CEDIT controls

zhaozj2021-02-16  51

The standard controls and class libraries provided by VC are very rich, but sometimes they still can't meet our needs, here is CEDIT as an example. We need to filter some input characters (such as digital input), many netizens recommend adopting Delicious method: overloading the CEDIT's onchar () function. This method is indeed clearly clear than others, but also the principle of refining and maintainability. However, we will also encounter a situation: adopting derived cannot achieve demand. For example: Colored Caret and the corresponding color character selection background, define character input, automatic format string, arbitrarily develop EditBox size, arbitrarily set the font, vertical direction, moderate demand. When you can apply, you simply write a CEDIT class. The development method of the CEDIT class is introduced below (don't be afraid, Microsoft standard CEDIT is not so terrible, development is not difficult). 1. You can use the MFC's class wizard, select CWND as base class; now write its crete () function: IF (m_strclassname.isempty ())

{

M_StrClassName = AFXREGISTERWNDCLASS (CS_PARENTDC | CS_HREDRAW | CS_VREDRAW,

:: LoadCursor (null, m_csname),

(Hbrush) :: getStockObject (gray_brush));

}

Lpszclassname = lpctstr (m_strclassname);

DWSTYLE | = WS_CHILD;

Return CWnd :: Create (LpszclassName, Lpszwindowname, DWStyle, Rect, PparentWnd, NID, PCONTEXT);

2. Edit your own Caret:

Probably you rarely see the colored characters Enter the symbol, because the system uses the monochrome Caret.caret created by yourself? The answer is yes. You can use createCaret (cBitMap) to create images as a character input symbol. The hardest is how to control the location of the CARET display. We need to consider the number of pixels (width and height) required for the characters of different fonts, and the same font different characters The width is also different. But don't matter, CPAINTDC provides standard processing methods:

Logfont logfont = wnffont.getfont (m_nfnlogidx);

CFONT font;

Font.createFontIndirect (& logfont); // Draw Font

Dc.selectObject (& font);

CSIZE SZTEXT = DC.GetOutputTextExtent (m_szdata);

Where m_nfnloidx is the font specified by the user, m_szdata is a string that the user has entered; the getOutputTextExtent () function has achieved the detailed string of X coordinate information in the pixel coordinate system, in other words: We can get this Where should CARE should be displayed?

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

New Post(0)