⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttonbt.cpp

📁 2410下面的外部中断的测试程序 此代码包含驱动和应用程序
💻 CPP
字号:
// ButtonBT.cpp : implementation file
//

#include "stdafx.h"

#include "ButtonBT.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CButtonBT

CButtonBT::CButtonBT()
{
	m_bBkTransparent=true;
	m_bIsFlat=true;
	m_bHasFocus=false;
	m_bIsEnabled=true;
	m_bMove=false;
	m_bDCStored=false;
	m_bText=false;
	m_strText=_T("");
	m_hMouseInIcon=NULL;
	m_hNormalIcon=NULL;
	m_IconAlign=AL_LEFT;
	m_DrawState=ST_MOVEOUT;
	m_IconYPos=5;
	m_IconXPos=5;
	m_cxText=0;
	m_cyText=0;
	m_nIconX=0;
	m_nIconY=0;
	m_bUseIcon=false;
	m_clNormalText=::GetSysColor(COLOR_WINDOWTEXT);
	m_clActiveText=::GetSysColor(COLOR_HIGHLIGHT);
	m_clMouseDownText=::GetSysColor(COLOR_WINDOWTEXT);
	m_bkColor=::GetSysColor(COLOR_BTNFACE);
	m_clText=m_clNormalText;
}

CButtonBT::~CButtonBT()
{

}


BEGIN_MESSAGE_MAP(CButtonBT, CButton)
	//{{AFX_MSG_MAP(CButtonBT)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtonBT message handlers

void CButtonBT::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{                        
	// TODO: Add your code to draw the specified item
	if(lpDIS->itemState & ODS_DISABLED )///无效
	{
		m_bIsEnabled=false;
		m_DrawState=ST_MOVEOUT;
	}
	if(!(lpDIS->itemState & ODS_DISABLED) )///有效
	{
		m_bIsEnabled=true;
	}
	if(lpDIS->itemState & ODS_FOCUS)///焦点
	{
		m_bHasFocus=true;
	}
	if( !(lpDIS->itemState & ODS_FOCUS) )///无焦点
	{
		m_bHasFocus=false;
	}
	Draw();


}

void CButtonBT::PreSubclassWindow() 
{
	SetButtonStyle(GetButtonStyle()|BS_OWNERDRAW);
}



void CButtonBT::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_DrawState=ST_MOUSEDOWN;
    Draw();
	CButton::OnLButtonDown(nFlags, point);
}

void CButtonBT::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (nFlags & MK_LBUTTON)
		return;
	if(!m_bMove && m_bIsEnabled)
	{
		m_bMove=true;
		m_DrawState=ST_MOVEIN;
		Draw();
		SetTimer(1,10,NULL);
	}
	CButton::OnMouseMove(nFlags, point);
}

BOOL CButtonBT::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_hCursor != NULL)
	{
		::SetCursor(m_hCursor);
		return TRUE;
	}	
	return CButton::OnSetCursor(pWnd, nHitTest, message);
}

void CButtonBT::Draw()
{
	Refresh();///刷新
	DrawBorders();///画边框
	DrawIcon();
	DrawText();
}

