更改MFC中运行时的编辑框属性

Change Edit Box Properties at Runtime in MFC

本文关键字:编辑 属性 运行时 MFC 更改      更新时间:2023-10-16

在我的应用程序中,我必须在运行时更改编辑框的Font、Font Size和Background Color属性。当用户选择一种特定的字体时,它的颜色应该更新并在编辑框中可见。我正在尝试使用CColorDialog、CFontDialog来实现这一点。有什么有效的方法吗??。我可以像在Visual Studio环境中那样使用属性栏来更改设置吗?我们在开发环境中使用它来更改属性。

您可以在以CEdit为父级的类中捕获WM_CTLCOLOR消息,然后将CDC对象更改为您的内容
例如:

HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
    HBRUSH hBrush;
    hBrush = (HBRUSH)m_myBrush; // An handle on a brush which was created with your background color for the edit
    pDC->SetBkColor(RGB(0, 0, 0)); // Color for the text background
    pDC->SetTextColor(RGB(255, 255, 255)); // Color for the text
    // More changes on the pDC like changing the font, etc...
    return hBrush;
}