在vc里
要求用滑杆条来实现对话框背景色的渐变
有什么办法吗?
请大家帮我!最好能够给出示范程序
分不够再加

解决方案 »

  1.   

    WM_CTLCOLORDLG
    The WM_CTLCOLORDLG message is sent to a dialog box before the system draws the dialog box. By responding to this message, the dialog box can set its text and background colors using the specified display device context handle. A window receives this message through its WindowProc function. LRESULT CALLBACK WindowProc(
      HWND hwnd,       // handle to window
      UINT uMsg,       // WM_CTLCOLORDLG
      WPARAM wParam,   // handle to DC (HDC)
      LPARAM lParam    // handle to dialog box (HWND)
    );
    Parameters
    wParam 
    Handle to the device context for the dialog box. 
    lParam 
    Handle to the dialog box. 
    Return Values
    If an application processes this message, it must return a handle to a brush. The system uses the brush to paint the background of the dialog box. Res
    By default, the DefWindowProc function selects the default system colors for the dialog box. The system does not automatically destroy the returned brush. It is the application's responsibility to destroy the brush when it is no longer needed.The WM_CTLCOLORDLG message is never sent between threads. It is sent only within one thread. Note that the WM_CTLCOLORDLG message is sent to the dialog box itself; all of the other WM_CTLCOLOR* messages are sent to the owner of the control. If a dialog box procedure handles this message, it should cast the desired return value to an INT_PTR and return the value directly. If the dialog box procedure returns FALSE, then default message handling is performed. The DWL_MSGRESULT value set by the SetWindowLong function is ignored. 
      

  2.   

    重载WM_CTLCOLORDLG一般用来改变dlg上的控件的背景色(以及前景色,如Edit的字体颜色等)
    如果要用来设置dlg的背景色,可以试试这样://当滑到某处时
    SetBgColor();
      

  3.   

    添加背景色的代码:在头文件的类里加入定义: CBrush m_brush;
    在CPP的构造函数中加入:m_brush .CreateSolidBrush(RGB(200,200,255));
    然后编写函数(可以class wizard添加)
    HBRUSH CMykhxizl::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_DLG )
         return (HBRUSH) m_brush.GetSafeHandle ( );

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

  4.   

    用楼上的方法。但有一点小问题。m_brush .CreateSolidBrush(RGB(200,200,255));
    不但应该在构造函数中,而且在需要改变背景颜色时需要销毁前面的而且从新创建。而且在创建之后应该调用对话框的Invalidate方法。注意:在OnCtlColor中返回的画刷的颜色就是背景颜色。如果你需要
      

  5.   

    给你一个完整的代码:
    // fffffDlg.h : header file
    //#if !defined(AFX_FFFFFDLG_H__B25FF2AA_27F1_4508_BECA_10EEF1921A79__INCLUDED_)
    #define AFX_FFFFFDLG_H__B25FF2AA_27F1_4508_BECA_10EEF1921A79__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
    // CFffffDlg dialogclass CFffffDlg : public CDialog
    {
    // Construction
    public:
    CFffffDlg(CWnd* pParent = NULL); // standard constructor
       
    // Dialog Data
    //{{AFX_DATA(CFffffDlg)
    enum { IDD = IDD_FFFFF_DIALOG };
    // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CFffffDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    CBrush m_brush;
    HICON m_hIcon; // Generated message map functions
    //{{AFX_MSG(CFffffDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    afx_msg void OnButton1();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_FFFFFDLG_H__B25FF2AA_27F1_4508_BECA_10EEF1921A79__INCLUDED_)// fffffDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "fffff.h"
    #include "fffffDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
    // CFffffDlg dialogCFffffDlg::CFffffDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CFffffDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CFffffDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_brush .CreateSolidBrush(RGB(255,0,0)); m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CFffffDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CFffffDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CFffffDlg, CDialog)
    //{{AFX_MSG_MAP(CFffffDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_CTLCOLOR()
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CFffffDlg message handlersBOOL CFffffDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // 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

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CFffffDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// 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 CFffffDlg::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();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CFffffDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }HBRUSH CFffffDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       return (HBRUSH) m_brush.GetSafeHandle ( );

    }void CFffffDlg::OnButton1() 
    {
       m_brush.DeleteObject();
       m_brush.CreateSolidBrush(RGB(0,0,255));
       this->Invalidate();
    }
      

  6.   

    主要代码是:
    protected:
    CBrush m_brush;
    ////////////////////////////////
    CFffffDlg::CFffffDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CFffffDlg::IDD, pParent)
    {
    m_brush .CreateSolidBrush(RGB(255,0,0)); m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    //////////////////////////////////
    void CFffffDlg::OnButton1() 
    {
       m_brush.DeleteObject();
       m_brush.CreateSolidBrush(RGB(0,0,255));
       this->Invalidate();
    }
    /////////////////////////////////////
    HBRUSH CFffffDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       return (HBRUSH) m_brush.GetSafeHandle ( );

    }
      

  7.   

    要改变对话框的背景色很容易,假设你的工程为TestDlg,只要在
    CTestDlgApp::InitInstance()中调用SetDialogBkColor即可,但是这个函数是一个protected级别的,如果你需要在别处调用,为你的CTestDlgApp添加一个public成员函数例如叫CTestDlgApp::SetDlgBkColor(COLORREF bkColor)
    {
        SetDialogBkColor(bkColor); //这个函数也可以修改前景色,默认为黑色
    }在你的程序别处如下调用
    void CTestDlgDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    CTestDlgApp *pMainApp = (CTestDlgApp*)AfxGetApp();
    pMainApp->SetDlgBkColor(RGB(255,0,0));
    UpdateWindow();
    Invalidate();
    }
      

  8.   

    需要注意的是,SetDialogBkColor如果在CTestDlgApp::InitInstance中调用,那么一定要在
    CTestDlgDlg dlg;之前调用