void CButtonBT::DrawBorders()
{
	CClientDC	dc(this);
	CRect	clientRect;
	GetClientRect(&clientRect);
	COLORREF  clBtnHiLight=RGB(255,255,255);///::GetSysColor(COLOR_HIGHLIGHT);
	COLORREF  clBtnShadow=::GetSysColor(COLOR_BTNSHADOW);
	COLORREF  clBtnBlack=RGB(0,0,0);

	if(!m_bIsFlat)
	{
		// Draw a ordinary-look button.普通按钮
		CRect  drawRect=clientRect;
		switch(m_DrawState)
		{
			case ST_MOUSEUP:
			case ST_MOVEOUT:
			case ST_MOVEIN:///光标在区域内
				dc.Draw3dRect (&drawRect,RGB(255,255,255),RGB(0,0,0));
					drawRect.top+=1;
					drawRect.bottom=clientRect.bottom-1;
					drawRect.left+=1;
					drawRect.right=clientRect.right-1;
				dc.Draw3dRect (&drawRect,RGB(192,192,192),RGB(128,128,128));
				/*dc.Draw3dRect (&drawRect,clBtnHiLight,clBtnBlack);
				drawRect.DeflateRect(1, 1);
				dc.Draw3dRect (&drawRect,clBtnHiLight,clBtnShadow);
				drawRect.InflateRect(1, 1);*/
				break;
			case ST_MOUSEDOWN:///按下鼠标
				dc.Draw3dRect (&drawRect,RGB(128,128,128),RGB(192,192,192));
					drawRect.top+=1;
					drawRect.bottom=clientRect.bottom-1;
					drawRect.left+=1;
					drawRect.right=clientRect.right-1;
				dc.Draw3dRect (&drawRect,RGB(0,0,0),RGB(255,255,255));
				/*dc.Draw3dRect (&drawRect,clBtnBlack,clBtnHiLight);
				drawRect.DeflateRect(1, 1);
				dc.Draw3dRect (&drawRect,clBtnShadow,clBtnHiLight);
				drawRect.InflateRect(1, 1);*/
				break;
			default:
				break;
		}
		if(m_bHasFocus)
		{
		    CBrush br(RGB(0,0,0));  
			dc.FrameRect(&drawRect, &br);
			CPen  pen(PS_SOLID,0,clBtnBlack);
			dc.SelectObject (&pen);
			dc.MoveTo(drawRect.right-2,drawRect.top );
			dc.LineTo(drawRect.right-2,drawRect.bottom-2 );
			dc.LineTo(drawRect.left,drawRect.bottom-2 );
			drawRect.DeflateRect(4, 4);
			dc.DrawFocusRect(&drawRect);
		}
	}
	else
	// We'll draw a flat button.
	{
		if(!m_bIsEnabled)
			return;
		switch(m_DrawState)
		{
			case ST_MOUSEUP:
			case ST_MOVEIN:
				dc.Draw3dRect (&clientRect,RGB(255,255,255),RGB(0,0,0));
					clientRect.top+=1;
					clientRect.bottom=clientRect.bottom-1;
					clientRect.left+=1;
					clientRect.right=clientRect.right-1;
				dc.Draw3dRect (&clientRect,RGB(192,192,192),RGB(128,128,128));
				break;
			case ST_MOUSEDOWN:
				dc.Draw3dRect (&clientRect,RGB(128,128,128),RGB(192,192,192));
					clientRect.top+=1;
					clientRect.bottom=clientRect.bottom-1;
					clientRect.left+=1;
					clientRect.right=clientRect.right-1;
				dc.Draw3dRect (&clientRect,RGB(0,0,0),RGB(255,255,255));
				break;
			default:
				break;
		}
	}
}

void CButtonBT::Refresh()
{
	CRect	rect;
	GetClientRect(&rect);	
	CClientDC  dc(this);
	if(m_bBkTransparent==false)
		dc.FillRect(&rect,&CBrush(m_bkColor) );//::GetSysColor(COLOR_BTNFACE)
	else
		dc.FillRect(&rect,&CBrush(RGB(231,231,247)) );
		//dc.BitBlt (0,0,rect.Width (),rect.Height (),&memDC,0,0,SRCCOPY);
}

void CButtonBT::DrawIcon()
{
	if(m_bIsEnabled)
	{
		switch(m_DrawState)
		{
			case ST_MOVEIN:
			case ST_MOUSEDOWN:
			case ST_MOUSEUP:
				{
					if(m_hMouseInIcon!=NULL)
					{
						DrawIcon(m_hMouseInIcon);
					}
				}
				break;
			case ST_MOVEOUT:
				{
					if(m_hNormalIcon!=NULL)
						DrawIcon(m_hNormalIcon);
				}
				break;
			default:
				break;
		}
	}
	else
	{
	    DrawIcon(m_hNormalIcon,TRUE);
	}
}

