MFC Quick Start Guide (Part 5): Brush

Text Box Background Color

  1. Inside MFC101Dlg.h, add a member variable:

    CBrush    m_TextBoxBrush;
    


    (Fig. 5.1: member variable)
  2. Inside OnInitDialog(), add the following line of code:

    m_TextBoxBrush.CreateSolidBrush(RGB(107, 133, 98));
    


    (Fig. 5.2: Create the brush)
  3.  Inside MESSAGE_MAP, add another event handler:

    ON_WM_CTLCOLOR()
    


    (Fig. 5.3: Message Map)
  4. Go back to MFC101Dlg.h, add the declaration:

    afx_msg HBRUSH OnCtlColor(CDC* pDC,  CWnd* pWnd, UINT nCtlCOlor);
    


    (Fig. 5.4: Declaration)
  5. Go back to MFC101Dlg.cpp, add the member function:

    HBRUSH  CMFC101Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
         HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
         ////  TODO:     Change any attributes of the DC here
    
         ////  TODO:     Return a  different brush if the default is not desired
         //return hbr;
    
    
         if (pWnd->GetDlgCtrlID() == IDC_TEXT_AREA)
         {
              hbr = m_TextBoxBrush;
         }
    }
    



    (Fig. 5.5: Member function)
  6. Press Ctrl+F5 to run and you should see something similar to this:


    (Fig. 5.6: Sample Output)

Text and Text Background Colors

  1. Inside OnCtlColor(),  add the following code:

    pDC->SetTextColor(RGB(107, 78, 255));
    pDC->SetBkColor(RGB(255, 255, 255));
    



    (Fig. 5.7: Code)
  2. Press Ctrl+F5 to run and you should see something similar to this:


    (Fig. 5.8: Sample Output 2)
  3. Now, we change one of the lines above so we can control the text background color with the slider and spin controls we made earlier:

    pDC->SetBkColor(RGB(RGB(m_nRedVal, m_nGreenVal, m_nBlueVal));
    


    (Fig. 5.9: Change)
     
  4.  Go back to OnHScroll(), add the following code accordingly so we can keep track of the value changes:

    m_nRedVal = nIndex;
    ...
    m_nGreenVal = nIndex;
    ...
    m_nBlueVal = nIndex;
    


    (Fig. 5.10: OnHScroll())
  5. Comment out the initialization of the 3 variables inside OnInitDialog()


    (Fig. 5.11: Comment)
  6. Instead, we'll be doing the initialization using constructor:


    (Fig. 5.12: Constructor)
  7. In OnEnChangeRedEdit(), OnEnChangeGreenEdit() and OnEnChangeBlueEdit(), add the following line of code:

    Invalidate();
    


    (Fig. 5.13: Invalidate())
  8. Add the same line of code inside OnHScroll():


    (Fig. 5.14: Invalidate())
  9. Press Ctrl+Shift+B to buid the solution
  10. Press Ctrl+F5 to run (without debugging)
  11. Trying dragging the sliders and spins and see what happen





For your reference, here is the code of MFC101Dlg.h and MFC101Dlg.cpp

MFC101Dlg.h

// MFC101Dlg.h : header file
//

#pragma once
#include "afxcmn.h"


// CMFC101Dlg dialog
class CMFC101Dlg : public CDialogEx
{
// Construction
public:
    CMFC101Dlg(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
    enum { IDD = IDD_MFC101_DIALOG };

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


// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
private:
    int     m_nNumberCount;
    CString m_sNumberCount;
    int     m_nRedVal;
    int     m_nGreenVal;
    int     m_nBlueVal;
public:
    afx_msg void OnBnClickedButton1();
    afx_msg void OnBnClickedButton2();
    afx_msg void OnBnClickedButton3();
    afx_msg void OnEnChangeRedEdit();
    afx_msg void OnEnChangeGreenEdit();
    afx_msg void OnEnChangeBlueEdit();
    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
private:
    CString m_RedSliderEcho;
    CString m_GreenSliderEcho;
    CString m_BlueSliderEcho;
    CSliderCtrl m_RedSliderCtrl;
    CSliderCtrl m_GreenSliderCtrl;
    CSliderCtrl m_BlueSliderCtrl;
    CBrush      m_TextBoxBrush;

public:
    
};




MFC101Dlg.cpp

// MFC101Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC101.h"
#include "MFC101Dlg.h"
#include "afxdialogex.h"
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFC101Dlg dialog



CMFC101Dlg::CMFC101Dlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CMFC101Dlg::IDD, pParent)
    , m_sNumberCount(_T(""))
    , m_RedSliderEcho(_T(""))
    , m_GreenSliderEcho(_T(""))
    , m_BlueSliderEcho(_T(""))
    , m_nRedVal(0)
    , m_nGreenVal(0)
    , m_nBlueVal(0)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFC101Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_TEXT_AREA, m_sNumberCount);
    DDX_Text(pDX, IDC_RED_EDIT, m_RedSliderEcho);
    DDX_Text(pDX, IDC_GREEN_EDIT, m_GreenSliderEcho);
    DDX_Text(pDX, IDC_BLUE_EDIT, m_BlueSliderEcho);
    DDX_Control(pDX, IDC_RED_SLIDER, m_RedSliderCtrl);
    DDX_Control(pDX, IDC_GREEN_SLIDER, m_GreenSliderCtrl);
    DDX_Control(pDX, IDC_BLUE_SLIDER, m_BlueSliderCtrl);
}

