哎,这种代码是我在国外网站上找到的。 一直用的。
#define _X(x) (x.left)
#define _Y(x) (x.top)
#define _W(x) (x.right - x.left)
#define _H(x) (x.bottom - x.top)
//**************************************************************************************
//
// GetBkBrush( HWND hWnd, UINT nID, HBITMAP hBmBk )
//
// Creates Background brush for the control specified by nID
// according to its position in the parent dialog window.
//
// hWnd [in]: Handle to the parent window
// nID [in]: Control ID
// hBmBk [in]: Bitmap handle of the parent dialog's background image
//
//
//**************************************************************************************
HBRUSH GetBkBrush( HWND hWnd, UINT nID, HBITMAP hBmBk )
{
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;
}
使用例子:
m_hBmBkgnd = ::LoadBitmap( _Module.GetResourceInstance(), MAKEINTRESOURCE(IDB_BT_NEWADD) );
if( m_hBmBkgnd )
{
m_hBrCtl[0] = GetBkBrush( m_hWnd, IDC_ADD_NAME, m_hBmBkgnd );
m_hBrCtl[1] = GetBkBrush( m_hWnd, IDC_ADD_MOBILE, m_hBmBkgnd );
m_hBrCtl[2] = GetBkBrush( m_hWnd, IDC_ADD_WORK, m_hBmBkgnd );
m_hBrCtl[3] = GetBkBrush( m_hWnd, IDC_ADD_HOME, m_hBmBkgnd );
m_hBrCtl[4] = GetBkBrush( m_hWnd, IDC_ADD_OTHER, m_hBmBkgnd );
}
else
{
m_hBrCtl[0] = NULL;
m_hBrCtl[1] = NULL;
m_hBrCtl[2] = NULL;
m_hBrCtl[3] = NULL;
m_hBrCtl[4] = NULL;
}
LRESULT OnCtlColorStatic(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
HDC hDCBtn = (HDC) wParam;
HWND hWndCtrl = (HWND) lParam;
UINT nID = ::GetDlgCtrlID( hWndCtrl );
::SetBkMode( hDCBtn, TRANSPARENT );
switch( nID )
{
case IDC_ADD_NAME:
return (LRESULT)m_hBrCtl[0];
case IDC_ADD_MOBILE:
return (LRESULT)m_hBrCtl[1];
case IDC_ADD_WORK:
return (LRESULT)m_hBrCtl[2];
case IDC_ADD_HOME:
return (LRESULT)m_hBrCtl[3];
case IDC_ADD_OTHER:
return (LRESULT)m_hBrCtl[4];
default:
break;
}
return 0;
}