⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 advbuttondlg.cpp

📁 深入剖析vcCodeCHAPTER11.1特色按钮源代码
💻 CPP
字号:
// AdvButtonDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AdvButton.h"
#include "AdvButtonDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAdvButtonDlg dialog

CAdvButtonDlg::CAdvButtonDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAdvButtonDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAdvButtonDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CAdvButtonDlg, CDialog)
	//{{AFX_MSG_MAP(CAdvButtonDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(MY_BTN1, OnButton)
	ON_BN_CLICKED(MY_BTN2, OnButton)
	ON_BN_CLICKED(MY_BTN3, OnButton)
	ON_BN_CLICKED(MY_BTN4, OnButton)
	ON_BN_CLICKED(MY_BTN5, OnButton)
	ON_BN_CLICKED(MY_BTN6, OnButton)
	ON_BN_CLICKED(MY_BTN7, OnButton)
	ON_BN_CLICKED(MY_BTN8, OnButton)
	ON_BN_CLICKED(MY_BTN9, OnButton)
	ON_BN_CLICKED(MY_BTN10, OnButton)
	ON_BN_CLICKED(MY_BTN11, OnButton)
	ON_BN_CLICKED(MY_BTN12, OnButton)
	ON_BN_CLICKED(MY_BTN13, OnButton)
	ON_BN_CLICKED(MY_BTN14, OnButton)
	ON_BN_CLICKED(MY_BTN15, OnButton)
	ON_BN_CLICKED(MY_BTN16, OnButton)
	ON_BN_CLICKED(MY_BTN17, OnButton)
	ON_BN_CLICKED(MY_BTN18, OnButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvButtonDlg message handlers

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

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	HRGN r; 

	r = CreateRectRgn(0, 0, 63, 31);
	m_Btn1.Create("按钮 1", WS_CHILD | WS_VISIBLE, CPoint(15, 15), r, this, MY_BTN1, RGB(255, 255, 0));
	DeleteObject(r);
	//创建一个矩形按钮
	r = CreateEllipticRgn(0, 0, 63, 31);
	m_Btn2.Create("按钮 2", WS_CHILD | WS_VISIBLE, CPoint(95, 15), r, this, MY_BTN2, 2, GetSysColor(COLOR_BTNFACE), RGB(0, 255, 0), RGB(0, 255, 0), GetSysColor(COLOR_BTNFACE)); 
	DeleteObject(r);
	//创建一个椭圆按钮
	HRGN rgnR = CreateRectRgn(0, 0, 127, 31);
	HRGN rgnE = CreateEllipticRgn(0, 0, 127, 31);
	OffsetRgn(rgnR, 63, 0);
	CombineRgn(rgnE, rgnE, rgnR, RGN_DIFF);
	m_Btn3.Create("  按钮 3", WS_CHILD | WS_VISIBLE, CPoint(175, 15), rgnE, this, MY_BTN3, 2, GetSysColor(COLOR_BTNFACE), RGB(156, 175, 194), RGB(237, 175, 71), GetSysColor(COLOR_BTNFACE)); 
	//创建一个椭圆和矩形合并形成的按钮
	rgnR = CreateRectRgn(0, 0, 127, 31);
	rgnE = CreateEllipticRgn(0, 0, 127, 31);
	OffsetRgn(rgnR, -63, 0);
	CombineRgn(rgnE, rgnE, rgnR, RGN_DIFF);
	OffsetRgn(rgnE, -64, 0);
	m_Btn4.Create("按钮 4  ", WS_CHILD | WS_VISIBLE | WS_DISABLED, CPoint(175 + 64, 15), rgnE, this, MY_BTN4, 2, GetSysColor(COLOR_BTNFACE), RGB(156, 175, 194), RGB(237, 175, 71), GetSysColor(COLOR_BTNFACE)); 
	DeleteObject(rgnE);
	DeleteObject(rgnR);

	HDC hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
	HRGN c;
	LOGFONT lf;
	GetFont() -> GetLogFont(&lf);
	lf.lfHeight = -128;
	lf.lfWeight = 1000;
	HFONT hFont = CreateFontIndirect(&lf);
	HFONT hOldFont = (HFONT) SelectObject(hDC, hFont);
	c = CreateRectRgn(0, 0, 0, 0);
	
	int mode = SetBkMode(hDC, TRANSPARENT);
	BeginPath(hDC);
	TextOut(hDC, 0, 0, "$", 1);
	EndPath(hDC);
	c = PathToRegion(hDC);
	SetBkMode(hDC, mode);
	//创建一个字符按钮,这里利用了BeginPath、EndPath函数
	//来获得字符区域
	m_Btn5.Create("", WS_CHILD | WS_VISIBLE, CPoint(15, 63), c, this, MY_BTN5, RGB(255, 255, 0)); 

	mode = SetBkMode(hDC, TRANSPARENT);
	BeginPath(hDC);
	TextOut(hDC, 0, 0, "@", 1);
	EndPath(hDC);
	c = PathToRegion(hDC);
	SetBkMode(hDC, mode);
	m_Btn6.Create("", WS_CHILD | WS_VISIBLE, CPoint(15 + 64, 63), c, this, MY_BTN6, RGB(255, 0, 255)); 

	SelectObject(hDC, hOldFont);
	DeleteObject(hFont);
	DeleteObject(c);

	HRGN cr; 

	BeginPath(hDC);
	MoveToEx(hDC, 0, 0, NULL);
	LineTo(hDC, 64, 0);
	LineTo(hDC, 64, 16);
	LineTo(hDC, 48, 32);
	LineTo(hDC, 0, 32);
	LineTo(hDC, 0, 0);
	EndPath(hDC);
	cr = CreateRectRgn(0, 0, 63, 63);
	cr = PathToRegion(hDC);
	m_Btn7.Create("保存  ", WS_CHILD | WS_VISIBLE, CPoint(15 + 64 + 128, 63), cr, this, MY_BTN7, 1, RGB(254, 247, 211), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 
	//创建一个利用直线拼凑的图形按钮
	BeginPath(hDC);
	MoveToEx(hDC, 64, 16, NULL);
	LineTo(hDC, 64, 32);
	LineTo(hDC, 48, 32);
	LineTo(hDC, 64, 16);
	EndPath(hDC);
	cr = PathToRegion(hDC);
	m_Btn8.Create("", WS_CHILD | WS_VISIBLE, CPoint(15 + 64 + 128 + 48, 63 + 16), cr, this, MY_BTN8, 1, RGB(247, 211, 254), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 

	
	DeleteObject(cr);


	HRGN r1;
	HRGN r2;
	BeginPath(hDC);
	MoveToEx(hDC, 0, 32, NULL);
	LineTo(hDC, 48, 32);
	LineTo(hDC, 48, 16);
	LineTo(hDC, 96, 48);
	LineTo(hDC, 48, 80);
	LineTo(hDC, 48, 64);
	LineTo(hDC, 0, 64);
	LineTo(hDC, 0, 32);
	EndPath(hDC);
	cr = CreateRectRgn(0, 0, 63, 63);
	cr = PathToRegion(hDC);
	m_Btn9.Create("按钮", WS_CHILD | WS_VISIBLE, CPoint(32 + 64 + 96, 63 + 16 + 128), cr, this, MY_BTN9, 2, RGB(250, 207, 194), RGB(255, 0, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

	DeleteObject(cr);
	DeleteObject(hDC);

	hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
	BeginPath(hDC);
	MoveToEx(hDC, 31, 15, NULL);
	CPoint p[7];
	p[ 0] = CPoint(5, 0);
	p[ 1] = CPoint(0, 55);
	p[ 2] = CPoint(0, 28);
	p[ 3] = CPoint(31, 64);
	p[ 4] = CPoint(59, 55);
	p[ 5] = CPoint(59, 0);
	p[ 6] = CPoint(31, 15);

	PolyBezier(hDC, &p[0], 7);
	EndPath(hDC);
	cr = CreateRectRgn(0, 0, 63, 63);
	cr = PathToRegion(hDC);
	m_Btn10.Create("按钮 10", WS_CHILD | WS_VISIBLE, CPoint(15, 63 + 16 + 64 + 64), cr, this, MY_BTN10, 2, RGB(151, 244, 219), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 

	DeleteObject(cr);
	DeleteObject(hDC);

	c = CreateRectRgn(16, 0, 80, 31);
	r1 = CreateEllipticRgn(0, 0, 32, 32);
	r2 = CreateEllipticRgn(64, 0, 96, 32);
	CombineRgn(c, c, r1, RGN_OR);
	CombineRgn(c, c, r2, RGN_OR);
	m_Btn11.Create("按钮 11", WS_CHILD | WS_VISIBLE, CPoint(15 + 64, 63 + 32 + 64 + 64), c, this, MY_BTN11, 2, GetSysColor(COLOR_BTNFACE), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 
	DeleteObject(c);
	DeleteObject(r1);
	DeleteObject(r2);

	c = CreateRectRgn(8, 0, 55, 31);
	r1 = CreateEllipticRgn(48, 8, 15 + 48, 15 + 8);
	CombineRgn(c, c, r1, RGN_OR);
	m_Btn12.Create("文件", WS_CHILD | WS_VISIBLE, CPoint(16, 63 + 32 + 64 + 128), c, this, MY_BTN12, 1, RGB(151, 219, 244), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 
	r1 = CreateEllipticRgn(0, 8, 15, 15 + 8);
	CombineRgn(c, c, r1, RGN_DIFF);
	m_Btn13.Create("编辑", WS_CHILD | WS_VISIBLE, CPoint(16 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN13, 1, RGB(151, 219, 244), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));
	m_Btn14.Create("视图", WS_CHILD | WS_VISIBLE, CPoint(16 + 48 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN14, 1, RGB(151, 219, 244), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));
	m_Btn15.Create("插入",WS_CHILD |WS_VISIBLE, CPoint(16 + 48 + 48 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN15, 1, RGB(151, 219, 244), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));
	m_Btn16.Create("工具", WS_CHILD |WS_VISIBLE, CPoint(16 + 48 + 48 + 48 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN16, 1, RGB(151, 219, 244), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));
	m_Btn17.Create("项目",  WS_CHILD |WS_VISIBLE, CPoint(16 + 48 + 48 + 48 + 48 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN17, 1, RGB(151, 244, 219), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));
	c = CreateRectRgn(8, 0, 63, 31);
	r1 = CreateEllipticRgn(0, 8, 15, 15 + 8);
	CombineRgn(c, c, r1, RGN_DIFF);
	m_Btn18.Create("按钮",  WS_CHILD |WS_VISIBLE, CPoint(16 + 48 + 48 + 48 + 48 + 48 + 48, 63 + 32 + 64 + 128), c, this, MY_BTN18, 1, RGB(244, 151, 219), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE));

	DeleteObject(c);
	DeleteObject(r1);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CAdvButtonDlg::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();
	}
}

HCURSOR CAdvButtonDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAdvButtonDlg::OnButton() 
{
	MessageBeep(0xFFFFFFFF);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -