自绘贴图-MFC方式

自绘

窗体要有自绘风格, 对于按钮, 样式是 BS_OWNERDRAW

响应WM_PAINT && 覆盖DrawItem

贴图操作

贴图,就是将自绘操作换成图片显示.

对于我这个Demo, 就是更换 CMyButton::PaintMe 实现

DC操作

DC是设备上下文, 代表和目标设备相关的环境.
物理DC相当于显示器的显存或打印机的打印缓冲区.
内存DC相当于和显示器的显存(打印机的打印缓冲区)相同size的缓冲区
将内存DC拷贝进物理DC后,就可以显示或打印.

Demo

// mainDlg.h : header file
//

#if !defined(AFX_MAINDLG_H__4701FEEA_EDE4_4437_9C42_957EE8840014__INCLUDED_)
#define AFX_MAINDLG_H__4701FEEA_EDE4_4437_9C42_957EE8840014__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/
// CMainDlg dialog

class CMainDlg : public CDialog
{
// Construction
public:
	CMainDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CMainDlg)
	enum { IDD = IDD_MAIN_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMainDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

    private:
        void DyCreateControl();
        void UnDyCreateControl();
// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CMainDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnDestroy();
    afx_msg void OnBtnDyCreate(UINT nID);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINDLG_H__4701FEEA_EDE4_4437_9C42_957EE8840014__INCLUDED_)
// mainDlg.cpp : implementation file
//

#include "stdafx.h"
#include <assert.h>
#include "main.h"
#include "mainDlg.h"
#include "MyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CMainDlg dialog

CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMainDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMainDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
	//{{AFX_MSG_MAP(CMainDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP

    ///< 必须放在 //}}AFX_MSG_MAP外面
    ON_COMMAND_RANGE(2000, 2399, OnBtnDyCreate)

END_MESSAGE_MAP()

/
// CMainDlg message handlers

/// 动态创建控件的好处 : 可以批量操作,创建数量可以超过255
const int g_iRow = 1;
const int g_iCol = 1;
const DWORD g_dwCtrlIdBegin = 2000;
const DWORD g_dwCtrlIdEnd = g_dwCtrlIdBegin + g_iRow * g_iCol - 1;
const int g_nWidth = 100;
const int g_nHeight = 30;

void CMainDlg::OnBtnDyCreate(UINT nID) {
    CString str;
    CButton* pBtn = (CButton*)GetDlgItem(nID);

    str.Format("%d", nID & 0xf);
    pBtn->SetWindowText(str);
}

void CMainDlg::DyCreateControl() {
    int i = 0;
    int j = 0;
    int x = 0;
    int y = 0;
    
    CButton* pBtn = NULL;
    RECT rt;
    
    for (i = 0; i < g_iRow; i++) {
        for (j = 0; j < g_iCol; j++) {
            pBtn = new CMyButton;
            assert(NULL != pBtn);
            
            SetRect(
                &rt, 
                x + j * g_nWidth, 
                y + i * g_nHeight, 
                x + j * g_nWidth + g_nWidth, 
                y + i * g_nHeight + g_nHeight);

            pBtn->Create(TEXT(""), 
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_OWNERDRAW, 
                rt, this, g_dwCtrlIdBegin + i * g_iRow + j);
        }
    }
}

void CMainDlg::UnDyCreateControl() {
    int i = 0;
    int j = 0;
    CButton* pBtn = NULL;
    
    for (i = 0; i < g_iRow; i++) {
        for (j = 0; j < g_iCol; j++) {
            pBtn = (CButton*)GetDlgItem(g_dwCtrlIdBegin + i * g_iRow + j);
            if (NULL != pBtn) {
                delete pBtn;
                pBtn = NULL;
            }
        }
    }
}

void CMainDlg::OnDestroy() 
{
    CDialog::OnDestroy();
    UnDyCreateControl();
}

BOOL CMainDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    DyCreateControl();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMainDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMainDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
        CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMainDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

#if !defined(AFX_MYBUTTON_H__4C9C57DA_61FC_4284_9F9C_78ED978514B2__INCLUDED_)
#define AFX_MYBUTTON_H__4C9C57DA_61FC_4284_9F9C_78ED978514B2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h : header file
//

/
// CMyButton window

class CMyButton : public CButton
{
public:
	CMyButton();
	virtual ~CMyButton();

	//{{AFX_VIRTUAL(CMyButton)
	public:
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
	//}}AFX_VIRTUAL

private:
    void PaintMe(CDC& dc, UINT itemAction);

protected:
	//{{AFX_MSG(CMyButton)
	afx_msg void OnPaint();
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()

private:
    UINT m_itemAction;
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYBUTTON_H__4C9C57DA_61FC_4284_9F9C_78ED978514B2__INCLUDED_)

// MyButton.cpp : implementation file
//

#include "stdafx.h"
#include <assert.h>
#include "main.h"
#include "MyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CMyButton

CMyButton::CMyButton()
{
    m_itemAction = ODA_DRAWENTIRE;
}

CMyButton::~CMyButton()
{
}


BEGIN_MESSAGE_MAP(CMyButton, CButton)
	//{{AFX_MSG_MAP(CMyButton)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CMyButton message handlers

void CMyButton::PaintMe(CDC& dc, UINT itemAction) {
    BOOL bRc = FALSE;
    RECT rt;
    CDC memdc;
    CBitmap bitmap;

    /// 以物理DC为参照产生内存DC
    bRc = memdc.CreateCompatibleDC(&dc);
    assert(bRc);

    switch (itemAction) {
    case ODA_SELECT: ///< 按下
        bitmap.LoadBitmap(IDB_BITMAP_BTN_D);
        break;

    case ODA_FOCUS: ///< 焦点
        bitmap.LoadBitmap(IDB_BITMAP_BTN_H);
        break;

    case ODA_DRAWENTIRE: ///< 正常显示
    default:
        bitmap.LoadBitmap(IDB_BITMAP_BTN_N);
        break;
    }

    memdc.SelectObject(&bitmap);

    GetClientRect(&rt);
    /// 将内存DC拷贝进物理DC, 进行显示
    dc.BitBlt(rt.left, rt.top, rt.right, rt.bottom, &memdc, 0, 0, SRCCOPY);
}


void CMyButton::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
    PaintMe(dc, m_itemAction);

	// Do not call CButton::OnPaint() for painting messages
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    // TODO: Add your code to draw the specified item
    CDC dc;
    
    if (NULL == lpDrawItemStruct) {
        return;
    }
    
    if (ODT_BUTTON != lpDrawItemStruct->CtlType) {
        return;
    }
    
    dc.SetOutputDC(lpDrawItemStruct->hDC);
    m_itemAction = lpDrawItemStruct->itemAction;
    PaintMe(dc, m_itemAction);
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值