MFC:更改 CEdit 的颜色

2023-07-02C/C++开发问题
1

本文介绍了MFC:更改 CEdit 的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

伙计们,谁能简要介绍一下如何在运行时更改 CEdit 控件的背景颜色?如果字段长度为零,我希望能够将背景更改为红色,否则为正常白色.

Guys, can someone give me a brief run through of how to change the background colour of a CEdit control at runtime? I want to be able to change the background to red if the field is zero length and the normal white otherwise.

推荐答案

你不能用一个普通的 CEdit 来完成,你需要覆盖一些位.

You cannot do it with a plain CEdit, you need to override a few bits.

实现您自己的 ON_WM_CTLCOLOR_REFLECT 处理程序,然后在处理程序中返回您的彩色 CBrush:

Implement your own ON_WM_CTLCOLOR_REFLECT handler, then return your coloured CBrush in the handler:

(粗略地说,你需要把常用的资源管理放在那里,记住在析构函数中删除你的画笔)

(roughly, you'll need to put the usual resource management in there, rememebr to delete your brush in the destructor)

class CColorEdit : public CEdit
{
  ....
  CBrush   m_brBkgnd;
  afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)
  {
    m_brBkgnd.DeleteObject();
    m_brBkgnd.CreateSolidBrush(nCtlColor);
  }
}

这篇关于MFC:更改 CEdit 的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么 C++ 不能从赋值推导出模板类型?
Why can#39;t C++ deduce template type from assignment?(为什么 C++ 不能从赋值推导出模板类型?)...
2024-08-14 C/C++开发问题
8

有状态函子 &STL:未定义的行为
Stateful functors amp; STL : Undefined behaviour(有状态函子 amp;STL:未定义的行为)...
2024-08-14 C/C++开发问题
9

你什么时候在 C++ 中使用函数对象?
When do you use function objects in C++?(你什么时候在 C++ 中使用函数对象?)...
2024-08-14 C/C++开发问题
8

插入 STL 映射是否会使其他现有迭代器无效?
Does insertion to STL map invalidate other existing iterator?(插入 STL 映射是否会使其他现有迭代器无效?)...
2024-08-14 C/C++开发问题
8