BEGIN_MESSAGE_MAP(CMFC101Dlg, CDialogEx)
    ON_WM_HSCROLL()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, &CMFC101Dlg::OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, &CMFC101Dlg::OnBnClickedButton2)
    ON_BN_CLICKED(IDC_BUTTON3, &CMFC101Dlg::OnBnClickedButton3)
    ON_EN_CHANGE(IDC_RED_EDIT, &CMFC101Dlg::OnEnChangeRedEdit)
    ON_EN_CHANGE(IDC_GREEN_EDIT, &CMFC101Dlg::OnEnChangeGreenEdit)
    ON_EN_CHANGE(IDC_BLUE_EDIT, &CMFC101Dlg::OnEnChangeBlueEdit)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CMFC101Dlg message handlers

BOOL CMFC101Dlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 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
    m_nNumberCount = 0;
    //m_nRedVal = 0;
    //m_nGreenVal = 0;
    //m_nBlueVal = 0;

    CSpinButtonCtrl* pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_RED_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_RED_EDIT));
    pSpinColorCtrl->SetPos(m_nRedVal);

    UDACCEL AccellValue;
    AccellValue.nInc = 1;   // Set how many the value would increase/decrease when a button is clicked
    pSpinColorCtrl->SetAccel(1, &AccellValue);

    pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_GREEN_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_GREEN_EDIT));
    pSpinColorCtrl->SetPos(m_nGreenVal);

    AccellValue.nInc = 1;
    pSpinColorCtrl->SetAccel(1, &AccellValue);

    pSpinColorCtrl = (CSpinButtonCtrl*)GetDlgItem(IDC_BLUE_SPIN);
    pSpinColorCtrl->SetRange(0, 255);
    pSpinColorCtrl->SetBuddy((CEdit *)GetDlgItem(IDC_BLUE_EDIT));
    pSpinColorCtrl->SetPos(m_nBlueVal);

    AccellValue.nInc = 1;
    pSpinColorCtrl->SetAccel(1, &AccellValue);

    m_RedSliderCtrl.SetRange(0, 255, TRUE);
    m_RedSliderCtrl.SetPos(0);

    m_RedSliderEcho.Format(_T("%d"), 0);

    m_GreenSliderCtrl.SetRange(0, 255, TRUE);
    m_GreenSliderCtrl.SetPos(0);

    m_GreenSliderEcho.Format(_T("%d"), 0);

    m_BlueSliderCtrl.SetRange(0, 255, TRUE);
    m_BlueSliderCtrl.SetPos(0);

    m_BlueSliderEcho.Format(_T("%d"), 0);

    m_TextBoxBrush.CreateSolidBrush(RGB(m_nRedVal, m_nGreenVal, m_nBlueVal));


    return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CMFC101Dlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFC101Dlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void CMFC101Dlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here
    m_nNumberCount++;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
}


void CMFC101Dlg::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
    m_nNumberCount = m_nNumberCount + 2;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
}


