CButton重绘

2024-05-30 08:38
文章标签 重绘 cbutton

本文主要是介绍CButton重绘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MFC下一个简单的按钮重绘,功能不是那么全

#if !defined(AFX_BMPBUTTON_H__FBB70540_EC88_4021_9272_574C1A7A571B__INCLUDED_)
#define AFX_BMPBUTTON_H__FBB70540_EC88_4021_9272_574C1A7A571B__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// BmpButton.h : header file
///////////////////////////////////////////////////////////////////////////////
// CBmpButton windowclass CBmpButton : public CButton
{
// Construction
public:BOOL m_bNonToolBarBtn;UINT m_uiDisBmpID;UINT m_uiNorBmpID;CBmpButton();void SetNonToolBarBtn(BOOL bBtn);void SetBitmap(UINT nBmpID);void SetItemData(DWORD dwData){m_dwData=dwData;}void SetDisableBmp(UINT nBmpID){m_uiDisBmpID=nBmpID;}void SetNormalBmp(UINT nBmpID){m_uiNorBmpID=nBmpID;}
/*
OK                      可用    不可用                                       SetNorBmp     SetDisBmp
Select All              可用    可用
Zoomin                  可用    不可用      可用     不可用                   SetNorBmp     SetDisBmp         SetNorBmp     SetDisBmp
*/DWORD GetItemData(){return m_dwData;}
// Attributes
public:// Operations
public:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CBmpButton)public:virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);protected:virtual void PreSubclassWindow();afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);//}}AFX_VIRTUAL// Implementation
public:virtual ~CBmpButton();// Generated message map functions
protected://{{AFX_MSG(CBmpButton)afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);afx_msg void OnCaptureChanged(CWnd *pWnd);afx_msg void OnKillFocus(CWnd* pNewWnd);afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg void OnSize(UINT nType, int cx, int cy);afx_msg BOOL OnEraseBkgnd(CDC* pDC);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);//}}AFX_MSGvoid ResetWndRgn();DECLARE_MESSAGE_MAP()
private:BOOL    m_bIsTracking;BOOL    m_bIsHovering;HBITMAP m_hBitmap;BOOL    m_MouseOnButton;DWORD  m_dwData;
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_BMPBUTTON_H__FBB70540_EC88_4021_9272_574C1A7A571B__INCLUDED_)

实现

