gps/GPSProject/GPSMain/BmpTxtButton.cpp

216 lines
4.5 KiB
C++
Executable File

// BmpTxtButton.cpp : implementation file
//
#include "stdafx.h"
#include "GPSMain.h"
#include "BmpTxtButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBmpTxtButton
CBmpTxtButton::CBmpTxtButton()
{
m_NormalID = 0; // 按钮背景图像资源ID号 正常状态
m_DownID = 0; // 按钮背景图像资源ID号 按下
m_FoucsedID = 0; // 按钮背景图像资源ID号 具有焦点
m_DisableID = 0; // 按钮背景图像资源ID号 禁用
m_butDown = false; // 按钮被按下
// 无标题
m_Title.Empty();
// 标题颜色为黑色
m_TitleColor = RGB(255,255,255);
// 按钮样式为普通样式
m_State = TBS_NORMAL;
m_NormalDC.CreateCompatibleDC(NULL);
m_DownDC.CreateCompatibleDC(NULL);
m_txtFont.CreateFont(14,0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial"));
m_butStyle = DT_CENTER|DT_BOTTOM;
}
CBmpTxtButton::~CBmpTxtButton()
{
m_NormalDC.DeleteDC();
m_DownDC.DeleteDC();
m_txtFont.DeleteObject();
}
BEGIN_MESSAGE_MAP(CBmpTxtButton, CButton)
//{{AFX_MSG_MAP(CBmpTxtButton)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpTxtButton message handlers
void CBmpTxtButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
// 在此设置按钮样式为自绘按钮
ModifyStyle(0, GetStyle()|BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CBmpTxtButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
// 获取客户区矩形
CRect rect;
GetClientRect(&rect);
// 客户区绘图设备环境
CClientDC dc(this);
// 设置字体
dc.SelectObject(&m_txtFont);
// 背景透明
dc.SetBkMode(TRANSPARENT);
UINT status = lpDrawItemStruct->itemState;
// 是否按下
if (status & ODS_SELECTED)
{
if (m_DownID)
{
// 重绘背景
dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_DownDC,0,0,SRCCOPY);
}
// 设置字体颜色为反色
COLORREF color = RGB(255 - GetRValue(m_TitleColor),
255 - GetGValue(m_TitleColor),
255 - GetBValue(m_TitleColor));
dc.SetTextColor(color);
}
else
{
if (m_NormalID)
{
// 重绘背景
dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_NormalDC,0,0,SRCCOPY);
}
// 判断是否绘制按钮标题
if (!m_Title.IsEmpty())
{
// 设置标题颜色
dc.SetTextColor(m_TitleColor);
}
}
// 绘制标题
if (!m_Title.IsEmpty())
{
dc.DrawText(m_Title,&rect, m_butStyle);
}
if ((status & ODS_SELECTED) && theApp.GetBeep())
{
// 按下发声
if (theApp.GetBeep())
{
theApp.PlayMsgSound();
}
}
}
void CBmpTxtButton::LoadBitmaps(UINT nIDBitmapResource, UINT nIDBitmapResourceSel, UINT nIDBitmapResourceFocus, UINT nIDBitmapResourceDisabled)
{
m_NormalID = nIDBitmapResource;
m_DownID = nIDBitmapResourceSel;
m_FoucsedID = nIDBitmapResourceFocus;
m_DisableID = nIDBitmapResourceDisabled;
CBitmap bmp;
bmp.LoadBitmap(m_NormalID);
m_NormalDC.SelectObject(&bmp);
bmp.DeleteObject();
bmp.LoadBitmap(m_DownID);
m_DownDC.SelectObject(&bmp);
bmp.DeleteObject();
}
void CBmpTxtButton::SetButtonTitle(CString iTitle)
{
// 设置按钮标题
m_Title = iTitle;
}
void CBmpTxtButton::SetButtonState(int iState)
{
// 设置状态
m_State = iState;
}
int CBmpTxtButton::GetButtonState()
{
// 获取按钮状态
return m_State;
}
CString CBmpTxtButton::GetButtonTitle()
{
return m_Title;
}
bool CBmpTxtButton::IsButtonDown()
{
return m_butDown;
}
void CBmpTxtButton::SetButtonDown(bool bDown)
{
m_butDown = bDown;
}
BOOL CBmpTxtButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return FALSE;
//return CButton::OnEraseBkgnd(pDC);
}
void CBmpTxtButton::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rc;
GetClientRect(&rc);
// TODO: Add your message handler code here
dc.BitBlt(0,0,rc.Width(),rc.Height(),&m_NormalDC,0,0,SRCCOPY);
dc.SelectObject(m_txtFont);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(m_TitleColor);
// 绘制标题
if (!m_Title.IsEmpty())
{
// 居中,底部对齐显示标题
dc.DrawText(m_Title,&rc,m_butStyle);
}
// Do not call CButton::OnPaint() for painting messages
}
void CBmpTxtButton::SetTitleStyle(int iStyle)
{
m_butStyle = iStyle;
}