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

📄 transbuttondlg.cpp

📁 可做出不同底色的按钮
💻 CPP
字号:
// TransButtonDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TransButton.h"
#include "TransButtonDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTransButtonDlg dialog

CTransButtonDlg::CTransButtonDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTransButtonDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTransButtonDlg)
		// 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 CTransButtonDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTransButtonDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTransButtonDlg, CDialog)
	//{{AFX_MSG_MAP(CTransButtonDlg)
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTransButtonDlg message handlers

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

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_hBmBkgnd = ::LoadBitmap( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1) );
	if( m_hBmBkgnd )
	{
		m_hBrCtl[0] = GetBkBrush( GetSafeHwnd(), IDC_BUTTON_TEST1, m_hBmBkgnd );
		m_hBrCtl[1] = GetBkBrush( GetSafeHwnd(), IDC_STATIC_TEST1, m_hBmBkgnd );
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}
HBRUSH CTransButtonDlg::GetBkBrush( HWND hWnd, UINT nID, HBITMAP hBmBk )
{
	#define _X(x)	(x.left)
	#define _Y(x)	(x.top)
	#define _W(x)	(x.right - x.left)
	#define _H(x)	(x.bottom - x.top)
	HWND hWndCtrl;
	hWndCtrl = ::GetDlgItem( hWnd, nID );
	HBRUSH	hBrushCtrl = NULL;
	if( NULL != hWndCtrl )
	{
		RECT rcCtrl;
		::GetWindowRect( hWndCtrl, &rcCtrl );
		::ScreenToClient(hWnd, (LPPOINT)&rcCtrl);
		::ScreenToClient(hWnd, ((LPPOINT)&rcCtrl)+1);

		HDC hDC = ::GetDC(hWnd);
		HDC hMemDCBk = CreateCompatibleDC( hDC );
		HDC hMemDCCtrl = CreateCompatibleDC( hDC );

		HBITMAP hBmCtrl = CreateCompatibleBitmap( hDC, _W(rcCtrl), _H(rcCtrl) );
		HBITMAP hBmOldBk;
		HBITMAP hBmOldCtrl;

		hBmOldBk = (HBITMAP) ::SelectObject( hMemDCBk, hBmBk );
		hBmOldCtrl = (HBITMAP) ::SelectObject( hMemDCCtrl, hBmCtrl );

		::BitBlt( hMemDCCtrl, 0, 0, _W(rcCtrl), _H(rcCtrl), hMemDCBk, _X(rcCtrl), _Y(rcCtrl), SRCCOPY );

		::SelectObject(hMemDCCtrl, hBmOldCtrl );
		::SelectObject(hMemDCBk, hBmOldBk );
		
		hBrushCtrl = ::CreatePatternBrush( hBmCtrl );

		DeleteObject( hBmCtrl );

		::DeleteDC( hMemDCBk );
		::DeleteDC( hMemDCCtrl );
		::ReleaseDC( hWnd, hDC );
	}

	return hBrushCtrl;
}



void CTransButtonDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CDC memDC;
	CBitmap bmBkgnd;
	bmBkgnd.Attach( m_hBmBkgnd );

	memDC.CreateCompatibleDC(&dc);
	CBitmap *pOldBm = memDC.SelectObject( &bmBkgnd );

	BITMAP bm;
	bmBkgnd.GetObject(sizeof(bm), &bm);
	

	dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0, SRCCOPY );
	memDC.SelectObject( pOldBm );
	bmBkgnd.Detach();
	// Do not call CDialog::OnPaint() for painting messages
}

HBRUSH CTransButtonDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(pWnd->GetDlgCtrlID()   ==   IDC_BUTTON_TEST1 )
	{
		pDC->SetTextColor(RGB(255, 0, 0));
		pDC->SetBkMode( TRANSPARENT ) ; 
		return m_hBrCtl[0];
	}
	else if(pWnd->GetDlgCtrlID()   ==   IDC_STATIC_TEST1)
	{
		pDC->SetTextColor(RGB(255, 0, 0));
		pDC->SetBkMode( TRANSPARENT ) ;
		return m_hBrCtl[1];
	}

	// TODO: Return a different brush if the default is not desired
	return hbr;
}

⌨️ 快捷键说明

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