// BmpButton.cpp : implementation file
//#include "stdafx.h"
#include "BmpButton.h"
#include "resource.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifextern CFont* g_DisplayFont24;
/////////////////////////////////////////////////////////////////////////////
// CBmpButtonCBmpButton::CBmpButton()
{m_hBitmap=NULL;m_MouseOnButton=FALSE;m_dwData=0xFFFFFFFF;m_bIsTracking = FALSE;m_bIsHovering = FALSE;m_uiDisBmpID = -1;m_uiNorBmpID = -1;m_bNonToolBarBtn = FALSE;
}CBmpButton::~CBmpButton()
{if(m_hBitmap)::DeleteObject(m_hBitmap);
}
BEGIN_MESSAGE_MAP(CBmpButton, CButton)//{{AFX_MSG_MAP(CBmpButton)ON_WM_CTLCOLOR()ON_WM_CAPTURECHANGED()ON_WM_KILLFOCUS()ON_WM_MOUSEMOVE()ON_WM_SIZE()ON_WM_ERASEBKGND()ON_WM_SETCURSOR()ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CBmpButton message handlersHBRUSH CBmpButton::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
void CBmpButton::PreSubclassWindow() 
{ModifyStyle(0,BS_OWNERDRAW);    ResetWndRgn();CButton::PreSubclassWindow();
}
void CBmpButton::OnMouseMove(UINT nFlags, CPoint point)
{CWnd* pWnd;CWnd* pParent;if(!m_bIsTracking){TRACKMOUSEEVENT tme;tme.cbSize      = sizeof(tme);tme.hwndTrack   = m_hWnd;tme.dwFlags     = TME_LEAVE|TME_HOVER;tme.dwHoverTime = 1;m_bIsTracking = _TrackMouseEvent(&tme);}CButton::OnMouseMove(nFlags, point);if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;pWnd = GetActiveWindow();pParent = GetOwner();if ((GetCapture() != this) && (pWnd != NULL && pParent != NULL)) {m_MouseOnButton = TRUE;SetFocus();SetCapture();Invalidate();}else{CRect rc;GetClientRect(&rc);if(!rc.PtInRect(point)){if(m_MouseOnButton == TRUE){m_MouseOnButton = FALSE;Invalidate();}if(!(nFlags & MK_LBUTTON)) ReleaseCapture();}}
}LRESULT CBmpButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{m_bIsTracking = FALSE;m_bIsHovering = FALSE;Invalidate();return 0;
}LRESULT CBmpButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{m_bIsHovering = TRUE;Invalidate();return 0;
}void CBmpButton::OnKillFocus(CWnd * pNewWnd)
{CButton::OnKillFocus(pNewWnd);if(m_MouseOnButton == TRUE){m_MouseOnButton = FALSE;Invalidate();}
}
void CBmpButton::OnCaptureChanged(CWnd *pWnd) 
{if (m_MouseOnButton == TRUE){ReleaseCapture();Invalidate();}CButton::OnCaptureChanged(pWnd);
}
void CBmpButton::SetBitmap(UINT nBmpID)
{HINSTANCE   hInstResource   = NULL; hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBmpID), RT_BITMAP);if(m_hBitmap)::DeleteObject(m_hBitmap);m_hBitmap = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBmpID), IMAGE_BITMAP, 0, 0, 0);
}
void CBmpButton::ResetWndRgn()
{CRect rcItem;GetClientRect(&rcItem);
#define DOTS    5CPoint pt[8];pt[0].x=DOTS;               pt[0].y=0;pt[1].x=rcItem.right-DOTS;  pt[1].y=0;pt[2].x=rcItem.right;       pt[2].y=DOTS;pt[3].x=rcItem.right;       pt[3].y=rcItem.bottom-DOTS;pt[4].x=rcItem.right-DOTS;  pt[4].y=rcItem.bottom;pt[5].x=DOTS;               pt[5].y=rcItem.bottom;pt[6].x=rcItem.left;        pt[6].y=rcItem.bottom-DOTS;pt[7].x=rcItem.left;        pt[7].y=DOTS;HRGN hRgn=::CreatePolygonRgn(pt,8,ALTERNATE);
//  HRGN hRgn = CreateRoundRectRgn(0,0,rcItem.right-rcItem.left,rcItem.bottom-rcItem.top,50,50);SetWindowRgn(hRgn,TRUE);::DeleteObject(hRgn);
}void CBmpButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{HDC hDC          = lpDrawItemStruct->hDC;BOOL bIsPressed  = (lpDrawItemStruct->itemState & ODS_SELECTED);BOOL bIsFocused  = (lpDrawItemStruct->itemState & ODS_FOCUS);BOOL bIsDisabled = (lpDrawItemStruct->itemState & ODS_DISABLED);CRect rcItem     = lpDrawItemStruct->rcItem;CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);CString strTitle;GetWindowText(strTitle);CFont *pOldFont=pDC->SelectObject(g_DisplayFont24);CSize szText=pDC->GetTextExtent(strTitle);::SetBkMode(hDC,TRANSPARENT);CRect rcCalc=rcItem;if(bIsDisabled){pDC->FillSolidRect(&rcItem,RGB(97,198,49));pDC->SetTextColor(RGB(128,128,128));}else if(m_bIsHovering){CBrush brsh;brsh.CreateSolidBrush(RGB(32,143,255));//76,137,189     18,118,191CPoint pt(10,10);CPen pen(PS_SOLID,1,RGB(32,143,255));//76,137,189       18,118,191pDC->SelectObject(&brsh);pDC->SelectObject(&pen);pDC->RoundRect(&rcItem,pt);if(bIsPressed)pDC->SetTextColor(RGB(255,0,0));elsepDC->SetTextColor(RGB(0,0,0));}else{
//      pDC->FillSolidRect(&rcItem,RGB(97,198,49));pDC->SetTextColor(RGB(0,0,0));//设置按钮文字的颜色CPen pen(PS_SOLID,1,RGB(0,128,255));//98,177,245        8,109,169CBrush brsh;brsh.CreateSolidBrush(RGB(0,128,255));//98,177,245      8,109,169CPoint pt(10,10);pDC->SelectObject(&brsh);pDC->SelectObject(&pen);pDC->RoundRect(&rcItem,pt);}if(szText.cx>rcItem.Width()){rcCalc.left = rcItem.left;rcCalc.right= rcItem.right;}else{rcCalc.left = (rcItem.Width()-szText.cx)/2;rcCalc.right= rcCalc.left+szText.cx;}rcCalc.top = rcItem.top;rcCalc.bottom = rcItem.bottom;pDC->DrawText(strTitle,rcCalc,/*DT_WORDBREAK*/DT_CENTER|DT_VCENTER|DT_SINGLELINE);pDC->SelectObject(pOldFont);
}void CBmpButton::OnSize(UINT nType, int cx, int cy) 
{CButton::OnSize(nType, cx, cy);if(IsWindow(m_hWnd))ResetWndRgn();
}BOOL CBmpButton::OnEraseBkgnd(CDC* pDC) 
{return TRUE;
}BOOL CBmpButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{::SetCursor(AfxGetApp()->LoadStandardCursor(MAKEINTRESOURCE(32649)));return TRUE;
}void CBmpButton::SetNonToolBarBtn(BOOL bBtn)
{m_bNonToolBarBtn = bBtn;
}
//////////////////////////////////////////////////////////////////////////
/*
void CBmpButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{HDC hDC          = lpDrawItemStruct->hDC;BOOL bIsDisabled = (lpDrawItemStruct->itemState & ODS_DISABLED);CRect rcItem     = lpDrawItemStruct->rcItem;CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);HDC hMemDC;CString strTitle;GetWindowText(strTitle);CSize szText=pDC->GetTextExtent(strTitle);CFont *pOldFont=pDC->SelectObject(g_DisplayFont16);::SetBkMode(hDC,TRANSPARENT);CRect rcCalc=rcItem;CRect rcBmp(0,0,39,36);if(bIsDisabled)pDC->SetTextColor(RGB(128,128,128));else if(m_bIsHovering){HRGN hRgnRct;hRgnRct = CreateRoundRectRgn(0,0,rcItem.right-rcItem.left,rcItem.bottom-rcItem.top,5,5);SetWindowRgn(hRgnRct,TRUE);pDC->FillSolidRect(&rcItem,RGB(255,255,255));pDC->SetTextColor(RGB(0,0,0));}else{HRGN hRgnRct;hRgnRct = CreateRoundRectRgn(0,0,rcItem.right-rcItem.left,rcItem.bottom-rcItem.top,5,5);SetWindowRgn(hRgnRct,TRUE);pDC->FillSolidRect(&rcItem,RGB(23,75,153));pDC->SetTextColor(RGB(255,255,255));}if(szText.cx>rcItem.Width()){rcCalc.left = rcItem.left;rcCalc.right= rcItem.right;}else{14rcCalc.left = (rcItem.Width()-szText.cx)/2;rcCalc.right= rcCalc.left+szText.cx;}rcCalc.top = rcBmp.Height()+5;rcCalc.bottom = rcItem.bottom;hMemDC=::CreateCompatibleDC(hDC);::SelectObject(hMemDC,m_hBitmap);::SetStretchBltMode(hDC,COLORONCOLOR);::StretchBlt(hDC,(rcItem.Width()-rcBmp.Width())/2,5,rcBmp.Width(),rcBmp.Height(),hMemDC,rcBmp.left,rcBmp.top,rcBmp.Width(),rcBmp.Height(),SRCCOPY);pDC->DrawText(strTitle,rcCalc,DT_WORDBREAK);pDC->SelectObject(pOldFont);::DeleteDC(hMemDC);
}*/

这篇关于CButton重绘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1016005

相关文章

MFC 控件重绘(2) NM_CUSTOMDRAW, WM_DRAWITEM, 虚函数DrawItem

控件重绘有三种方法: 1 设定界面属性 2 利用Windows的消息机制,通过Windows消息映射(Message Mapping)和反映射(Message Reflecting),在合适的时机修改控件的状态和行为。此方式涉及NM_CUSTOMDRAW和WM_DRAWITEM 3 利用虚函数机制,重载虚函数。即DrawItem虚函数。 对于NM_CUSTOMDRAW,某些支持此消息的控件

什么是回流与重绘,如何尽力避免

回流(reflow)和重绘(repaint)是浏览器渲染页面的两个不同过程。 回流概念(reflow) 当我们对 DOM 的修改引发了 DOM 几何尺寸的变化(比如修改元素的宽、高或隐藏元素等)时,浏览器需要重新计算元素的几何属性(其他元素的几何属性和位置也会因此受到影响),然后再将计算的结果绘制出来。这个过程就是回流(也叫重排)。【重新排列布局,即打碎重组】 重绘概念(repaint)

BrushNet重绘电商商品背景效果测试

🎨背景 之前写过一篇文章,简单的介绍了brushnet这个局部重绘节点,如何安装和使用可以参考我之前写的这篇文章,本篇重点测试下在背景生成这部分,brushnet是不是跟默认的inpaint有比较大的效果提升。 上一篇节点介绍内容链接: ComfyUI-BrushNet(局部重绘)节点安装及效果、模型下载及详细使用方法✨_brushnet模型放哪里-CSDN博客 🎄测试 首先搭建个基

MFC---重绘按钮

#ifndef _VANCBUTTON_MFC_H_ #define _VANCBUTTON_MFC_H_ #include "VUIControl.h" /**************************************************************/ //作者:Vansix //功能:重载CButton //版本:1.0 //修订:2016.05.08 //其

C# 窗体中Control以及Invalidate,Update,Refresh三种重绘方法的区别

在 C# 中,Control 类是 Windows Forms 应用程序中所有控件的基类。它提供了控件的基本功能和属性,这些功能和属性被所有继承自 Control 类的子类所共享。这意味着 Control 类是构建 Windows Forms 应用程序中用户界面元素的基础。 以下是 Control 类的一些关键特性和方法: 属性: Size:获取或设置控件的宽度和高度。Location:获

MFC三种不同方式实现图形的保存和重绘---方法二: 运用CMetaFileDC

在OnButtonUp()函数中的代码 //选中透明画刷 CBrush* brush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); m_mfDc.SelectObject(brush); //m_mfDc为CMetaFileDC对象,全局的 //通过m_mfDc绘图 switch(m_gType) {

MFC三种不同方式实现图形的保存和重绘---方法一:通过集合类CPtrArray保存点的坐标

//方法一:通过集合类CPtrArray保存点的坐标 响应WM_LBUTTONUP消息 在OnButtonUp()函数的代码 CClientDC dc(this);  //获得透明画刷并选中 CBrush* brush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); dc.SelectObject(brus

页面重绘和页面回流

页面重绘 页面重绘是指当元素样式改变时(包括颜色、背景色、可见性、透明度、文本内容、CSS动画和过度、改变类名、窗口大小变化以及修改字体相关属性等),浏览器需要重新绘制该元素的部分或全部内容,而不改变元素的位置和布局。 页面回流 当页面布局和几何属性发生改变时,浏览器需要重新计算元素的布局和几何属性,然后更新页面的渲染效果。 例如:修改元素的尺寸、位置等属性时,会触发该元素及其祖先元素的回流

UGUI空白可点击组件,减少重绘

如果使用image alpha = 0,会导致overDraw,直接清空mesh,不绘制即可避免 #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using UnityEngine.UI; namespace UnityGameFramework {     [AddComponentMenu("Game/UI/Gam

跨境电商必备神器!一秒实现图像处理|批量抠图|局部重绘|高清修复,统统一步到位!

跨境电商的小伙伴们,你们是否经常头疼产品图片的处理。批量抠图太麻烦?抠完图片还要打开多个软件进行图片处理,切来切去,效率低下?今天,我要给大家带来一个电商设计神器——千鹿设计助手,让你的设计工作变得简单又高效! 工具准备: 访问千鹿设计助手https://qianlu.cc/?s=rGUHvi,填写码:rGUHvi 完成注册。 Alt+空格 呼出快搜索框,安装插件。 操作步骤: 一、批