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的更多相关文章

  1. QT按键(Qbutton)改变颜色

    第一种是按键上面的字颜色的改变:   ui->motor1->setStyleSheet("color: red"); 效果: 第二种是背景改变: ui->mot ...

  2. WIN32 窗口类封装 框架实现部分

    上面已经讲了窗口封装部分,内容可点击:http://www.cnblogs.com/mengdejun/p/4010320.html,下面分享框架部分内容,完成WINDOWS消息迭代 CQFrameW ...

  3. Qt QObject

    [1]Qt的QObject 1.测试代码如下: #include<QApplication> #include<QPushButton> #include<QDebug& ...

  4. 模仿MFC封装Windows API

    .... 最后添加了两个按钮,分别处理每个按钮的单击事件时,走了弯路,本来想的是在QButton中重写OnLButtonDown方法,但是,无法区分是那个按钮.参考这篇文章: http://zhida ...

  5. 《Linux与Qt程序设计》知识框架

    本文主要是通过一本书来大致了解Qt开发的框架,不对具体内容做详细分析. 1.首先弄清楚概念:定义->以自己的话理解是什么-> 实现的是什么功能->用在哪些地方 2.前面认识到的知识点 ...

  6. Qt 属性

      Qt提供了一套和一些编译器提供商也提供的属性系统类似的完善的属性系统.然而,作为一个不依赖编译器和平台的库,Qt不能依赖像__property或者[property]那样的非标准编译器特征.我们的 ...

  7. Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)

    Skin(表皮) 是制作比较酷的软件界面的有利工具. 一个软件可以同时使用多种Skin 以取得不同的外观, 使同一个软件有截然不同的风格. 用户可以根据自己的喜好选择 不同的风格. 本节介绍使用 Qt ...

  8. Qt控件精讲一:按钮

    原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...

  9. JavasScript实现调查问卷插件

    原文:JavasScript实现调查问卷插件 鄙人屌丝程序猿一枚,闲来无事,想尝试攻城师是感觉,于是乎搞了点小玩意.用js实现调查问卷,实现了常规的题型,单选,多选,排序,填空,矩阵等. 遂开源贴出来 ...

随机推荐

  1. Oracle中文乱码解决办法总结

    AMERICAN_AMERICA.WE8ISO8859P1,这个字符编码是西欧字符编码,对应于.Net的就是iso-8859-1字符编码,所以只需要改到系统的默认字符编码就行了. 解决方法: (1) ...

  2. Eclipse Oxygen创建maven web项目(二)

    配置项目依赖库及第三方工具. 如示例的pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  3. C#winform实现跑马灯

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. ASTER:An Attentional Scene Text Recognizer with Flexible Rectification

    代码链接:https://github.com/bgshih/aster 方法概述 本文方法主要解决不规则排列文字的文字识别问题,论文为之前一篇CVPR206的paper(Robust Scene T ...

  5. JVM 入门三板斧

    一个JVM实例只存在一个堆内存,堆内存的大小是可以调节的.类加载器读取了类文件后,需要把类.方法.常变量放到堆内存中,保存所有引用类型的真实信息,以方便执行器执行,堆内存分为三部分: Young Ge ...

  6. Matlab中classperf对象各属性解释[原创]

    1.ClassLabels:类型标识.第一个label作为pos,第二次label作为neg. 2.GroundTruth:各次实验的观察值,也就是真实值. 3.ValidationCounter: ...

  7. Libgdx之Music Sound 音效

    教程总文件夹: http://blog.csdn.net/zqiang_55/article/details/50878524 一个好的游戏.假设没有游戏音效那么这个游戏就过于简单乏味.Libgdx中 ...

  8. Android 如何在Eclipse 引入外部纯Java项目(不是打成Jar使用)

    应用情景--如标题: 在Eclipse的 “Android启动项目”中引入“外部的纯Java项目”,能运行的只有是基于Android的测试代码才可以. 一直很纳闷,如果外部写好一个Java插件(例如服 ...

  9. [svc]nginx优化

    nginx的25条优化

  10. vue-cli 本地数据模拟

    方法一: 使用express搭建静态服务 mock数据写在json文件中,proxyTable 里将接口代理到具体mock数据json文件上.具体方法: 创建 mock 文件夹 build/dev-s ...