📄 jnbmpbutton.cpp
字号:
// JnBmpButton.cpp : implementation file
//
#include "stdafx.h"
#include "Prog.h"
#include "JnBmpButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// JnBmpButton
JnBmpButton::JnBmpButton()
{
m_nType = 0;
nMouseState=0;
m_bMouseCaptured=FALSE;
m_bSetFocus=FALSE;
sTitle="";
crNormal=RGB(0,0,0);
crActive=RGB(0,0,255);
bEnableWnd=TRUE;
p_bmpIn=NULL; //没有获得焦点,鼠标进入的弹起图象
p_bmpOut=NULL; //失去焦点时的背景图象
p_bmpDown=NULL; //按下时的背景图象
p_bmpFalse=NULL; //不能用时候的图象
::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
m_font.CreateFontIndirect(&m_lf);
bHS=TRUE;
}
JnBmpButton::~JnBmpButton()
{
}
BEGIN_MESSAGE_MAP(JnBmpButton, CButton)
//{{AFX_MSG_MAP(JnBmpButton)
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
ON_WM_CTLCOLOR()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// JnBmpButton message handlers
BOOL JnBmpButton::OnEraseBkgnd(CDC* pDC)
{
CFont* pOldFont;
CRect rc;
GetClientRect(rc);
int x = 0, y = 0;
if(sTitle=="")
GetWindowText(sTitle);
CSize s;
switch(nMouseState)
{
case 0:
if(p_bmpOut==NULL)
{
if(m_bmpOut.m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpOut.GetWidth()) / 2;
y = (rc.Height() - m_bmpOut.GetHeight()) / 2;
m_bmpOut.DrawDIB(pDC, x, y);
pOldFont=pDC->SelectObject(&m_font);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crNormal);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
else
{
if(p_bmpOut->m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - p_bmpOut->GetWidth()) / 2;
y = (rc.Height() - p_bmpOut->GetHeight()) / 2;
p_bmpOut->DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crNormal);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
break;
case 1:
if(p_bmpIn==NULL)
{
if(m_bmpIn.m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpIn.GetWidth()) / 2;
y = (rc.Height() - m_bmpIn.GetHeight()) / 2;
m_bmpIn.DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
else
{
if(p_bmpIn->m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - p_bmpIn->GetWidth()) / 2;
y = (rc.Height() - p_bmpIn->GetHeight()) / 2;
p_bmpIn->DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
break;
case 2:
if(p_bmpDown==NULL)
{
if(m_bmpDown.m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpDown.GetWidth()) / 2;
y = (rc.Height() - m_bmpDown.GetHeight()) / 2;
m_bmpDown.DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
else
{
if(p_bmpDown->m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - p_bmpDown->GetWidth()) / 2;
y = (rc.Height() - p_bmpDown->GetHeight()) / 2;
p_bmpDown->DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
break;
case 3:
if(p_bmpOut==NULL)
{
if(m_bmpOut.m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpOut.GetWidth()) / 2;
y = (rc.Height() - m_bmpOut.GetHeight()) / 2;
m_bmpOut.DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
else
{
if(p_bmpOut->m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - p_bmpOut->GetWidth()) / 2;
y = (rc.Height() - p_bmpOut->GetHeight()) / 2;
p_bmpOut->DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crActive);
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
break;
case 4:
if(p_bmpFalse==NULL)
{
if(m_bmpFalse.m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - m_bmpFalse.GetWidth()) / 2;
y = (rc.Height() - m_bmpFalse.GetHeight()) / 2;
m_bmpFalse.DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(128,128,128));
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
else
{
if(p_bmpFalse->m_bImage)
{
CButton::OnEraseBkgnd(pDC);
x = (rc.Width() - p_bmpFalse->GetWidth()) / 2;
y = (rc.Height() - p_bmpFalse->GetHeight()) / 2;
p_bmpFalse->DrawDIB(pDC, x, y);
if(bHS)
rc.top=(int)(rc.Height()/2-6);
else
{
s=pDC->GetTextExtent(sTitle);
if(s.cx<rc.Height()) //如果文本的高度短于描绘的高度
rc.top=(int)((rc.Height()-s.cx)/2)+6;
else
rc.top=6;
}
pOldFont=pDC->SelectObject(&m_font);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(128,128,128));
pDC->DrawText(sTitle,rc,DT_CENTER+DT_VCENTER);
pDC->SelectObject(pOldFont);
}
else
return CButton::OnEraseBkgnd(pDC);
}
break;
}
return TRUE;
}
BOOL JnBmpButton::SetBitmap(UINT nOut,UINT nIn,UINT nDown, UINT nFalse)
{
if(nOut==0 || nIn==0 || nDown==0)
return FALSE;
if(!m_bmpOut.Load(nOut))
return FALSE;
if(!m_bmpIn.Load(nIn))
return FALSE;
if(!m_bmpDown.Load(nDown))
return FALSE;
if(!m_bmpFalse.Load(nFalse))
return FALSE;
return TRUE;
}
BOOL JnBmpButton::SetBitmap(CString StrOut, CString StrIn,CString StrDown, CString StrFalse)
{
if(!m_bmpOut.Load(StrOut))
return FALSE;
if(!m_bmpIn.Load(StrIn))
return FALSE;
if(!m_bmpDown.Load(StrDown))
return FALSE;
if(!m_bmpFalse.Load(StrFalse))
return FALSE;
return TRUE;
}
void JnBmpButton::OnMouseMove(UINT nFlags, CPoint point)
{
CButton::OnMouseMove(nFlags,point);
if (!m_bMouseCaptured || GetCapture()!=this)
{
SetCapture();
m_bMouseCaptured=TRUE;
OnMouseEnter(nFlags,point);
}
else
{
CRect rc;
GetClientRect(&rc);
if (!rc.PtInRect(point))
{
OnMouseLeave(nFlags,point);
m_bMouseCaptured=FALSE;
ReleaseCapture();
}
}
}
void JnBmpButton::OnLButtonDown(UINT nFlags, CPoint point)
{
CButton::OnLButtonDown(nFlags, point);
if(!CheckEnable())
return;
if(nMouseState!=2)
{
nMouseState=2;
Invalidate();
}
}
void JnBmpButton::OnMouseLeave(UINT nFlags, CPoint point)
{
if(!CheckEnable())
return;
if(!m_bSetFocus)
{
if(nMouseState!=0)
{
nMouseState=0;
Invalidate();
}
}
else
{
if(nMouseState!=3)
{
nMouseState=3;
Invalidate();
}
}
}
void JnBmpButton::OnMouseEnter(UINT nFlags, CPoint point)
{
if(!CheckEnable())
return;
switch(nFlags)
{
case 0:
if(nMouseState!=1)
{
nMouseState=1;
Invalidate();
}
break;
case 1:
if(nMouseState!=2)
{
nMouseState=2;
Invalidate();
}
break;
}
}
void JnBmpButton::OnSetFocus(CWnd* pOldWnd)
{
if(!CheckEnable())
return;
CButton::OnSetFocus(pOldWnd);
m_bSetFocus=TRUE;
if(m_bMouseCaptured)
{
if(nMouseState!=1)
{
nMouseState=1;
Invalidate();
}
}
else
{
if(nMouseState!=3)
{
nMouseState=3;
Invalidate();
}
}
}
void JnBmpButton::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
m_bSetFocus=FALSE;
if(nMouseState!=0)
{
nMouseState=0;
Invalidate();
}
if(!CheckEnable())
return;
}
void JnBmpButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CButton::OnKeyDown(nChar, nRepCnt, nFlags);
if(!CheckEnable())
return;
if(nMouseState!=2)
{
nMouseState=2;
Invalidate();
}
}
void JnBmpButton::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CButton::OnKeyUp(nChar, nRepCnt, nFlags);
if(!CheckEnable())
return;
if(nMouseState!=3)
{
nMouseState=3;
Invalidate();
}
}
void JnBmpButton::SetText(CString &str)
{
sTitle=str;
}
void JnBmpButton::SetTextColor(COLORREF color1, COLORREF color2)
{
crNormal=color1;
crActive=color2;
}
void JnBmpButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
}
void JnBmpButton::PreSubclassWindow()
{
SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
BOOL JnBmpButton::EnableWindow(BOOL bEnable)
{
if(!bEnable)
nMouseState=4;
else
nMouseState=0;
bEnableWnd=bEnable;
Invalidate();
return CButton::EnableWindow(bEnable);
}
BOOL JnBmpButton::CheckEnable()
{
if(!bEnableWnd)
{
if(nMouseState!=4)
{
nMouseState=4;
Invalidate();
}
return FALSE;
}
else
return TRUE;
}
BOOL JnBmpButton::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==13)
{
SendMessage(WM_LBUTTONDOWN);
SendMessage(WM_LBUTTONUP);
return TRUE;
}
}
return CButton::PreTranslateMessage(pMsg);
}
void JnBmpButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// AfxMessageBox("Up");
CButton::OnLButtonUp(nFlags, point);
}
void JnBmpButton::Copy(JnBmpButton &buttonSet)
{
p_bmpIn=&buttonSet.m_bmpIn;
p_bmpOut=&buttonSet.m_bmpOut;
p_bmpDown=&buttonSet.m_bmpDown;
p_bmpFalse=&buttonSet.m_bmpFalse;
}
void JnBmpButton::SetTextNormalColor(COLORREF color1)
{
crNormal=color1;
Invalidate();
}
void JnBmpButton::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
ASSERT(bCreated);
SetFont(&m_font);
}
void JnBmpButton::SetFontName(const CString &strFont)
{
strcpy(m_lf.lfFaceName,strFont);
ReconstructFont();
RedrawWindow();
}
void JnBmpButton::SetFontSize(int nSize)
{
nSize=-theApp.jns.DoubleToInt(nSize*1.333);
m_lf.lfHeight = nSize;
ReconstructFont();
RedrawWindow();
}
void JnBmpButton::SetText(CString str)
{
sTitle=str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -