MFC 日期时间控件CDateTimeCtrl自绘
清泛原创
先看效果:
方法同ComboBox自绘(https://www.tsingfun.com/down/code/100.html),采用图片拼接的方式,本例实现较基础仍有细节待完善。
部分代码如下,需自行调整:
MyDateTime.h
方法同ComboBox自绘(https://www.tsingfun.com/down/code/100.html),采用图片拼接的方式,本例实现较基础仍有细节待完善。
部分代码如下,需自行调整:
MyDateTime.h
#pragma once
#include <vector>
#include "../Resource.h"
/////////////////////////////////////////////////////////////////////////////
// CMyDateTime window
class CMyDateTime : public CDateTimeCtrl
{
// Construction
public:
CMyDateTime();
// Attributes
private:
//-------------Combo Box image scheme-----------------------------
// -------------------------------------------------------
// | m_nIDLeft -----m_nIDCenter----- |m_nIDRight|
// -------------------------------------------------------
//-----------------------------------------------------------------
UINT m_nIDLeft, m_nIDRight, m_nIDCenter; //Resource IDs for ComboBox
// Implementation
public:
void SetComboBitmap(UINT nIDLeft, UINT nIDRight, UINT nIDCenter);
virtual ~CMyDateTime();
private:
CFont* m_pfDefault;
// Generated message map functions
protected:
//{{AFX_MSG(CMyDateTime)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
MyDateTime.cpp
#include "stdafx.h"
#include "MyDateTime.h"
#include "../CommonFunc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CMyDateTime::CMyDateTime()
{
m_nIDLeft = m_nIDRight = m_nIDCenter = 0;
//m_nIDListLeft, m_nIDListRight, m_nIDListTop, m_nIDListBot = 0;
m_pfDefault = ::GetFont(_T("微软雅黑"), 10);
//默认图片初始化
SetComboBitmap(IDB_COMBO_LEFT,IDB_COMBO_RIGHT,IDB_COMBO_CEN);
}
CMyDateTime::~CMyDateTime()
{
SAFEDELETE(m_pfDefault)
}
BEGIN_MESSAGE_MAP(CMyDateTime, CDateTimeCtrl)
//{{AFX_MSG_MAP(CMyDateTime)
ON_WM_PAINT()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDateTime message handlers
#define SET_ZERO(X) memset((void *)&X,0,sizeof(X));
void CMyDateTime::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SelectObject(m_pfDefault);
if(IsWindowEnabled())
dc.SetTextColor(RGB(40,40,40));
else
dc.SetTextColor(RGB(174,174,174));
RECT rc;
CDC MemDC;
GetClientRect(&rc);
MemDC.CreateCompatibleDC(&dc);
CBitmap bmpComboRight,bmpComboLeft, bmpComboCenter,bmpComboBot;
BITMAP bitRight, bitLeft, bitCenter;
SET_ZERO(bitRight); SET_ZERO(bitLeft); SET_ZERO(bitCenter);
if( m_nIDLeft )
{
bmpComboLeft.LoadBitmap(m_nIDLeft);
bmpComboLeft.GetBitmap(&bitLeft);
MemDC.SelectObject(bmpComboLeft);
dc.BitBlt(rc.left,rc.top,bitLeft.bmWidth,bitLeft.bmHeight,&MemDC,0,0,SRCCOPY);
}
if( m_nIDRight )
{
bmpComboRight.LoadBitmap(m_nIDRight);
bmpComboRight.GetBitmap(&bitRight);
MemDC.SelectObject(bmpComboRight);
dc.BitBlt(rc.right-bitRight.bmWidth,rc.top,bitRight.bmWidth,bitRight.bmHeight,&MemDC,0,0,SRCCOPY);
}
if( m_nIDCenter )
{
bmpComboCenter.LoadBitmap(m_nIDCenter);
bmpComboCenter.GetBitmap(&bitCenter);
MemDC.SelectObject(bmpComboCenter);
dc.StretchBlt(rc.left+bitLeft.bmWidth,rc.top,rc.right-bitLeft.bmWidth-bitRight.bmWidth,bitCenter.bmHeight,&MemDC,0,0,bitCenter.bmWidth,bitCenter.bmHeight,SRCCOPY);
}
CString sz;
GetWindowText(sz);
rc.left += 5; //左边距
rc.top += 1; //上边距
dc.SetBkMode(TRANSPARENT);
dc.DrawText(sz,&rc,DT_VCENTER | DT_SINGLELINE);
}
void CMyDateTime::SetComboBitmap(UINT nIDLeft, UINT nIDRight, UINT nIDCenter)
{
m_nIDCenter = nIDCenter; m_nIDLeft = nIDLeft; m_nIDRight = nIDRight;
}
其中图片就是左中右三个截图啦:注意:GetMonthCalCtrl() 用于获得月历控件的窗口句柄。
上一篇:Picture Control(图片控件)中静态显示位图,解决位图不显示的问题
下一篇:总结const_cast、static_cast、dynamic_cast、reinterpret_cast