//Download by http://www.NewXing.com
//
// GdipButton.cpp : Version 1.0 - see article at CodeProject.com
//
// Author: Darren Sessions
//
//
// Description:
// GdipButton is a CButton derived control that uses GDI+
// to support alternate image formats
//
// History
// Version 1.0 - 2008 June 10
// - Initial public release
//
// License:
// This software is released under the Code Project Open License (CPOL),
// which may be found here: http://www.codeproject.com/info/eula.aspx
// You are free to use this software in any way you like, except that you
// may not sell this source code.
//
// This software is provided "as is" with no expressed or implied warranty.
// I accept no liability for any damage or loss of business that this
// software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GdipButton.h"
#include "CGdiPlusBitmap.h"
#include "MemDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGdipButton
CGdipButton::CGdipButton()
{
m_pStdImage = NULL;
m_pAltImage = NULL;
m_bHaveBitmaps = FALSE;
m_bHaveAltImage = FALSE;
m_pCurBtn = NULL;
m_bIsDisabled = FALSE;
m_bIsToggle = FALSE;
m_bIsHovering = FALSE;
m_bIsTracking = FALSE;
m_nCurType = STD_TYPE;
m_pToolTip = NULL;
}
CGdipButton::~CGdipButton()
{
if(m_pStdImage) delete m_pStdImage;
if(m_pAltImage) delete m_pAltImage;
if(m_pToolTip) delete m_pToolTip;
}
BEGIN_MESSAGE_MAP(CGdipButton, CButton)
//{{AFX_MSG_MAP(CGdipButton)
ON_WM_DRAWITEM()
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//=============================================================================
//
// LoadStdImage()
//
// Purpose: The LoadStdImage() Loads the image for the button. This
// function must be called at a minimum or the button wont do
// anything.
//
// Parameters:
// [IN] id
// resource id, one of the resources already imported with the
// resource editor, usually begins with IDR_
//
// [IN] pType
// pointer to string describing the resource type
//
// Returns: BOOL
// Non zero if successful, otherwise zero
//
//=============================================================================
BOOL CGdipButton::LoadStdImage(UINT id, LPCTSTR pType)
{
m_pStdImage = new CGdiPlusBitmapResource;
return m_pStdImage->Load(id, pType);
}
//=============================================================================
//
// LoadAltImage()
//
// Purpose: The LoadAltImage() Loads the altername image for the button.
// This function call is optional
// Parameters:
// [IN] id
// resource id, one of the resources already imported with the
// resource editor, usually begins with IDR_
//
// [IN] pType
// pointer to string describing the resource type
//
// Returns: BOOL
// Non zero if successful, otherwise zero
//
//=============================================================================
BOOL CGdipButton::LoadAltImage(UINT id, LPCTSTR pType)
{
m_bHaveAltImage = TRUE;
m_pAltImage = new CGdiPlusBitmapResource;
return (m_pAltImage->Load(id, pType));
}
//=============================================================================
//
// The framework calls this member function when a child control is about to
// be drawn. All the bitmaps are created here on the first call. Every thing
// is done with a memory DC except the background, which get's it's information
// from the parent. The background is needed for transparent portions of PNG
// images. An always on top app (such as Task Manager) that is in the way can
// cause it to get an incorrect background. To avoid this, the parent should
// call the SetBkGnd function with a memory DC when it creates the background.
//
//=============================================================================
HBRUSH CGdipButton::CtlColor(CDC* pScreenDC, UINT nCtlColor)
{
if(!m_bHaveBitmaps)
{
if(!m_pStdImage)
{
return NULL; // Load the standard image with LoadStdImage()
}
CBitmap bmp, *pOldBitmap;
CRect rect;
GetClientRect(rect);
// do everything with mem dc
CMemDC pDC(pScreenDC, rect);
Gdiplus::Graphics graphics(pDC->m_hDC);
// background
if (m_dcBk.m_hDC == NULL)
{
CRect rect1;
CClientDC clDC(GetParent());
GetWindowRect(rect1);
GetParent()->ScreenToClient(rect1);
m_dcBk.CreateCompatibleDC(&clDC);
bmp.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
pOldBitmap = m_dcBk.SelectObject(&bmp);
m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
bmp.DeleteObject();
}
// standard image
if (m_dcStd.m_hDC == NULL)
{
PaintBk(pDC);
graphics.DrawImage(*m_pStdImage, 0, 0);
m_dcStd.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
pOldBitmap = m_dcStd.SelectObject(&bmp);
m_dcStd.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
bmp.DeleteObject();
// standard image pressed
if (m_dcStdP.m_hDC == NULL)
{
PaintBk(pDC);
graphics.DrawImage(*m_pStdImage, 1, 1);
m_dcStdP.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
pOldBitmap = m_dcStdP.SelectObject(&bmp);
m_dcStdP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
bmp.DeleteObject();
}
// standard image hot
if(m_dcStdH.m_hDC == NULL)
{
PaintBk(pDC);
ColorMatrix HotMat = { 1.05f, 0.00f, 0.00f, 0.00f, 0.00f,
0.00f, 1.05f, 0.00f, 0.00f, 0.00f,
0.00f, 0.00f, 1.05f, 0.00f, 0.00f,
0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
0.05f, 0.05f, 0.05f, 0.00f, 1.00f };
ImageAttributes ia;
ia.SetColorMatrix(&HotMat);
float width = (float)m_pStdImage->m_pBitmap->GetWidth();
float height = (float)m_pStdImage->m_pBitmap->GetHeight();
RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;
graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);
m_dcStdH.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
pOldBitmap = m_dcStdH.SelectObject(&bmp);
m_dcStdH.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
bmp.DeleteObject();
}
// grayscale image
if(m_dcGS.m_hDC == NULL)
{
PaintBk(pDC);
ColorMatrix GrayMat = { 0.30f, 0.30f, 0.30f, 0.00f, 0.00f,
0.59f, 0.59f, 0.59f, 0.00f, 0.00f,
0.11f, 0.11f, 0.11f, 0.00f, 0.00f,
0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
0.00f, 0.00f, 0.00f, 0.00f, 1.00f };
ImageAttributes ia;
ia.SetColorMatrix(&GrayMat);
float width = (float)m_pStdImage->m_pBitmap->GetWidth();
float height = (float)m_pStdImage->m_pBitmap->GetHeight();
RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;
graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);
m_dcGS.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
pOldBitmap = m_dcGS.SelectObject(&bmp);
m_dcGS.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
bmp.DeleteObject();
}
}
// alternate image
if( (m_dcAlt.m_hDC == NULL) && m_bHaveAltImage )
{
PaintBk(pDC);
graphics.DrawImage(*m_pAltImage, 0, 0);
m_dcAlt.CreateCompatibleDC(pDC);
bmp.Create
没有合适的资源?快使用搜索试试~ 我知道了~
VC中采用PNG图片作为按钮
共26个文件
h:7个
cpp:4个
png:4个
4星 · 超过85%的资源 需积分: 42 9 下载量 138 浏览量
2013-05-24
16:36:01
上传
评论
收藏 587KB RAR 举报
温馨提示
可以将PNG作为按钮,使做出来的界面美观好看,很实用的,希望大家可以学习一下
资源推荐
资源详情
资源评论
收起资源包目录
VC中采用PNG图片作为按钮.rar (26个子文件)
www.NewXing.com
src
GdipButton.h 3KB
resource.h 1KB
TestGdipButton.h 1KB
TestGdipButton.cpp 2KB
TestGdipButtonDlg.h 2KB
res
TestGdipButton.ico 1KB
Jules-Asner.bmp 599KB
GEAR2.png 31KB
exit.png 11KB
play.png 11KB
Thumbs.db 29KB
TestGdipButton.rc2 406B
pause.png 11KB
MemDC.h 3KB
TestGdipButton.aps 701KB
TestGdipButtonDlg.cpp 7KB
StdAfx.cpp 254B
TestGdipButton.rc 6KB
GdipButton.cpp 17KB
StdAfx.h 1KB
CGdiPlusBitmap.h 3KB
vs2005
TestGdipButton.vcproj 9KB
TestGdipButton.sln 892B
TestGdipButton.suo 22KB
vs6
TestGdipButton.dsw 551B
TestGdipButton.dsp 5KB
共 26 条
- 1
资源评论
- zhyhao_8712013-06-21挺好的,移植的时候要注意GDI+ 初始化。
- zhangbf20012014-05-26不错,挺有用的
LMG天之蓝
- 粉丝: 12
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功