Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. change background color of dialog

change background color of dialog

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Malli_S

    Delete the old brush and update the brush color by creating the new one

    m_brush.CreateSolidBrush(RGB(255, 255, 255));

    with the selected color.

    [Delegates]      [Virtual Desktop]      [Tray Me !]
    -Malli...! :rose:****

    Z Offline
    Z Offline
    zhiyuan16
    wrote on last edited by
    #6

    thanks malli don't understand well, i wanna change different color for different person, when someone click change background color. so i need a changbackground(COLORREF color), don't how to write down it. thanks. could you help me. thanks. i tried your that way, just can change once.

    M 1 Reply Last reply
    0
    • Z zhiyuan16

      thanks malli don't understand well, i wanna change different color for different person, when someone click change background color. so i need a changbackground(COLORREF color), don't how to write down it. thanks. could you help me. thanks. i tried your that way, just can change once.

      M Offline
      M Offline
      Malli_S
      wrote on last edited by
      #7

      I hope following code snippet will help you. I've not included the details in depth. Just try it out.

      class CMeiTengDlg: public CDialog
      {
      ...
      //Add the member variable.
      protected:
      CBrush m_objMyBrush;

      ...
      };

      BOOL CMyDlg::OnInitDialog()
      {
      ...
      //initialize it with default brush color (back color)
      m_objMyBrush.CreateSolidBrush(RGB(255, 255, 255)); // color white brush
      ...
      }

      BOOL CMyDlg::DistroyWindow()
      {
      //delete it while distroying the dialog.
      m_objMyBrush.DeleteObject();
      }

      HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
      /*
      ** No need to do this!
      **
      ** HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      */

      /*
      ** Return your brush.
      */
      return m_objMyBrush;
      }

      void CMeiTengDlg::OnBnClickedChangebkcolor()
      {
      // TODO: Add your control notification handler code here
      CColorDialog dlg;
      if (dlg.DoModal() == IDOK)
      COLORREF color=dlg.GetColor();
      //change my background color of dialog code here
      changbackground(color);
      }

      //probably this is what you were searching for
      void CMeiTengDlg::changbackground(COLORREF color)
      {
      //change the brush color
      m_objMyBrush.DeleteObject();
      m_objMyBrush.CreateSolidBrush( color );
      Invalidate(TRUE);
      }

      [Delegates]      [Virtual Desktop]      [Tray Me !]
      -Malli...! :rose:****

      Z 1 Reply Last reply
      0
      • M Malli_S

        I hope following code snippet will help you. I've not included the details in depth. Just try it out.

        class CMeiTengDlg: public CDialog
        {
        ...
        //Add the member variable.
        protected:
        CBrush m_objMyBrush;

        ...
        };

        BOOL CMyDlg::OnInitDialog()
        {
        ...
        //initialize it with default brush color (back color)
        m_objMyBrush.CreateSolidBrush(RGB(255, 255, 255)); // color white brush
        ...
        }

        BOOL CMyDlg::DistroyWindow()
        {
        //delete it while distroying the dialog.
        m_objMyBrush.DeleteObject();
        }

        HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
        {
        /*
        ** No need to do this!
        **
        ** HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        */

        /*
        ** Return your brush.
        */
        return m_objMyBrush;
        }

        void CMeiTengDlg::OnBnClickedChangebkcolor()
        {
        // TODO: Add your control notification handler code here
        CColorDialog dlg;
        if (dlg.DoModal() == IDOK)
        COLORREF color=dlg.GetColor();
        //change my background color of dialog code here
        changbackground(color);
        }

        //probably this is what you were searching for
        void CMeiTengDlg::changbackground(COLORREF color)
        {
        //change the brush color
        m_objMyBrush.DeleteObject();
        m_objMyBrush.CreateSolidBrush( color );
        Invalidate(TRUE);
        }

        [Delegates]      [Virtual Desktop]      [Tray Me !]
        -Malli...! :rose:****

        Z Offline
        Z Offline
        zhiyuan16
        wrote on last edited by
        #8

        thanks Malli i want to confirm i just wanna change my CMeiTengDlg background color, what mentioned above is CMyDlg, CAboutDlg, so i no need to add one class, right? so i followed your idea, but after compiled, one error is c:\documents and settings\zhiyuan li\desktop\meiteng\meiteng\meitengdlg.cpp(163) : error C4716: 'CMeiTengDlg::DistroyWindow' : must return a value Build log was saved at "file://c:\Documents and Settings\ZhiYuan Li\Desktop\MeiTeng\MeiTeng\Debug\BuildLog.htm" MeiTeng - 1 error(s), 0 warning(s) by the way,could i get your email, and keep in touch. thanks.

        M 1 Reply Last reply
        0
        • Z zhiyuan16

          thanks Malli i want to confirm i just wanna change my CMeiTengDlg background color, what mentioned above is CMyDlg, CAboutDlg, so i no need to add one class, right? so i followed your idea, but after compiled, one error is c:\documents and settings\zhiyuan li\desktop\meiteng\meiteng\meitengdlg.cpp(163) : error C4716: 'CMeiTengDlg::DistroyWindow' : must return a value Build log was saved at "file://c:\Documents and Settings\ZhiYuan Li\Desktop\MeiTeng\MeiTeng\Debug\BuildLog.htm" MeiTeng - 1 error(s), 0 warning(s) by the way,could i get your email, and keep in touch. thanks.

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #9

          Sorry Zhiyuan16, I forgot to replace the CMyDls with your dialog class name. And you should return value from DestroyWindow function. I had deleted rest of the code (and put ....) considering you'd put the required one. For details have a look at here[^]

          [Delegates]      [Virtual Desktop]      [Tray Me !]
          -Malli...! :rose:****

          Z 1 Reply Last reply
          0
          • M Malli_S

            Sorry Zhiyuan16, I forgot to replace the CMyDls with your dialog class name. And you should return value from DestroyWindow function. I had deleted rest of the code (and put ....) considering you'd put the required one. For details have a look at here[^]

            [Delegates]      [Virtual Desktop]      [Tray Me !]
            -Malli...! :rose:****

            Z Offline
            Z Offline
            zhiyuan16
            wrote on last edited by
            #10

            thanks Malli comiling had no problem, but still no color change of background after i chose different color. where is problem, Malli? thanks

            Z 1 Reply Last reply
            0
            • Z zhiyuan16

              thanks Malli comiling had no problem, but still no color change of background after i chose different color. where is problem, Malli? thanks

              Z Offline
              Z Offline
              zhiyuan16
              wrote on last edited by
              #11

              dear Malli could you help me to check is it right below: thanks a lot BOOL CMeiTengDlg::OnDestroyWindow() { //delete it while distroying the dialog. m_objMyBrush.DeleteObject(); return TRUE; } HBRUSH CMeiTengDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { return m_objMyBrush; } void CMeiTengDlg::SetBackgroundColor(COLORREF color) { m_objMyBrush.DeleteObject(); m_objMyBrush.CreateSolidBrush( color ); //redraw Invalidate(TRUE); } void CMeiTengDlg::OnBnClickedChangebkcolor() { // TODO: Add your control notification handler code here CColorDialog dlg; if (dlg.DoModal() == IDOK) color=dlg.GetColor(); //change my background color of dialog code here SetBackgroundColor(color); }

              M 1 Reply Last reply
              0
              • Z zhiyuan16

                dear Malli could you help me to check is it right below: thanks a lot BOOL CMeiTengDlg::OnDestroyWindow() { //delete it while distroying the dialog. m_objMyBrush.DeleteObject(); return TRUE; } HBRUSH CMeiTengDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { return m_objMyBrush; } void CMeiTengDlg::SetBackgroundColor(COLORREF color) { m_objMyBrush.DeleteObject(); m_objMyBrush.CreateSolidBrush( color ); //redraw Invalidate(TRUE); } void CMeiTengDlg::OnBnClickedChangebkcolor() { // TODO: Add your control notification handler code here CColorDialog dlg; if (dlg.DoModal() == IDOK) color=dlg.GetColor(); //change my background color of dialog code here SetBackgroundColor(color); }

                M Offline
                M Offline
                Malli_S
                wrote on last edited by
                #12

                The above code worked for me (only 'color' variable I found missing). Do you have any overlapping control on your dialog ? And I think you should call return CDialog::DestroyWindow(); in your dialog's DistroyWindow();

                [Delegates]      [Virtual Desktop]      [Tray Me !]
                -Malli...! :rose:****

                Z 1 Reply Last reply
                0
                • M Malli_S

                  The above code worked for me (only 'color' variable I found missing). Do you have any overlapping control on your dialog ? And I think you should call return CDialog::DestroyWindow(); in your dialog's DistroyWindow();

                  [Delegates]      [Virtual Desktop]      [Tray Me !]
                  -Malli...! :rose:****

                  Z Offline
                  Z Offline
                  zhiyuan16
                  wrote on last edited by
                  #13

                  no other control.. Malli could you check for me the following my dialog .h and .cpp files thanks. CMeiTengDlg.h file

                  // MeiTengDlg.h : header file
                  //

                  #pragma once
                  #include "afxwin.h"

                  // CMeiTengDlg dialog
                  class CMeiTengDlg : public CDialog
                  {
                  // Construction
                  public:
                  CMeiTengDlg(CWnd* pParent = NULL); // standard constructor
                  void SetBackgroundColor(COLORREF color);

                  // Dialog Data
                  enum { IDD = IDD_MEITENG_DIALOG };

                  protected:
                  virtual void DoDataExchange(CDataExchange\* pDX);	// DDX/DDV support
                  

                  // Implementation
                  protected:
                  HICON m_hIcon;
                  //CBrush m_brush;
                  //COLORREF m_crBackGnd;
                  CBrush m_objMyBrush;
                  COLORREF color;

                  // Generated message map functions
                  virtual BOOL OnInitDialog();
                  virtual BOOL OnDistroyWindow();
                  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);
                  	DECLARE\_MESSAGE\_MAP()
                  

                  public:

                  CComboBox m\_pComboBox;
                  //afx\_msg HBRUSH OnCtlColor(CDC\* pDC, CWnd\* pWnd, UINT nCtlColor);
                  afx\_msg void OnBnClickedChangebkcolor();
                  

                  };

                  CMeiTengDlg.cpp file

                  // MeiTengDlg.cpp : implementation file
                  //

                  #include "stdafx.h"
                  #include "MeiTeng.h"
                  #include "MeiTengDlg.h"

                  #ifdef _DEBUG
                  #define new DEBUG_NEW
                  #endif

                  // CAboutDlg dialog used for App About

                  class CAboutDlg : public CDialog
                  {
                  public:
                  CAboutDlg();

                  // Dialog Data
                  enum { IDD = IDD_ABOUTBOX };

                  protected:
                  virtual void DoDataExchange(CDataExchange\* pDX);    // DDX/DDV support
                  

                  // Implementation
                  protected:
                  DECLARE_MESSAGE_MAP()
                  };

                  CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
                  {
                  }

                  void CAboutDlg::DoDataExchange(CDataExchange* pDX)
                  {
                  CDialog::DoDataExchange(pDX);
                  }

                  BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
                  END_MESSAGE_MAP()

                  // CMeiTengDlg dialog

                  CMeiTengDlg::CMeiTengDlg(CWnd* pParent /*=NULL*/)
                  : CDialog(CMeiTengDlg::IDD, pParent)
                  {
                  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
                  }

                  void CMeiTengDlg::DoDataExchange(CDataExchange* pDX)
                  {
                  CDialog::DoDataExchange(pDX);
                  DDX_Control(pDX, IDC_BITMAP, m_pComboBox);
                  // DDX_Control(pDX, IDC_Enjoystatic, m_pEnjoyStatic);
                  }

                  BEGIN_MESSAGE_MAP(CMeiTengDlg, CDialog)
                  ON_WM_SYSCOMMAND()
                  ON_WM_PAINT()
                  ON_WM_QUERYDRAGICON()
                  //}}AFX_MSG_MAP

                  ON\_BN\_CLICKED(IDC\_ChangeBKColor, &CMeiTengDlg::OnBnClickedChangebkcolor)
                  

                  END_MESSAGE_MAP()

                  // CMeiTengDlg message handlers

                  BOOL CMeiTengDlg::OnInitDialog()

                  M 1 Reply Last reply
                  0
                  • Z zhiyuan16

                    no other control.. Malli could you check for me the following my dialog .h and .cpp files thanks. CMeiTengDlg.h file

                    // MeiTengDlg.h : header file
                    //

                    #pragma once
                    #include "afxwin.h"

                    // CMeiTengDlg dialog
                    class CMeiTengDlg : public CDialog
                    {
                    // Construction
                    public:
                    CMeiTengDlg(CWnd* pParent = NULL); // standard constructor
                    void SetBackgroundColor(COLORREF color);

                    // Dialog Data
                    enum { IDD = IDD_MEITENG_DIALOG };

                    protected:
                    virtual void DoDataExchange(CDataExchange\* pDX);	// DDX/DDV support
                    

                    // Implementation
                    protected:
                    HICON m_hIcon;
                    //CBrush m_brush;
                    //COLORREF m_crBackGnd;
                    CBrush m_objMyBrush;
                    COLORREF color;

                    // Generated message map functions
                    virtual BOOL OnInitDialog();
                    virtual BOOL OnDistroyWindow();
                    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);
                    	DECLARE\_MESSAGE\_MAP()
                    

                    public:

                    CComboBox m\_pComboBox;
                    //afx\_msg HBRUSH OnCtlColor(CDC\* pDC, CWnd\* pWnd, UINT nCtlColor);
                    afx\_msg void OnBnClickedChangebkcolor();
                    

                    };

                    CMeiTengDlg.cpp file

                    // MeiTengDlg.cpp : implementation file
                    //

                    #include "stdafx.h"
                    #include "MeiTeng.h"
                    #include "MeiTengDlg.h"

                    #ifdef _DEBUG
                    #define new DEBUG_NEW
                    #endif

                    // CAboutDlg dialog used for App About

                    class CAboutDlg : public CDialog
                    {
                    public:
                    CAboutDlg();

                    // Dialog Data
                    enum { IDD = IDD_ABOUTBOX };

                    protected:
                    virtual void DoDataExchange(CDataExchange\* pDX);    // DDX/DDV support
                    

                    // Implementation
                    protected:
                    DECLARE_MESSAGE_MAP()
                    };

                    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
                    {
                    }

                    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
                    {
                    CDialog::DoDataExchange(pDX);
                    }

                    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
                    END_MESSAGE_MAP()

                    // CMeiTengDlg dialog

                    CMeiTengDlg::CMeiTengDlg(CWnd* pParent /*=NULL*/)
                    : CDialog(CMeiTengDlg::IDD, pParent)
                    {
                    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
                    }

                    void CMeiTengDlg::DoDataExchange(CDataExchange* pDX)
                    {
                    CDialog::DoDataExchange(pDX);
                    DDX_Control(pDX, IDC_BITMAP, m_pComboBox);
                    // DDX_Control(pDX, IDC_Enjoystatic, m_pEnjoyStatic);
                    }

                    BEGIN_MESSAGE_MAP(CMeiTengDlg, CDialog)
                    ON_WM_SYSCOMMAND()
                    ON_WM_PAINT()
                    ON_WM_QUERYDRAGICON()
                    //}}AFX_MSG_MAP

                    ON\_BN\_CLICKED(IDC\_ChangeBKColor, &CMeiTengDlg::OnBnClickedChangebkcolor)
                    

                    END_MESSAGE_MAP()

                    // CMeiTengDlg message handlers

                    BOOL CMeiTengDlg::OnInitDialog()

                    M Offline
                    M Offline
                    Malli_S
                    wrote on last edited by
                    #14

                    ON_WM_CTLCOLOR() is missing in your message map. I hope you've added OnCtlColor handler manually.

                    [Delegates]      [Virtual Desktop]      [Tray Me !]
                    -Malli...! :rose:****

                    Z 1 Reply Last reply
                    0
                    • M Malli_S

                      ON_WM_CTLCOLOR() is missing in your message map. I hope you've added OnCtlColor handler manually.

                      [Delegates]      [Virtual Desktop]      [Tray Me !]
                      -Malli...! :rose:****

                      Z Offline
                      Z Offline
                      zhiyuan16
                      wrote on last edited by
                      #15

                      thanks malli now is ok. i have one more question here: in my dialog, i have several buttons, when i changed the background color, but dun chanaged the buttons' color, how can i can change the buttons' color after i choose different color. thanks

                      M 1 Reply Last reply
                      0
                      • Z zhiyuan16

                        thanks malli now is ok. i have one more question here: in my dialog, i have several buttons, when i changed the background color, but dun chanaged the buttons' color, how can i can change the buttons' color after i choose different color. thanks

                        M Offline
                        M Offline
                        Malli_S
                        wrote on last edited by
                        #16

                        I think this[^] would help you.

                        [Delegates]      [Virtual Desktop]      [Tray Me !]
                        -Malli...! :rose:****

                        Z 1 Reply Last reply
                        0
                        • M Malli_S

                          I think this[^] would help you.

                          [Delegates]      [Virtual Desktop]      [Tray Me !]
                          -Malli...! :rose:****

                          Z Offline
                          Z Offline
                          zhiyuan16
                          wrote on last edited by
                          #17

                          thanks, malli i have 3 radio buttons and one common button now, for 3 radio buttons, one is change background color, one is play music, last one is browse photoes, so when i check anyone radio button, and my that common button will show correspoding content what the radio button contains. thanks a lot.i searched online, but my pc and connection are slow, counldn't find. thanks.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups