這是一個按鈕自繪的框架,其他控件也類似
成都創新互聯公司服務項目包括紅橋網站建設、紅橋網站制作、紅橋網頁制作以及紅橋網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,紅橋網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到紅橋省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
//.h頭文件 #pragma once #include "afxwin.h" #include "MemDC.h"//封裝內存DC類 class CYuButton :public CWnd { private: BOOL m_bIsDown; BOOL m_bIsMove; BOOL _bMouseTrack; CString m_sCaption; CFont *m_pFont; INT m_nFocusPic;//焦點圖片 INT m_nForePic; //前景圖片 CMemoryDC m_Icon_MDC; CMemoryDC m_focus_MDC; CMemoryDC m_fore_MDC; CMemoryDC m_CompoundDC;//合成位圖DC bool m_bTracking = TRUE; public: DECLARE_DYNCREATE(CYuButton) CYuButton(void); virtual ~CYuButton(void); BOOL Create(LPCTSTR strCaption,const CRect &rt,CWnd * pParendWnd,UINT uId,UINT strFocusPic,UINT strForePic); afx_msg void OnNcPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnPaint(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnKillFocus(CWnd* pNewWnd); DECLARE_MESSAGE_MAP() //鼠標按下 void DrawDown(CDC * pDC); void DrawNormal(CDC * pDC); //鼠標移動 void DrawMove(CDC * pDC); //字體 void SetFont(CFont * pFont); CFont * GetFont(); //獲取當前程序路徑 CString GetApplicationPath(); public: //修改按鈕狀態 void SetNormalStatus(); afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point); afx_msg void OnMouseHover(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnMouseLeave(); afx_msg void OnSetFocus(CWnd* pOldWnd); afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); };
//.cpp文件 #include "StdAfx.h" #include "YuButton.h" CYuButton::CYuButton(void) { // WNDCLASS wd={CS_VREDRAW|CS_HREDRAW,::DefWindowProc}; // wd.lpszClassName = _T("YUButton"); // // AfxRegisterClass(&wd); m_bIsDown = FALSE; m_bIsMove = FALSE; _bMouseTrack = TRUE; } CYuButton::~CYuButton(void) { } BOOL CYuButton::Create(LPCTSTR strCaption,const CRect &rt,CWnd * pParendWnd, UINT uId,UINT strFocusPic,UINT strForePic) { LPCTSTR lpszClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | WS_TABSTOP, AfxGetApp()->LoadStandardCursor(IDC_ARROW), (HBRUSH)GetStockObject(LTGRAY_BRUSH), NULL); m_sCaption = strCaption; m_nFocusPic = strFocusPic; m_nForePic = strForePic; BOOL re = m_Icon_MDC.LoadBitmap(m_nForePic); re = m_focus_MDC.LoadBitmap(m_nFocusPic);//畫焦點圖片85*110 re = m_fore_MDC.LoadBitmap(m_nForePic); //顯示圖標 m_CompoundDC.Create(m_focus_MDC.Width(),m_focus_MDC.Height());//空白內存位圖 m_pFont = pParendWnd->GetFont(); return CWnd::Create(lpszClassName,strCaption,WS_VISIBLE|WS_CHILD,rt,pParendWnd,uId); } BOOL CYuButton::CreateEx(DWORD dwExStyle,LPCTSTR sCaption,DWORD dwStyle, CONST CRect & rt,CWnd * pParendWnd,UINT uId) { m_sCaption = sCaption; return CWnd::CreateEx(dwExStyle,_T("YUButton"),sCaption, dwStyle|WS_CHILD,rt,pParendWnd,uId); } IMPLEMENT_DYNCREATE(CYuButton, CWnd) BEGIN_MESSAGE_MAP(CYuButton, CWnd) ON_WM_ERASEBKGND() ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_KILLFOCUS() ON_WM_LBUTTONDBLCLK() ON_WM_MOUSEHOVER() ON_WM_MOUSEMOVE() ON_WM_MOUSELEAVE() ON_WM_SETFOCUS() ON_WM_KEYDOWN() END_MESSAGE_MAP() void CYuButton::OnNcPaint() { } BOOL CYuButton::OnEraseBkgnd(CDC* pDC) { return true;//CWnd::OnEraseBkgnd(pDC); } void CYuButton::OnPaint() { CPaintDC dc(this); // device context for painting if(m_bIsDown) DrawNormal(&dc); else DrawDown(&dc); //繪按鈕上面的文字 CRect rt; GetClientRect(&rt); rt.top = 69; dc.SetTextColor(RGB(0,120,215)); CFont font; VERIFY(font.CreatePointFont(90,_T("微軟雅黑"), &dc)); CFont * pOldFont = dc.SelectObject(&font); dc.SetBkMode(TRANSPARENT); rt.top = 40; dc.DrawText(m_sCaption,rt,DT_CENTER); dc.SelectObject(pOldFont); } void CYuButton::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 CWnd::OnLButtonDown(nFlags, point); m_bIsDown = TRUE; this->Invalidate(FALSE); SetFocus(); //TRACE(_T("點擊按鈕!\n")); } void CYuButton::OnLButtonUp(UINT nFlags, CPoint point) { CWnd::OnLButtonUp(nFlags, point); CWnd * pWnd = this->GetParent(); if (pWnd) pWnd->SendMessage(WM_COMMAND, GetDlgCtrlID(), (LPARAM)this->GetSafeHwnd()); } void CYuButton::DrawNormal(CDC * pDC) { //m_CompoundDC.BitBlt(0,0,m_focus_MDC.Width(),m_focus_MDC.Height(), //&m_focus_MDC,0,0,SRCCOPY); //m_fore_MDC.BitTrans(0,0,m_fore_MDC.Width(),m_fore_MDC.Height(), //&m_CompoundDC,0,0,RGB(135,200,241)); //pDC->BitBlt(0,0,m_focus_MDC.Width(),m_focus_MDC.Height(), //&m_CompoundDC,0,0,SRCCOPY); pDC->BitBlt(0, 0, m_Icon_MDC.Width(), m_Icon_MDC.Height(), &m_fore_MDC, 0, 0, SRCCOPY); } void CYuButton::DrawDown(CDC * pDC) { //畫小圖標 // CBrush br(RGB(221, 203, 255)); // CBrush * pOldBrush = pDC->SelectObject(&br); // CRect rect(0, 0, m_Icon_MDC.Width(), m_Icon_MDC.Height()); // pDC->FillRect(&rect,&br); pDC->BitBlt(0,0,m_Icon_MDC.Width(),m_Icon_MDC.Height(),&m_focus_MDC,0,0,SRCCOPY); //pDC->SelectObject(pOldBrush); } void CYuButton::SetFont(CFont * pFont) { m_pFont = pFont; } CFont * CYuButton::GetFont() { return m_pFont; } void CYuButton::OnKillFocus(CWnd* pNewWnd) { CWnd::OnKillFocus(pNewWnd); m_bIsDown = FALSE; Invalidate(TRUE); } CString CYuButton::GetApplicationPath() { WCHAR buff[255]={0}; ::GetModuleFileName(0,buff,255); CString strAppFullName; strAppFullName.Format(_T("%s"),buff); CString strAppPath = _T(""); strAppPath = strAppFullName.Left(strAppFullName.ReverseFind('\\')+1); return strAppPath; } void CYuButton::SetNormalStatus() { m_bIsDown = FALSE; Invalidate(FALSE); } void CYuButton::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 //CWnd * pWnd = this->GetParent(); //if (pWnd) //pWnd->SendMessage(WM_COMMAND, GetDlgCtrlID(), (LPARAM)this->GetSafeHwnd()); CWnd::OnLButtonDblClk(nFlags, point); } void CYuButton::OnMouseHover(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 m_bIsDown = TRUE; Invalidate(); CWnd::OnMouseHover(nFlags, point); } void CYuButton::OnMouseMove(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 TRACKMOUSEEVENT tme = { 0 }; tme.cbSize = sizeof(TRACKMOUSEEVENT); tme.dwFlags = TME_HOVER | TME_LEAVE; tme.dwHoverTime = 50; tme.hwndTrack = this->m_hWnd; if (TrackMouseEvent(&tme)) {} CWnd::OnMouseMove(nFlags, point); } void CYuButton::OnMouseLeave() { // TODO: 在此添加消息處理程序代碼和/或調用默認值 m_bIsDown = FALSE; Invalidate(); CWnd::OnMouseLeave(); } void CYuButton::OnSetFocus(CWnd* pOldWnd) { m_bIsDown = TRUE; Invalidate(); CWnd::OnSetFocus(pOldWnd); // TODO: 在此處添加消息處理程序代碼 } void CYuButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 CWnd::OnKeyDown(nChar, nRepCnt, nFlags); }
文章題目:VC自繪控件框架
網站地址:http://vcdvsql.cn/article18/peiggp.html
成都網站建設公司_創新互聯,為您提供App設計、網站導航、全網營銷推廣、網站維護、營銷型網站建設、網站改版
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