// CPjPage1.cpp: 实现文件 // #include "stdafx.h" #include "Servo.h" #include "CCjPageBase.h" #include "afxdialogex.h" // CPjPage1 对话框 IMPLEMENT_DYNAMIC(CCjPageBase, CDialogEx) CCjPageBase::CCjPageBase(UINT nID, CWnd* pPage) : CDialogEx(nID, pPage) { m_crBkgnd = RGB(255, 255, 255); m_crBkgndCached = CLR_INVALID; m_onContentChanged = nullptr; m_bContentChangedLock = FALSE; m_pContext = nullptr; m_nContextType = 0; } CCjPageBase::~CCjPageBase() { } void CCjPageBase::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CCjPageBase, CDialogEx) ON_WM_CTLCOLOR() ON_WM_SIZE() END_MESSAGE_MAP() // CPjPage1 消息处理程序 void CCjPageBase::SetTitle(CString strTitle) { SetDlgItemText(IDC_LABEL_TITLE, strTitle); } void CCjPageBase::SetContext(void* pContext, int type) { m_pContext = pContext; m_nContextType = type; OnSetContext(pContext); } void* CCjPageBase::GetContext() { return m_pContext; } void CCjPageBase::SetOnContentChanged(ONCONTENTCHANGED onContentChanged) { m_onContentChanged = onContentChanged; } BOOL CCjPageBase::OnInitDialog() { CDialogEx::OnInitDialog(); Resize(); m_labelTitle.SubclassDlgItem(IDC_LABEL_TITLE, this); m_labelTitle.Setpadding(PADDING_LEFT, 0); m_labelTitle.Setpadding(PADDING_RIGHT, 0); ; return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CCjPageBase::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); // 想给哪些控件改底色就把它们的类型写进来: const bool needCustomBg = (nCtlColor == CTLCOLOR_STATIC) || (nCtlColor == CTLCOLOR_DLG) || // 对话框底色 (nCtlColor == CTLCOLOR_BTN); // 按钮(可选) if (needCustomBg) { // 若第一次创建,或颜色改变则重建 if (m_brBkgnd.GetSafeHandle() == nullptr || m_crBkgndCached != m_crBkgnd) { if (m_brBkgnd.GetSafeHandle()) m_brBkgnd.DeleteObject(); m_brBkgnd.CreateSolidBrush(m_crBkgnd); m_crBkgndCached = m_crBkgnd; } // 文本前景/背景设置(仅影响文本绘制) pDC->SetBkColor(m_crBkgnd); pDC->SetTextColor(RGB(0, 0, 0)); // 如需让静态文本透明叠在底色上,可用: // pDC->SetBkMode(TRANSPARENT); return (HBRUSH)m_brBkgnd; // 安全的隐式转换 } // 其他控件类型沿用基类默认的刷子 return hbr; } void CCjPageBase::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_LABEL_TITLE) == nullptr) return; Resize(); } void CCjPageBase::Resize() { CWnd* pItem; CRect rcClient, rcItem; GetClientRect(&rcClient); pItem = GetDlgItem(IDC_LABEL_TITLE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(12, 8, rcClient.Width() - 24, rcItem.Height()); } void CCjPageBase::ContentChanged(int code) { if (!m_bContentChangedLock && m_onContentChanged != nullptr) { m_onContentChanged(this, code, m_pContext, m_nContextType); } }