Handling WM_PAINT in a Subclassed CStatic Control(在子类 CStatic 控件中处理 WM_PAINT)
问题描述
我创建了一个自定义控件,其类以 CStatic 作为基类.目前我使用 WM_PAINT 事件处理绘图.但是有一个奇怪的行为.当我使用 CWnd::EnableWindow 函数禁用它后重新启用窗口时,它拒绝绘制我在 OnPaint 函数中编写的内容.它改为绘制静态控件.
I created a custom control whose class has CStatic as base class. Currently I handle the drawing using WM_PAINT event. But there is a strange behavior. When I re-enable the window after disabling it using CWnd::EnableWindow function, it refuses to draw what I written in OnPaint function. It draws the static control instead.
我同意有这种覆盖 DrawItem 并使用 SS_OWNERDRAW 样式的标准方法.但是 WM_PAINT 有什么问题?
I agree that there is this standard method of overriding DrawItem and using SS_OWNERDRAW style. But what's wrong with WM_PAINT?
void XXControl::OnPaint()
{
CPaintDC PaintDC( this );
// ** draw the control to PaintDC**
}
推荐答案
这正是我写的:
class CMyStatic : public CStatic
{
DECLARE_MESSAGE_MAP()
public:
void OnPaint(void);
};
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyStatic::OnPaint(void)
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(120,255,0));
}
和子类化:
class CMyDlg : public CDialog
{
// Construction
CMyStatic my_static;
...
};
BOOL CCMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
my_static.SubclassDlgItem(IDC_DRAW, this);
return true;
}
IDC_DRAW 是此对话框资源的静态控件.我写了两个按钮处理程序:
Where IDC_DRAW is static control on resource for this dialog. I wrote two button handlers:
void CMyDlg::OnBnClickedOk()
{
my_static.EnableWindow(FALSE);
my_static.Invalidate();
}
void CMyDlg::OnBnClickedOk2()
{
my_static.EnableWindow();
my_static.Invalidate();
}
而且它完美无瑕!删除 Invalidate 调用会失败.
And it works flawlessly! Remove Invalidate call and it would fail.
这篇关于在子类 CStatic 控件中处理 WM_PAINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在子类 CStatic 控件中处理 WM_PAINT
基础教程推荐
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
