How to modify the font size and color of the text in CEdit (finishing)

Source: Internet
Author: User
How to modify the font size and color (finishing) of the text in CEdit to process the WM_CTRLCOLOR message in the heavy load dialog box.
In the following example, m_edit is a text box object (a member of the dialog box ).
HBRUSH CTestDlg: OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog: OnCtlColor (pDC, pWnd, nCtlColor );
// TODO: Change any attributes of the DC here
If (pWnd = & m_edit)
{
PDC-> SetBkMode (TRANSPARENT );
PDC-> SetTextColor (RGB (0, 0, 255 ));
HBRUSH newHbr = CreateSolidBrush (GetSysColor (COLOR_BTNFACE ));
// TODO: Return a different brush if the default is not desired
Return newHbr;
}
Return hbr;
}
You can also derive a class from CEdit and reload its OnPaint function.
Bytes ---------------------------------------------------------------------------------------
From the perspective of software engineering, it is best to derive a class from the cedit class that can handle the font, background, and font color on its own. According to your requirements, the fastest way to achieve this is to use the unique message reflection mechanism of MFC, so that the edit control can complete color settings on its own.
We know that when the control needs to be re-painted, it generally sends a wm_ctlcolor to its parent class, requiring the parent class to provide a DC for re-painting. in the preceding method, if there are 20 edit controls in the dialog box, isn't it necessary to judge and process each control? Why don't you let these edit controls know how to reproduce them? Therefore, we can use Message reflection, an advanced MFC technology. message reflection refers to sending some notification messages sent from the control to the parent class and then returning them to the control itself for control processing. there are many messages that can be reflected, except wm_ctlcolor and wm_notify, wm_drawitem, etc. We can use Message reflection to implement a class that can customize the color and font. Once and for all, put it in the components Gallery of VC for use at any time.

The following describes how to perform the following operations:
1. create a new class in your project, that is, open classwizard, click Add class, and select new. The new class is derived from cedit and called ccoloredit. click OK. The ccoloredit class is added to your project.
2. switch to the Class View, right-click CColorEdit, and select "Add Windows Message Handler". In the displayed dialog box, you can see many messages that can be processed. note: No. One of the messages is "= WM_CTLCOLOR", and the '=' sign is added to indicate that it is a reflected message. select the message, click Add handler, and click OK. Classwizard automatically maps the message for you.
3. your CColorEdit class will have a member function of HBRUSH CColorEdit: CtlColor (CDC * pDC, UINT nCtlColor. you can declare two member variables for this class, indicating the foreground color and background color respectively. now that you have a re-painted pDC, the rest of the code is similar to what Brother andygood provides.
After that, you only need to specify the foreground color and background color of the edit control. For other things, it will do it by itself, and you do not have to bother with the parent class.
In short, if you are good at Message reflection, sometimes it can help you get twice the result with half the effort.

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.