这个控件能够制作出和PhotoShop几乎一样的工具栏。当然换了图片以后也能作出其他的效果。能够完美的支持三态(Normal,Mouse Over,Checked)。
描画部分使用了GDI+,如果没有安装的话,安装一下就好了。GDI+是一个微软免费提供的描画库,是WIN平台上GDI库的改进版,拥有很多方便的功能。下载后,解压缩到一个位置,然后在VC6的Tools / Options / Directories设置一下就可以了(其他IDE大致上都差不多)。
.h文件
/**/
/*************************************************************************
* Copyright (c) 2007 李兰非(Li Lanfei)
* Ver: 1.0
* Date : 2004.12.15
* Email : TheSmallCar@Gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*************************************************************************/


#if
!defined(AFX_BMPBUTTON_H__5D262D01_6F1C_43DF_B7EC_5CD86FFB1A88__INCLUDED_)
#define
AFX_BMPBUTTON_H__5D262D01_6F1C_43DF_B7EC_5CD86FFB1A88__INCLUDED_

//
包含了std::string,并使用了std的名字空间
#include
<
String
>
using
namespace
std;
#if
_MSC_VER > 1000
#pragma
once
#endif
//
_MSC_VER > 1000
//
BMPButton.h : header file


/**/
//////////////////////////////[ For Init GDI+ ]///////////////////////////
//
该部分加载了GDI+ 的相关头文件和lib文件
//
需要GDI+的支持,相关的文件路径也需要设置正确。
//
建议在 [ Tools / Options / Directories ]里设置正确的路径
#ifndef _GDIPLUS_H
#define
_SELF_IMPORT_GDI_PLUS
#include
<
comdef.h
>

#if
defined(_WIN64)
typedef unsigned __int64 ULONG_PTR;
#else
typedef unsigned
long
ULONG_PTR;
#endif

#include
"
GdiPlus.h
"

#pragma
comment( lib, "gdiplus.lib" )

using
namespace
Gdiplus;
#endif


/**/
//////////////////////////////[ For Init GDI+ End ]////////////////////////

//
按钮状态类型
enum
ButtonStatus
...
{
B_NORMAL, //正常
B_DOWN, //按下
B_CHECK, //选中(BT_CHECK)风格有效
B_MOUSE_OVER, //鼠标悬浮
B_DISABLE //无效
}
;
//
按钮风格类型
enum
ButtonType
...
{
BT_NORMAL, //正常
BT_CHECK //Check Box,即三态按钮
}
;

/**/
/////////////////////////////////////////////////////////////////////////////
//
BMPButton window
class
CBMPButton :
public
CButton
...
{
// Construction
public:
CBMPButton();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{
{AFX_VIRTUAL(CBMPButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CBMPButton();
// Generated message map functions
protected:
//{
{AFX_MSG(CBMPButton)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave( WPARAM wparam, LPARAM lparam );
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

// For GDI+
#if _SELF_IMPORT_GDI_PLUS
private:
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_pGdiToken;
#endif
public:
void SetImage( CString Normal, CString Hot = "", CString Click = "", CString Disable = "" );
void SetPosition( int l, int t, int w, int h );
ButtonStatus SetCheck( BOOL Check );
BO

这篇博客介绍了如何使用C++/MFC创建一个类似PhotoShop工具栏的Button控件,支持三种状态:正常、鼠标悬停、选中。通过GDI+库进行描画,当GDI+未安装时需要安装。文章提供了.h和.cpp文件以及不同状态的按钮图例,展示了控件的基本效果,并指出只需一张正常状态的图片即可自动生成其他状态的样式,简化了设计过程。
7336

被折叠的 条评论
为什么被折叠?



