您的位置:首页 > 其它

MFC:重绘Button,定制CButton,自画CPngButton,求赐教(各种bug包括性能bug)谢谢谢谢

2013-06-19 20:04 344 查看
【1、】CPngButton.h(资源是最后图片)

#pragma once

#include<atlimage.h>
#define PNUM 19
#define PLAYTIME 100

// CPngButton

class CPngButton : public CButton
{
DECLARE_DYNAMIC(CPngButton)

public:
CPngButton();
virtual ~CPngButton();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void PreSubclassWindow();
virtual BOOL OnEraseBkgnd(CDC *pDC);

int DestroyP(void);
int LoadP(void);
int CutP(void);
int PlayP(void);
void DrawButton(CDC* pDC);

protected:

afx_msg void OnTimer(UINT nIDEvent);
DECLARE_MESSAGE_MAP()
private:
CImage m_image;
CRect m_rectArr[PNUM];
CRect m_rect;
};

【2】、CPngButton.cpp

// PngButton.cpp : 实现文件
//

#include "stdafx.h"
#include "animal1.h"
#include "PngButton.h"

// CPngButton

IMPLEMENT_DYNAMIC(CPngButton, CButton)

CPngButton::CPngButton()
{
LoadP();
CutP();
m_rect=m_rectArr[0];
}

CPngButton::~CPngButton()
{
DestroyP();
}

BEGIN_MESSAGE_MAP(CPngButton, CButton)
ON_WM_TIMER()
END_MESSAGE_MAP()

void CPngButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(0,BS_OWNERDRAW);
}

// CPngButton 消息处理程序
void CPngButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(lpDrawItemStruct->CtlType!=ODT_BUTTON)
return;

CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
int nSavedDC=pDC->SaveDC();
VERIFY(pDC);
DrawButton(pDC);
pDC->RestoreDC(nSavedDC);
}

int CPngButton::LoadP(void)
{
m_image.Load("res\\Card.png");//此处Card是一幅4750*150的(.png)格式的图片
if(m_image.IsNull())
{
MessageBox(_T("加载失败"));
return -1;
}
if(m_image.GetBPP() ==32)
{
int i,j;
for(i=0;i<m_image.GetWidth();i++)
{
for(j=0;j<m_image.GetHeight();j++)
{
byte *pByte=(byte*)m_image.GetPixelAddress(i,j);
pByte[0]=pByte[0]*pByte[3]/255;
pByte[1]=pByte[1]*pByte[3]/255;
pByte[2]=pByte[2]*pByte[3]/255;
}
}
}
return 0;
}

int CPngButton::DestroyP(void)
{
m_image.Destroy();
return 0;
}

int CPngButton::CutP()
{
int width=4750/PNUM;
for(int i=0;i<PNUM;i++)
{
m_rectArr[i].left=i*width;
m_rectArr[i].top=0;
m_rectArr[i].right=(i+1)*width;
m_rectArr[i].bottom=150;
}
return 0;
}

int CPngButton::PlayP()
{
SetTimer(PLAYTIME,200,NULL);
return 0;
}

void CPngButton::OnTimer(UINT nIDEvent)
{
if(nIDEvent==PLAYTIME)
{
static int i=0;
++i;
m_rect=m_rectArr[i];
if(i>= 19)
{
KillTimer(PLAYTIME);
i=0;
}

CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
InvalidateRect(&rect);
DrawButton(&dc);
}
CButton::OnTimer(nIDEvent);

}

void CPngButton::DrawButton(CDC *pDC)
{
// pDC->SetBkMode(TRANSPARENT);
// pDC->SetBkMode(OPAQUE);
CRect rect;
GetClientRect(&rect);
m_image.Draw(pDC->m_hAttribDC,rect,m_rect);
}

BOOL CPngButton::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}

【3、】animal1Dlg.h

#pragma once
#include "PngButton.h"

// Canimal1Dlg 对话框
class Canimal1Dlg : public CDialog
{
// 构造
public:
Canimal1Dlg(CWnd* pParent = NULL); // 标准构造函数

// 对话框数据
enum { IDD = IDD_ANIMAL1_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

// 实现
protected:
HICON m_hIcon;

// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:

CPngButton m_pngButton;
afx_msg void OnBnClickedOk();
};

【4、】// animal1Dlg.cpp : 实现文件

#include "stdafx.h"
#include "animal1.h"
#include "animal1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Canimal1Dlg 对话框

Canimal1Dlg::Canimal1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(Canimal1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void Canimal1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON2, m_pngButton);
}

BEGIN_MESSAGE_MAP(Canimal1Dlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &Canimal1Dlg::OnBnClickedOk)
END_MESSAGE_MAP()

// Canimal1Dlg 消息处理程序

BOOL Canimal1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void Canimal1Dlg::OnPaint()
{

if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR Canimal1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

void Canimal1Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
m_pngButton.PlayP();
// OnOK();
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: