我正在尝试使用
OnCtlColor
处理程序使我的控件透明:
Fragment.h
#pragma once
#include "afxdialogex.h"
#include "Fragment.h"
#include "resource.h"
class Fragment : public CDialog
{
public:
Fragment(CWnd* pParent = nullptr) : CDialog(IDD_FRAGMENT1, pParent) {}
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetTextColor(RGB(220, 120, 120));
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum
{
IDD = IDD_FRAGMENT1
};
#endif
protected:
DECLARE_DYNAMIC(Fragment)
DECLARE_MESSAGE_MAP()
void DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SYSLINK1, m_link);
}
CLinkCtrl m_link; // is syslink
};
Fragment.cpp
#include "pch.h"
#include "Fragment.h"
IMPLEMENT_DYNAMIC(Fragment, CDialog)
BEGIN_MESSAGE_MAP(Fragment, CDialog)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
它适用于其他控件,但不适用于
CLinkCtrl
。
然而,当我在 VS 的资源编辑器中切换它的 CLinkCtrl
属性时,Use visual style
会改变颜色(我认为,它会将颜色更改为系统主题的颜色)。
更新:我不确定为什么我忽略了这个问题,但我尝试添加更多代码。
也许你不需要定义一个新类或使用
WM_CTLCOLOR
,因为透明背景绘制似乎是原生支持的。检查LWS_TRANSPARENT
样式;如果无法在属性编辑器中设置,您可以编辑脚本或以编程方式设置(在 OnInitDialog()
中)。