MFC learning to modify setting control font display and color parameters

foreword

insert image description here

Recently, I have been cooperating with the R&D department to write some small debugging software. I remember that the configuration of the computer monitor and graphics card was not high, the display resolution was relatively low, and the text on the software interface could still be seen clearly (not too small).

Now the company's newly configured computers are relatively good, and the interface fonts, especially some values ​​that display real-time status or lower computer device status values, need to be highlighted, so it is necessary to adjust the font size and color of the control display.

really difficult...

Is there a way to set it in batches? Friends who know it can discuss it together below, and we will learn together (I have relatively little development experience).

Introduction to MFC Controls

The MFC control is a part of the MFC class library, which provides various visual interface elements for building user interfaces. The following are some common MFC controls and their functions and property settings:

1. CButton (button): A control used to trigger a specific operation or perform a selected action. You can set text, images, styles, etc.
2. CEdit (edit box): A control for receiving and displaying text input. You can set text content, size, style, limit input, etc.
3. CListCtrl (list control): A control used to display data in list form. Support single or multiple columns of data, you can set the data type, width, sorting, etc. of each column.
4. CComboBox (combo box): A control that combines a drop-down list box and an edit box. Allow users to choose from predefined options or enter their own.
5. CListBox (list box): A control used to display a list of one or more items. Supports single or multiple selection modes, and provides functions such as adding, deleting, and selecting items.
6. CScrollBar (scroll bar): A control used to select values ​​within a scrollable range. Provides both horizontal and vertical scrolling.
7. CSliderCtrl (slider control): A control used to select a value within a range of values. You can set min, max, slider position, etc.
8. CStatic (Static Text Control): A control used to display static text content. Typically used for labels, instructions, or status displays, etc.
9. CProgressCtrl (progress bar control): A control used to display the progress of the operation, which can reflect the completion percentage of the operation.
10. CDateTimeCtrl (date time control): A control for selecting and editing date and time. Support date, time format, locale and other attributes.
11. CRichEditCtrl (rich text edit box): A control for editing and displaying formatted text. Supports font styles, colors, image insertion, and more.
12. CTreeView (tree view control): A control used to display hierarchical relationships in a tree structure. Support node selection, expand/collapse, drag and drop and other operations.

Some controls modify the color and font sample code

The following are code samples for font and color settings of some common controls:

Modify the font and color of the button (CButton):

// 获取按钮控件对象
Button* pButton = (CButton*)GetDlgItem(IDC_BUTTON1);

// 创建字体对象
CFont font;
font.CreatePointFont(12, _T("Arial"));

// 设置字体
pButton->SetFont(&font);

// 设置文本颜色
pButton->SetTextColor(RGB(255, 0, 0)); // 设置为红色

Modify the font and color of the edit box (CEdit):

// 获取编辑框控件对象
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);

// 创建字体对象
CFont font;
font.CreatePointFont(10, _T("Verdana"));

// 设置字体
pEdit->SetFont(&font);

// 设置背景颜色和文本颜色
pEdit->SetBackgroundColor(FALSE, RGB(192, 192,192)); // 设置背景为灰色
pEdit->SetTextColor(RGB(0, 0, 255)); // 设置本为蓝色

Modify the font and color of the list control (CListCtrl):

// 获取列表控件对象
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);

// 创建字体对象
CFont font;
font.CreatePointFont(8, _T("Tahoma"));

// 设置字体
pListCtrl->SetFont(&font);

// 设置文本颜色
pListCtrl->SetTextColor(RGB(0, 128, 0)); // 设置为绿色

// 设置背景颜色
pListCtrl->SetBkColor(RGB(255, 255, 0)); // 设置为黄色

Modify the font and color of the static text control (CStatic):

// 获取静态文本控件对象
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATIC1);

// 创建字体对象
CFont font;
font.CreatePointFont(14, _T("Courier New"));

// 设置字体
pStatic->SetFont(&font);

// 设置文本颜色
pStatic->SetTextColor(RGB(255, 0, 0)); // 设置为红

Modify the font and color of the combo box (CComboBox):

// 获取组合框件对象
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO1);

// 创建字体对象
CFont font;
font.CreatePointFont(12, _T("Arial"));

// 设置字体
pComboBox->SetFont(&font);

// 设置文本颜色
pComboBox->SetTextColor(RGB(255, 0, 0)); // 设置为红色

// 设置背景颜色
pComboBox->SetBkColor(RGB(192, 192, 192)); // 设置为灰色

Modify the font and color of the scroll bar (CScrollBar):

// 获取滚动条控件对象
CScrollBar* pScrollBar (CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);

// 创建字体对象
CFont font;
font.CreatePointFont(10, _T("Verdana"));

// 设置字体
pScrollBar->SetFont(&font);

// 设置滑块颜色
pScrollBar->SetScrollColors(RGB(0, 0, 0), RGB(255, 0, 0)); // 设置滑块为黑色,背景为红色

Modify the word and color of the progress bar (CProgressCtrl):

// 获取进度条控件对象
CProgressCtrl* pProgressCtrl = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);

// 创建字体对象
CFont font;
font.CreatePointFont(10, _T("Verdana"));

// 设置字体
pProgressCtrl->SetFont(&font);

// 设置前景颜色(进度条颜色)
pProgressCtrl->SetBarColor(RGB(0, 255, )); // 设置为绿色

// 设置背颜色
pProgressCtrl->SetBkColor(RGB(255, 0,0)); // 设置为红色

Modify the font and color of the date and time control (CDateTime):

// 获取日期时间控件对象
CDateTimeCtrl* pDateTimeCtrl =CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER1);

// 创建字体对象
C font;
font.CreatePointFont(10, _T("Arial"));

// 设置字体
pDateTimeCtrl->SetFont(&font);

// 设置文本颜色
pDateTimeCtrl->SetMonthColor(MCSC_TEXT, RGB0, 0, 255)); // 设置日历为蓝色

// 设置背景颜色
pDateTimeCtrl->SetMonthCalColor(MCSC_BACKGROUND, RGB(192, 192, 192)); // 设置日历背景为灰色

at last

The above is the content shared today. I hope it will be helpful to the friends who saw it. In addition, if there is any way to realize batch modification in the article, you can actively speak up. Let's learn and make progress together!

Guess you like

Origin blog.csdn.net/u014740628/article/details/131331985