void CButtonBT::DrawIcon(HICON hIcon, BOOL bDisabled)
{
	CRect	clientrect;
	CClientDC	dc(this);
	int cx=::GetSystemMetrics(SM_CXICON);
	int cy=::GetSystemMetrics(SM_CYICON);
//	int cx=::GetSystemMetrics(SM_CXICONSPACING);
//	int cy=::GetSystemMetrics(SM_CYICONSPACING);
	int x,y; // the icon's output position.
	int rectwidth,rectheight;
	GetClientRect(&clientrect);
	rectwidth=clientrect.Width ();
	rectheight=clientrect.Height ();

	CString		itemString;
	GetWindowText(itemString);

	if(itemString!="")
	{
		switch(m_IconAlign)
		{
			case AL_LEFT:
			// draw the icon left.
			{
				x=m_IconXPos;
				y=(rectheight-cy-m_nIconY)/2;
				break;
			}
			case AL_TOP:
			{
				x=(rectwidth-cx-m_nIconX)/2;
				y=m_IconYPos;
				break;
			}
			default:
				break;
		}
	}
	else
	// If there is not any text,we'll center the icon.
	{
		x=(rectwidth-cx)/2;
		y=(rectheight-cy)/2;
	}
	// Draw the icon,as if it is pressed
	if(m_DrawState==ST_MOUSEDOWN)
	{
		x++;
		y++;
	}
	// Draw the icon.
	if(!bDisabled)
	{
	    dc.DrawIcon (x,y,hIcon);
	}
	else
	{
    //	dc.DrawState(CPoint(x,y),CSize(32,32),hIcon,DST_ICON | DSS_DISABLED,(HBRUSH)NULL);
	}
}

void CButtonBT::DrawText()
{
	CString		itemString;
	CRect  clientRect;
	CClientDC  dc(this);
	CFont m_fntStatic;

	m_fntStatic.CreateFont(20,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
			CLIP_STROKE_PRECIS,DRAFT_QUALITY,VARIABLE_PITCH | FF_SWISS,_T("Arial"));
	CFont* pOldFont=dc.SelectObject(&m_fntStatic);
	GetClientRect(&clientRect);
	GetWindowText(itemString);
	// Draw text depend on different state.
	if(m_bText)itemString=m_strText;
	if(itemString)
	{
		// First Get some necessary infomation.
		CSize size=dc.GetTextExtent (itemString);
		int rectwidth=clientRect.Width();
		int rectheight=clientRect.Height();
		int textwidth=size.cx ;
		int textheight=size.cy ;
		int iconwidth=::GetSystemMetrics(SM_CXICON);
		int iconheight=::GetSystemMetrics(SM_CYICON);
		int x,y; // the position of the text.
		// Next caculate the text output position.
		switch(m_IconAlign)
		{
			case AL_LEFT:
			{
				if(m_bUseIcon==true)
				{
					x=m_IconXPos+iconwidth+
						(rectwidth-textwidth-m_IconXPos-iconwidth)/2;
				}
				else
				{
					x=(rectwidth-textwidth)/2;
				}
				y=(rectheight-textheight)/2;
				break;
			}
			case AL_TOP:
			{
				if(m_bUseIcon==true)
				{
					y=m_IconYPos+iconheight+
						(rectheight-textheight-m_IconYPos-iconheight)/2;
				}
				else
				{
					y=(rectheight-textheight)/2;
				}
				x=(rectwidth-textwidth)/2;
				break;
			}
			default:
				break;
		}
		// Should we draw the text as if it is disabled?
		if(!m_bIsEnabled)
		{
			// .. Yes.Now draw the text disabled.
		///	dc.DrawState(CPoint(x,y),CSize(0,0),itemString,DST_TEXT | DSS_DISABLED,FALSE,0,(HBRUSH)NULL);
		}
		else
		{
			switch(m_DrawState)
			{
				case ST_MOVEIN:
				{
				//	m_clText=m_clActiveText;
					m_clText=m_clNormalText;
				}
					break;
				case ST_MOVEOUT:
				{
					m_clText=m_clNormalText;
				}
					break;
				case ST_MOUSEUP:
				{
				//	m_clText=m_clActiveText;
					m_clText=m_clNormalText;
				}
					break;
				case ST_MOUSEDOWN:
				{
					m_clText=m_clNormalText;
				}
					break;
				default:
					break;
			}
			// Draw the text,as if it is pressed
			if(m_DrawState==ST_MOUSEDOWN)
			{
			//	x++;
			//	y++;
				x=1;
				y=1;
				clientRect.OffsetRect(x,y);
			}
			dc.SetTextColor(m_clText);
			dc.SetBkMode(TRANSPARENT);
			//clientRect.OffsetRect(m_cxText,m_cyText);//10,58
			if (m_cxText==0)
				dc.DrawText (itemString,itemString.GetLength(),clientRect,
					 DT_CENTER  | DT_VCENTER | DT_SINGLELINE);//| DT_BOTTOM 
			else if(m_cxText!=0 && m_cyText!=0)
			{
				clientRect.OffsetRect(m_cxText,m_cyText);//10,58
				dc.DrawText (itemString,itemString.GetLength(),clientRect,DT_SINGLELINE);//DT_VCENTER |
			}
			else 
			{	
				clientRect.OffsetRect(m_cxText,m_cyText);//10,58
				dc.DrawText (itemString,itemString.GetLength(),clientRect,
					 DT_VCENTER  | DT_SINGLELINE);//DT_VCENTER |
			}

		}
	}
	dc.SelectObject(pOldFont);
	m_fntStatic.DeleteObject();
}





