遇到一个需求,修改MFC中datetimepicker的背景颜色,看到网上的一些建议,重写OnEraseBkgnd
部分代码如下:
//h
class CCDateTimeCtrlDlg : public CDialogEx
{
// 构造
...
DECLARE_MESSAGE_MAP()
public:
class _CDateTimeCtrl; //内部 CDateTimeCtrl 类
_CDateTimeCtrl* m_pInnerDateTimeCtrl;
...
};
//cpp
class CCDateTimeCtrlDlg::_CDateTimeCtrl : public CDateTimeCtrl
{
public:
_CDateTimeCtrl(CCDateTimeCtrlDlg* pSiteUIComponent)
: m_pSiteUIComponent(pSiteUIComponent)
, m_dwTickCount(0)
{
}
~_CDateTimeCtrl()
{
};
public:
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
private:
CCDateTimeCtrlDlg* m_pSiteUIComponent;
DWORD m_dwTickCount;
CBrush m_cBrush;
};
BEGIN_MESSAGE_MAP(CCDateTimeCtrlDlg::_CDateTimeCtrl, CDateTimeCtrl)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
BOOL CCDa
teTimeCtrlDlg::_CDateTimeCtrl::OnEraseBkgnd(CDC* pDC)
{
//HBRUSH hBrushBkgnd = (HBRUSH) ::SendMessage(GetParent()->GetSafeHwnd(),
// WM_CTLCOLOREDIT, WPARAM(pDC->GetSafeHdc()), LPARAM(m_hWnd));
//CRect rectClient;
//GetClientRect(rectClient);
//pDC->FillRect(rectClient, CBrush::FromHandle(hBrushBkgnd));
//return TRUE;
CBrush bBrush(RGB(10, 200, 255));
//m_cBrush.CreateSolidBrush(RGB(0, 0, 255));
CBrush* pOldBrush2 = pDC->SelectObject(&bBrush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->FillRect(rect, &bBrush);
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(143, 159, 140));
//pDC->SelectObject(pOldBrush2);
return TRUE;
}
HBRUSH CCDateTimeCtrlDlg::_CDateTimeCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDateTimeCtrl::OnCtlColor(pDC, pWnd, nCtlColor);
hbr = ::CreateSolidBrush(RGB(14, 159, 140));
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(14, 159, 140));
pDC->SetTextColor(RGB(25, 255, 255));
pDC->SetBkColor(RGB(143, 159, 140));
return hbr;
}
HBRUSH CCDateTimeCtrlDlg::_CDateTimeCtrl::CtlColor(CDC* pDC, UINT nCtlColor)
{
COLORREF clTextColor = m_pSiteUIComponent->m_clTextColor;
pDC->SetTextColor(clTextColor);
//HBRUSH newHbr = CreateSolidBrush( clTextColor );
//return newHbr;
//m_cBrush.DeleteObject();
//m_cBrush.CreateSolidBrush(clTextColor);
m_cBrush.CreateSolidBrush(RGB(255, 0, 255));
CRect rect;
pDC->SelectObject(&m_cBrush);
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->FillRect(rect, &m_cBrush);
pDC->SetBkColor(RGB(143, 159, 140));
// ::SetBkColor(pDC, RGB(255, 0, 255));
return (HBRUSH)(m_cBrush.GetSafeHandle());
}
....
void CCDateTimeCtrlDlg::OnBnClickedButton1()
{
if (m_pInnerDateTimeCtrl->GetSafeHwnd() != NULL)
{
{
m_pInnerDateTimeCtrl->SetFocus();
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
keybd_event(VK_DOWN, MapVirtualKey(VK_DOWN, 0), 0, 0);
keybd_event(VK_DOWN, MapVirtualKey(VK_DOWN, 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
}
{
//SendMessage(m_pInnerDateTimeCtrl->GetSafeHwnd(), WM_LBUTTONDOWN, MK_LBUTTON, m_pInnerDateTimeCtrl->Width - 5);
}
}
// TODO: 在此添加控件通知处理程序代码
DWORD dwStyle(DTS_SHORTDATEFORMAT);
// DTS_SHORTDATEFORMAT;
//DTS_LONGDATEFORMAT;
//DTS_TIMEFORMAT | DTS_UPDOWN; // 对于时间方式,要显示带有上下调整时间的小按钮。默认方式是日历方式
//DTS_TIMEFORMAT | DTS_UPDOWN; // 对于自定义时间方式,要显示带有上下调整时间的小按钮。默认方式是日历方式
CRect rcBounds(100, 150, 400, 200);
m_pInnerDateTimeCtrl = new _CDateTimeCtrl(this);
BOOL bResult = m_pInnerDateTimeCtrl->Create(
WS_CHILD | WS_VISIBLE | WS_TABSTOP | dwStyle,
rcBounds,
this,
0
);
if (!bResult)
{
MessageBox(_T("error"));
}
return;
}
胡乱试验了一通,还是没有实现,不知道应该怎么弄。
参考:
https://blog.csdn.net/u010679316/article/details/80127501
https://blog.csdn.net/liumenghappy/article/details/6475628
https://bbs.csdn.net/topics/30433378?list=160112
https://blog.csdn.net/xuheazx/article/details/16856525
https://bbs.csdn.net/topics/40237067
不是很通用的方法,屏蔽以下语句:
#pragma comment(linker,"/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'/"")
还有个想象可行的方法就是
#include "uxtheme.h"
#pragma comment(lib, "uxtheme.lib")
//调用创建后调用SetWindowTheme
BOOL bResult = m_pInnerDateTimeCtrl->Create(
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS | dwStyle,
rcBounds,
this,
101
);
HRESULT result = ::SetWindowTheme(m_pInnerDateTimeCtrl->m_hWnd, L"", L"");
但是实际没有生效,不知道什么原因
如果想要下拉月历窗口背景等颜色可修改,可以试试
SetThemeAppProperties(0/*dwFlags*/);