1. 程式人生 > 實用技巧 >對話方塊透明,控制元件不透明(控制元件顯示背景圖)

對話方塊透明,控制元件不透明(控制元件顯示背景圖)

    virtual BOOL OnInitDialog();
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);



ON_WM_CTLCOLOR()





BOOL CDlgXXX::OnInitDialog()
{
    CBCGPDialog::OnInitDialog();
    COLORREF maskColor = RGB(255, 255, 255);
    m_brush_.CreateSolidBrush(maskColor);

    LONG nExStyle = ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
    nExStyle 
|= WS_EX_LAYERED; ::SetWindowLong(m_hWnd, GWL_EXSTYLE, nExStyle); SetLayeredWindowAttributes(maskColor, 128, LWA_COLORKEY); // 透明度範圍是0~255 CRect rtClient(0, 0, 22, 42); MoveWindow(&rtClient); m_btn_arrow_.MoveWindow(rtClient); m_btn_arrow_.m_nFlatStyle = CBCGPButton::FlatStyle::BUTTONSTYLE_NOBORDERS; m_btn_arrow_.m_bTransparent
= TRUE; m_btn_arrow_.SetMouseCursorHand(); if (m_nDir_ == 0) //左邊 { m_btn_arrow_.SetImage(IDB_PNG_LEFT_CLOSE,IDB_PNG_LEFT_CLOSE_HOT,0,IDB_PNG_LEFT_CLOSE_HOT); } else if (m_nDir_ == 1) //右邊 { m_btn_arrow_.SetImage(IDB_PNG_RIGHT_CLOSE,IDB_PNG_RIGHT_CLOSE_HOT,0,IDB_PNG_RIGHT_CLOSE_HOT); }
return TRUE; } HBRUSH CDlgXXX::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(nCtlColor == CTLCOLOR_DLG) { return (HBRUSH)(m_brush_.m_hObject); } else if (nCtlColor == CTLCOLOR_BTN) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(0, 0, 0)); return HBRUSH(GetStockObject(HOLLOW_BRUSH)); } return hbr; } 別忘了析構 CDlgXXX::~CDlgXXX() { m_brush_.DeleteObject(); }