void CMFC101Dlg::OnBnClickedButton3()
{
    // TODO: Add your control notification handler code here
    m_nNumberCount = m_nNumberCount + 3;
    m_sNumberCount.Format(_T("%d"), m_nNumberCount);
    UpdateData(FALSE);
}


void CMFC101Dlg::OnEnChangeRedEdit()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialogEx::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
    char acStr[5] = "";
    CEdit* pRedBox = (CEdit *)GetDlgItem(IDC_RED_EDIT);
    pRedBox->GetWindowText(LPTSTR(acStr), 256);
    if (!(atoi((const char *)acStr)))
        pRedBox->SetWindowText(_T(""));
    else
    {
        if ((atoi((const char *)acStr)) > 255)
        {
            pRedBox->SetWindowText(_T("255"));
        }
        else if ((atoi((const char *)acStr)) < 0)
        {
            pRedBox->SetWindowText(_T("0"));
        }
        m_nRedVal = atoi((const char *)acStr);
    }
    m_RedSliderCtrl.SetPos(m_nRedVal);
    Invalidate();
}

void CMFC101Dlg::OnEnChangeGreenEdit()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialogEx::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
    char acStr[5] = "";
    CEdit* pGreenBox = (CEdit *)GetDlgItem(IDC_GREEN_EDIT);
    pGreenBox->GetWindowText(LPTSTR(acStr), 256);
    if (!(atoi((const char *)acStr)))
        pGreenBox->SetWindowText(_T(""));
    else
    {
        if ((atoi((const char *)acStr)) > 255)
        {
            pGreenBox->SetWindowText(_T("255"));
        }
        else if ((atoi((const char *)acStr)) < 0)
        {
            pGreenBox->SetWindowText(_T("0"));
        }
        m_nGreenVal = atoi((const char *)acStr);
    }
    m_GreenSliderCtrl.SetPos(m_nGreenVal);
    Invalidate();
}


void CMFC101Dlg::OnEnChangeBlueEdit()
{
    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialogEx::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO:  Add your control notification handler code here
    char acStr[5] = "";
    CEdit* pBlueBox = (CEdit *)GetDlgItem(IDC_BLUE_EDIT);
    pBlueBox->GetWindowText(LPTSTR(acStr), 256);
    if (!(atoi((const char *)acStr)))
        pBlueBox->SetWindowText(_T(""));
    else
    {
        if ((atoi((const char *)acStr)) > 255)
        {
            pBlueBox->SetWindowText(_T("255"));
        }
        else if ((atoi((const char *)acStr)) < 0)
        {
            pBlueBox->SetWindowText(_T("0"));
        }
        m_nBlueVal = atoi((const char *)acStr);
    }
    m_BlueSliderCtrl.SetPos(m_nBlueVal);
    Invalidate();
}

void CMFC101Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    if ((pScrollBar == (CScrollBar *)&m_RedSliderCtrl) ||
        (pScrollBar == (CScrollBar *)&m_GreenSliderCtrl) ||
        (pScrollBar == (CScrollBar *)&m_BlueSliderCtrl))
    {
        int nIndex;
        nIndex = m_RedSliderCtrl.GetPos();
        m_RedSliderEcho.Format(_T("%d"), nIndex);

        m_nRedVal = nIndex;

        nIndex = m_GreenSliderCtrl.GetPos();
        m_GreenSliderEcho.Format(_T("%d"), nIndex);

        m_nGreenVal = nIndex;

        nIndex = m_BlueSliderCtrl.GetPos();
        m_BlueSliderEcho.Format(_T("%d"), nIndex);

        m_nBlueVal = nIndex;

        UpdateData(FALSE);
    }
    else
    {
        CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
    }
    Invalidate();
}

HBRUSH CMFC101Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    //// TODO:  Change any attributes of the DC here

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


    if (pWnd->GetDlgCtrlID() == IDC_TEXT_AREA)
    {
        pDC->SetTextColor(RGB(107, 78, 255));
        pDC->SetBkColor(RGB(m_nRedVal, m_nGreenVal, m_nBlueVal));
        hbr = m_TextBoxBrush;
    }
    return hbr;
}







MFC Quick Start Guide (Part 5): Brush MFC Quick Start Guide (Part 5): Brush Reviewed by Kevin Lai on 12:37:00 AM Rating: 5

No comments:

Powered by Blogger.