void CButtonBT::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
		m_DrawState=ST_MOUSEUP;

/*	CString  str;
	str.Format ("%d",m_IconXPos);
	MessageBox(str);
*/
    Draw();

	CButton::OnLButtonUp(nFlags, point);
}

void CButtonBT::SetActiveTextColor(COLORREF color)
{
	m_clActiveText=color;
	Draw();
}

void CButtonBT::SetAlign(int nAlign)
{
	m_IconAlign=nAlign;
	Draw();
}

void CButtonBT::SetBkMode(bool bTransparent)
{
	m_bBkTransparent=bTransparent;
}



void CButtonBT::SetIcon(UINT nNormal, UINT nMouseIn)
{	m_hNormalIcon=AfxGetApp()->LoadIcon(nNormal);
	m_hMouseInIcon=AfxGetApp()->LoadIcon(nMouseIn);
	m_bUseIcon=true;
	
}

void CButtonBT::SetIcon(HICON hNormal, HICON hMouseIn)
{
	m_hNormalIcon=hNormal;
	m_hMouseInIcon=hMouseIn;
	m_bUseIcon=true;
}

void CButtonBT::SetIconXPos(int x)
{

	m_IconXPos=x;
	Draw();
}

void CButtonBT::SetIconYPos(int y)
{	m_IconYPos=y;
	Draw();

}

void CButtonBT::SetMouseDownTextColor(COLORREF color)
{
	m_clMouseDownText=color;
	Draw();
}

void CButtonBT::SetNormalTextColor(COLORREF color)
{
	m_clNormalText=color;
	Draw();
}

void CButtonBT::SetStyle(bool bIsflat)
{
	m_bIsFlat=bIsflat;

}

void CButtonBT::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
		if(nIDEvent!=1)
		return;
	CPoint	point;
	CRect	rect;
	GetWindowRect(&rect);	
	GetCursorPos(&point);
	// If the mouse left our button,redraw it.
	if (!rect.PtInRect(point) && m_bMove && m_bIsEnabled)
	{
		KillTimer (1);
		m_DrawState=ST_MOVEOUT;
		m_bMove=false;
		Draw();
	}
	CButton::OnTimer(nIDEvent);
}

BOOL CButtonBT::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
		if(!m_bDCStored)
	{
    	CRect  clientRect;
	    GetClientRect(&clientRect);
	    int cx=clientRect.Width ();
	    int cy=clientRect.Height ();
		// Store our orignal DC.
		CBitmap	bitmap;
		memDC.CreateCompatibleDC (pDC);
		bitmap.CreateCompatibleBitmap (pDC,cx,cy);
		memDC.SelectObject (&bitmap);
		memDC.BitBlt (0,0,cx,cy,pDC,0,0,SRCCOPY);
		m_bDCStored=true;
	}
	// We'll not erase our button's back ground.
	return TRUE;
///	return CButton::OnEraseBkgnd(pDC);
}

void CButtonBT::SetTextXPos(int x)
{
	m_cxText=x;
	Draw();
}

void CButtonBT::SetTextYPos(int y)
{
	m_cyText=y;
	Draw();
}

void CButtonBT::SetIconPos(int x, int y)
{
	m_nIconX=x;
	m_nIconY=y;
	
}

void CButtonBT::SetBKColor(COLORREF bkColor, BOOL bRepaint)
{
	if(bRepaint==false)
	{
		m_bkColor=bkColor;
		m_bBkTransparent=false;
	}
	else
	{
		m_bkColor=::GetSysColor(COLOR_BTNFACE);
		m_bBkTransparent=false;
	}
	
}



void CButtonBT::SetModel(COLORREF color,UINT nNormal, UINT nMouseIn)
{

	SetBKColor(color,false);
	SetIcon(nNormal,nMouseIn);

}

void CButtonBT::SetTextModel(bool bModel, CString str)
{
	if(bModel)
	{
		m_bText=true;	
		m_strText=str;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -