Giter VIP home page Giter VIP logo

imagebutton's Introduction

CImageButton

MFC/WTL CImageButton 支持[BMP,GIF,JPEG,PNG,TIFF], 支持高DPI

CImageButton是一个支持4种状态图形显示的按钮控件

  • 正常状态
  • 鼠标放上去的悬停状态
  • 鼠标按下去的状态
  • 按钮被禁用的状态

以上4种状态可分别设置为不同的图片, 支持PNG图片透明通道

使用方法

MFC

  • 添加ImageButton.hImageButton.cpp到项目中
  • CxxxDlg类包含头文件 ImageButton.h
  • 用类向导定义CImageButton类型的变量到资源ID
  • OnInitDialog()虚函数中调用btn.SetImages()方法

CxxxDlg.h

#pragma once

#include "ImageButton.h"


// CMFCDemoDlg 对话框
class CMFCDemoDlg : public CDialogEx
{
    // ...

private:
	CImageButton m_btnTest;
};

CxxxDlg.cpp

BOOL CMFCDemoDlg::OnInitDialog()
{
	
    // ....

	// 设置按钮图片
	m_btnTest.SetImages(IDB_PNG1, IDB_PNG2, IDB_PNG3, IDB_PNG4, _T("PNG"));
	// 开启自动大小, 跟随图片宽高
	m_btnTest.AutoSize();

    // ....
}

WTL

  • 添加ImageButton.h项目中
  • CMainDlg类包含头文件 ImageButton.h
  • CMainDlgBEGIN_MSG_MAP(CMainDlg)END_MSG_MAP()之间加REFLECT_NOTIFICATIONS()用来反射WM_DRAWITEM消息, 随后自行加入COMMAND_HANDLER()绑定按钮事件
  • 添加CImageButton类型的成员变量
  • OnInitDialog()函数中调用btn.SubclassWindow()方法与btn.SetImages()方法

CMainDlg.h

#pragma once

#include "ImageButton.h"


class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>, public CMessageFilter, public CIdleHandler
{
    // ...
    	BEGIN_MSG_MAP(CMainDlg)
		COMMAND_HANDLER(IDC_BUTTON_TEST, BN_CLICKED, OnBnClickedButtonTest) // 按钮事件
		REFLECT_NOTIFICATIONS() // 反射 WM_MEASUREITEM 和 WM_DRAWITEM 消息
	END_MSG_MAP()

    // ...
private:
	CImageButton m_btnTest;
};

CMainDlg.cpp

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// ...

    // 子类化控件
	m_btnTest.SubclassWindow(GetDlgItem(IDC_BUTTON_TEST)); 
	// 设置按钮图片
	m_btnTest.SetImages(IDB_PNG1, IDB_PNG2, IDB_PNG3, IDB_PNG4, _T("PNG"));
	// 开启自动大小, 跟随图片宽高
	m_btnTest.AutoSize();

	// ...
	
	return TRUE;
}

// 按钮事件
LRESULT CMainDlg::OnBnClickedButtonTest(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MessageBox(_T("Test"), _T(""), MB_OK | MB_ICONINFORMATION);

	return 0;
}

说明

IDB_PNG1, IDB_PNG2, IDB_PNG3, IDB_PNG4 均为资源ID, 如何导入资源请去百度上谷歌一下

imagebutton's People

Contributors

int2e avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

imagebutton's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.