读书人

改变按钮背景颜色如何不好使

发布时间: 2012-04-04 16:38:51 作者: rapoo

改变按钮背景颜色怎么不好使

C/C++ code
#include<afxwin.h>#include <afxtempl.h>class CMainWindow : public CFrameWnd{public:    DECLARE_MESSAGE_MAP()public:    CMainWindow();    CString strClassName;    HBRUSH h;    CButton button;protected:    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);    void click();public:    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);};



C/C++ code
#include "CMainWindow.h"#include<locale.h>#include<TCHAR.H>/*#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")*/#include "resource.h"BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)    ON_BN_CLICKED(100,click)        ON_WM_CTLCOLOR()END_MESSAGE_MAP()BOOL CMainWindow::PreCreateWindow(CREATESTRUCT& cs){    CFrameWnd::PreCreateWindow(cs);    cs.dwExStyle = cs.dwExStyle & (!WS_EX_CLIENTEDGE);    return TRUE;}CMainWindow::CMainWindow(){    strClassName = AfxRegisterWndClass        (CS_HREDRAW | CS_VREDRAW,        (AfxGetApp()) ->LoadStandardCursor(IDC_ARROW) ,        (HBRUSH)(COLOR_3DFACE+1),        AfxGetApp() ->LoadStandardIcon(IDI_WINLOGO) );    Create(strClassName,L"",WS_OVERLAPPEDWINDOW | WS_VSCROLL);        CRect r;    r.left = 10;    r.top = 220;    r.right = 110;    r.bottom = 320;    button.Create(L"CButton",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD /*| BS_ICON*/, r,this,100);//    button.SetIcon( AfxGetApp()->LoadIconW(IDI_ICON1));    h = ::CreateSolidBrush( RGB(0,255,0) );}void CMainWindow::click(){    ::setlocale(LC_ALL,"chs");}HBRUSH CMainWindow::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){    if( pWnd->m_hWnd == button.m_hWnd )    {        return h;    }    return CFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor);}


[解决办法]
按钮的背景不能通过WM_CTLCOLOR来设置,你需要自绘Button,添加处理DrawItem虚函数
[解决办法]
1。按钮要ownerdraw。
2。HBRUSH CTest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_BTN)
{
HBRUSH hbr=(HBRUSH)::CreateSolidBrush(RGB(255,0,0));//all
return hbr;
}
//
3。void CTest::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if(nIDCtl==IDC_BUTTON1)
{
return;// 画其他!
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

读书人网 >VC/MFC

热点推荐