QButton
Button.h
#pragma once // QButton class QButton : public CButton
{
DECLARE_DYNAMIC(QButton)
// Construction
public:
QButton(); //Attributes
protected:
//The outside border of the button
CPen m_penBoundry; //When the mouse pointer above button button inside the frame
CPen m_penInsideBoundryLeft;
CPen m_penInsideBoundryRight;
CPen m_penInsideBoundryTop;
CPen m_penInsideBoundryBottom; //Button gains focus when the button on the borders
CPen m_penInsideBoundryLeftSel;
CPen m_penInsideBoundryRightSel;
CPen m_penInsideBoundryTopSel;
CPen m_penInsideBoundryBottomSel; //Button's background, including two kinds of valid and invalid
CBrush m_brFillActive;
CBrush m_brFillInactive; //Button status
BOOL m_bOver; //This value to true when the mouse in the button above, instead of flase
BOOL m_bTracking;//In the mouse press not to release the value to true
BOOL m_bSelected;//Button is pressed the value is true
BOOL m_bFocus;//Button for the current focus when the value is true CFont m_font; //Overridables
public:
virtual void PreSubclassWindow();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual ~QButton(); // Implementation
protected:
virtual void DoGradientFill(CDC *pDC, CRect* rect);
virtual void DrawInsideBorder(CDC *pDC, CRect* rect); // Generated message map functions
protected:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam); DECLARE_MESSAGE_MAP() };
Button.cpp
// Button.cpp
// #include "stdafx.h"
#include "Button.h" // QButton IMPLEMENT_DYNAMIC(QButton, CButton) QButton::QButton()
{
//The outside border of the button
m_penBoundry.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(,,)); //When the mouse pointer above button button inside the frame
m_penInsideBoundryLeft.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryRight.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryTop.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryBottom.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , )); //Button gains focus when the button on the borders
m_penInsideBoundryLeftSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryRightSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryTopSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , ));
m_penInsideBoundryBottomSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, , RGB(, , )); //Button's background, including two kinds of valid and invalid
m_brFillActive.CreateSolidBrush(RGB(, , ));
m_brFillInactive.CreateSolidBrush(RGB(, , )); //Button status
m_bOver = m_bSelected = m_bTracking = m_bFocus = FALSE; m_font.CreateFont(
, // nHeight
, // nWidth
, // nEscapement
, // nOrientation
FW_MEDIUM, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFacename
} QButton::~QButton()
{
m_penBoundry.DeleteObject(); m_penInsideBoundryLeft.DeleteObject();
m_penInsideBoundryRight.DeleteObject();
m_penInsideBoundryTop.DeleteObject();
m_penInsideBoundryBottom.DeleteObject(); m_penInsideBoundryLeftSel.DeleteObject();
m_penInsideBoundryRightSel.DeleteObject();
m_penInsideBoundryTopSel.DeleteObject();
m_penInsideBoundryBottomSel.DeleteObject(); m_brFillActive.DeleteObject();
m_brFillInactive.DeleteObject(); m_font.DeleteObject();
} BEGIN_MESSAGE_MAP(QButton, CButton)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
END_MESSAGE_MAP() void QButton::OnMouseMove(UINT nFlags, CPoint point)
{
if(!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = ;
m_bTracking = ::_TrackMouseEvent(&tme);
} CButton::OnMouseMove(nFlags, point);
} LRESULT QButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bOver = FALSE;
m_bTracking = FALSE;
InvalidateRect(NULL, FALSE); //parm2:FALSE Heavy paintings don't erase the background
//InvalidateRect to call DrawItem
return ;
} LRESULT QButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
m_bOver = TRUE;
InvalidateRect(NULL); //parm2:TRUE Again when will erase the background.
return ;
} //Add the Owner Draw properties
void QButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(, BS_OWNERDRAW);
} void QButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//Information obtained from lpDrawItemStruct controls
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nSaveDC = pDC->SaveDC();
UINT state = lpDrawItemStruct->itemState;
POINT pt ;
TCHAR strText[MAX_PATH + ];
::GetWindowText(m_hWnd, strText, MAX_PATH); //The outside border of the draw button, it is a radius of 5 of the rounded rectangle
pt.x = ;
pt.y = ;
CPen* hOldPen = pDC->SelectObject(&m_penBoundry);
pDC->RoundRect(&rect, pt); //Access to state of the button
if (state & ODS_FOCUS)
{
m_bFocus = TRUE;
m_bSelected = TRUE;
}
else
{
m_bFocus = FALSE;
m_bSelected = FALSE;
} if (state & ODS_SELECTED || state & ODS_DEFAULT)
{
m_bFocus = TRUE;
} pDC->SelectObject(hOldPen); rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE))); //According to the status of button fill color button
CBrush* pOldBrush;
if (m_bOver)
{
pOldBrush = pDC->SelectObject(&m_brFillActive);
DoGradientFill(pDC, &rect);
}
else
{
pOldBrush = pDC->SelectObject(&m_brFillInactive);
DoGradientFill(pDC, &rect);
} //According to the button state to plot borders
if (m_bOver || m_bSelected)
DrawInsideBorder(pDC, &rect); pDC->SelectObject(pOldBrush); //Displays the text button
if (strText!=NULL)
{
//CFont* hFont = GetFont();
CFont* hOldFont = pDC->SelectObject(&m_font);
CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
CPoint pt( rect.CenterPoint().x - szExtent.cx / , rect.CenterPoint().y - szExtent.cy / );
if (state & ODS_SELECTED)
pt.Offset(, );
int nMode = pDC->SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, , (HBRUSH)NULL);
else
pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, , (HBRUSH)NULL);
pDC->SelectObject(hOldFont);
pDC->SetBkMode(nMode);
} pDC->RestoreDC(nSaveDC);
} //The color of button
void QButton::DoGradientFill(CDC *pDC, CRect* rect)
{
#define QBUTTON_WIDTH 64
CBrush brBk[QBUTTON_WIDTH];
int nWidth = rect->Width();
int nHeight = rect->Height();
CRect rct;
int i;
for(i = ; i < QBUTTON_WIDTH; i++)
{
if(m_bOver)
{
if(m_bFocus)
brBk[i].CreateSolidBrush(RGB( - (i / ), - (i / ), - (i / )));
else
brBk[i].CreateSolidBrush(RGB( - (i / ), - (i / ), - (i / )));
}
else
{
if(m_bFocus)
brBk[i].CreateSolidBrush(RGB( - (i / ), - (i / ), - (i / )));
else
brBk[i].CreateSolidBrush(RGB( - (i / ), - (i / ), - (i / )));
}
} for(i = rect->top; i <= nHeight + ; i++)
{
rct.SetRect(rect->left, i, nWidth + , i + );
pDC->FillRect(&rct, &brBk[((i * ) / nHeight)]);
} for(i = ; i < QBUTTON_WIDTH; i++)
brBk[i].DeleteObject();
} //Within the draw button on the borders
void QButton::DrawInsideBorder(CDC *pDC, CRect* rect)
{
CPen *pLeft, *pRight, *pTop, *pBottom; if(m_bSelected && !m_bOver)
{
pLeft = &m_penInsideBoundryLeftSel;
pRight = &m_penInsideBoundryRightSel;
pTop = &m_penInsideBoundryTopSel;
pBottom = &m_penInsideBoundryBottomSel;
}
else
{
pLeft = &m_penInsideBoundryLeft;
pRight = &m_penInsideBoundryRight;
pTop = &m_penInsideBoundryTop;
pBottom = &m_penInsideBoundryBottom;
} CPoint oldPoint = pDC->MoveTo(rect->left, rect->bottom - );
CPen *pOldPen = pDC->SelectObject(pLeft);
pDC->LineTo(rect->left, rect->top + );
pDC->SelectObject(pRight);
pDC->MoveTo(rect->right - , rect->bottom - );
pDC->LineTo(rect->right - , rect->top);
pDC->SelectObject(pTop);
pDC->MoveTo(rect->left - , rect->top);
pDC->LineTo(rect->right - , rect->top);
pDC->SelectObject(pBottom);
pDC->MoveTo(rect->left, rect->bottom);
pDC->LineTo(rect->right - , rect->bottom);
pDC->SelectObject(pOldPen);
pDC->MoveTo(oldPoint); if(m_bSelected && !m_bOver)
::DrawFocusRect(pDC->m_hDC, rect);
}
QButton的更多相关文章
- QT按键(Qbutton)改变颜色
第一种是按键上面的字颜色的改变: ui->motor1->setStyleSheet("color: red"); 效果: 第二种是背景改变: ui->mot ...
- WIN32 窗口类封装 框架实现部分
上面已经讲了窗口封装部分,内容可点击:http://www.cnblogs.com/mengdejun/p/4010320.html,下面分享框架部分内容,完成WINDOWS消息迭代 CQFrameW ...
- Qt QObject
[1]Qt的QObject 1.测试代码如下: #include<QApplication> #include<QPushButton> #include<QDebug& ...
- 模仿MFC封装Windows API
.... 最后添加了两个按钮,分别处理每个按钮的单击事件时,走了弯路,本来想的是在QButton中重写OnLButtonDown方法,但是,无法区分是那个按钮.参考这篇文章: http://zhida ...
- 《Linux与Qt程序设计》知识框架
本文主要是通过一本书来大致了解Qt开发的框架,不对具体内容做详细分析. 1.首先弄清楚概念:定义->以自己的话理解是什么-> 实现的是什么功能->用在哪些地方 2.前面认识到的知识点 ...
- Qt 属性
Qt提供了一套和一些编译器提供商也提供的属性系统类似的完善的属性系统.然而,作为一个不依赖编译器和平台的库,Qt不能依赖像__property或者[property]那样的非标准编译器特征.我们的 ...
- Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)
Skin(表皮) 是制作比较酷的软件界面的有利工具. 一个软件可以同时使用多种Skin 以取得不同的外观, 使同一个软件有截然不同的风格. 用户可以根据自己的喜好选择 不同的风格. 本节介绍使用 Qt ...
- Qt控件精讲一:按钮
原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...
- JavasScript实现调查问卷插件
原文:JavasScript实现调查问卷插件 鄙人屌丝程序猿一枚,闲来无事,想尝试攻城师是感觉,于是乎搞了点小玩意.用js实现调查问卷,实现了常规的题型,单选,多选,排序,填空,矩阵等. 遂开源贴出来 ...
随机推荐
- Python字符串与二进制串的相互转换
python基础知识之字符编码与转换 - 机壳啦 - 博客园https://www.cnblogs.com/home979/p/7838244.html Python 字符串与二进制串的相互转换 - ...
- xml中的<![CDATA[]]> 简介
被<![CDATA[]]>这个标记所包含的内容将表示为纯文本,比如<![CDATA[<]]>表示文本内容“<”. 此标记用于xml文档中,我们先来看看使用转义符的 ...
- Intel CPU命名规则
intel的几代CPU中,后缀字母主要有以下几种:M:笔记本专用CPU,一般为双核,M前面一位数字是0,意味着是标准电压处理器,如果是7,则是低电压处理器.U:笔记本专用低电压CPU,一般为双核,U前 ...
- unity, 在材质上指定render queue
材质球inspector面板在debug模式下可以看到Custom Render Queue一项: 其默认值为-1,表示使用相应shader的render queue设置. 也可以人为将其改为其它值, ...
- Another app is currently holding the yum lock; waiting for it to exit.. yum被锁定无法使用
yum被锁定无法使用 Another app is currently holding the yum lock; waiting for it to exit.. 解决方法: rm -rf /var ...
- kafka 监控(eagle)
topic:创建时topic名称 partition:分区编号 offset:表示该parition已经消费了多少条message logSize:表示该partition已经写了多少条message ...
- 关于haproxy多域名证书的配置
frontend main bind *: bind *: ssl crt /etc/haproxy/kong.com.pem crt /etc/haproxy/51yijI.com.pem no-s ...
- Ubuntu java install & config
im:http://jingyan.baidu.com/article/08b6a591cb06f114a8092209.html http://www.cnblogs.com/zknublx/p/5 ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- HTML5学习笔记(十三):JavaScript函数
函数定义 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述 ...