The Color Changer

Introduction

In this exercise, we will use track bars to change the color of a static control.

Prerequisites:

There are various types of objects you can use to preview a color. The static control is convenient because it provides a ready made rectangle although you still have to configure it because it is not natively equip to handle the change of colors. To do this, you can consider it simply as a platform with a client area. When painting this control, you mostly use the properties of the CWnd class. For this reason, you could as well use a list box, a tree view, or a list view, etc.

To allow the user to set the variances of a color, you can use sliders, scroll bars, or spin buttons, etc. In this exercise, we will use the sliders simply because we decide to use them. They don't necessarily provide more or less functionality than scroll bars or spin buttons.

Giving live and necessary feedback to the user is one of the greatest assets of a friendly application. On this application, we will use static text controls to show the red, green, and blue values of a color.

Creating the Application

The simplest static control is considered a label. It consists of a simple object that displays text. The user cannot directly change this text unless you programmatically allow it. This form of the static control is the simplest control you can use in an MFC application.

To add a simple label to a dialog box, you can click the Static Text button on the Controls toolbar and click on the dialog box. The only thing you need to do is to change the Caption property on the Properties window.

If you cannot add a label to the dialog box when designing it, you can programmatically create one. This is done using the CStatic class and calling the Create() method.

By default, you cannot change the properties of a static control unless you state this explicitly. This can be done in various ways. For example you can declare a CString variable, set its value using the Format method, then call the SetDlgItemText() function and change the text of the static control using its identifier.

 

<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>

 

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++
  2. Create a new project named ColorChanger1
  3. Create the project as a Dialog Based and set the Dialog Title as Color Changer
  4. Design the dialog box as follows:
     
    ControlIDCaptionAdditional Properties
    Picture ControlIDC_PREVIEWAREA   
    Slider ControlIDC_RED_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Slider ControlIDC_GREEN_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Slider ControlIDC_BLUE_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Static TextIDC_RED_VALUE128Align Text: Center
    Static TextIDC_GREEN_VALUE128Align Text: Center
    Static TextIDC_BLUE_VALUE128Align Text: Center
  5. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls (the Static Text controls are created/declared as CString variables):
     
    IdentifierValue VariableControl Variable
    IDC_PREVIEWAREA m_PreviewArea
    IDC_RED_TRACK m_RedTrack
    IDC_GREEN_TRACK m_GreenTrack
    IDC_BLUE_TRACK m_BlueTrack
    IDC_RED_VALUEm_RedValue 
    IDC_GREEN_VALUEm_GreenValue 
    IDC_BLUE_VALUEm_BlueValue 
  6. In the header file of the dialog, declare a private COLORREF variable as follows:
     
    private:
    	COLORREF brColor;
    };
  7. Initialize the colors and variable in the OnInitDialog() event of the dialog box as follows:
     
    BOOL CColorChanger1Dlg::OnInitDialog()
    {
    	CDialog::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_RedTrack.SetRange(0, 255);
    	m_RedTrack.SetTicFreq(15);
    	m_RedTrack.SetPos(128);
    
    	m_GreenTrack.SetRange(0, 255);
    	m_GreenTrack.SetTicFreq(15);
    	m_GreenTrack.SetPos(128);
    
    	m_BlueTrack.SetRange(0, 255);
    	m_BlueTrack.SetTicFreq(15);
    	m_BlueTrack.SetPos(128);
    
    	brColor = RGB(128, 128, 128);
    
    	SetTimer(0x124, 20, NULL);
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
  8. When the user changes the value of one of the sliders, we will retrieve its value, combine it with the values of the other two sliders, create a color from these values and apply it to the picture control. To implement this, initiate the OnTimer() event of the dialog and implement it as follows:
     
    void CColorChanger1Dlg::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    	CClientDC dc(this);
        
    	// This is the rectangle that includes the big Static rectangle
    	CRect Recto;
    
    	// Store the dimensions of the Static control in the Recto object
    	m_PreviewArea.GetWindowRect(&Recto);
    	ScreenToClient(&Recto);
    
    	// Create a brush that will be used to paint the Static
    	CBrush BrushColor(brColor);
    	// Select the brush
    	CBrush *pBrush = dc.SelectObject(&BrushColor);
    
    	// Draw the background of the Static object
    	dc.Rectangle(&Recto);
    
    	// Give the brush back to the device context
    	dc.SelectObject(pBrush);
    	CDialog::OnTimer(nIDEvent);
    }
  9. Initiate the WM_VSCROLL message for the dialog and implement its OnVScroll() event as follows:
     
    void CColorChanger1Dlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
    	// TODO: Add your message handler code here and/or call default
    	UpdateData();
    	// The actual value is the position of the slider CSliderCtrl::GetPos()
    	// We subtract it from 255 because of the orientation of the slider
    	// and the navigation of the thumb box
    	int nRed   = 255 - m_RedTrack.GetPos();
    	int nGreen = 255 - m_GreenTrack.GetPos();
    	int nBlue  = 255 - m_BlueTrack.GetPos();
    
    	CString PosRed, PosGreen, PosBlue;
    
    	m_RedValue.Format("%d", nRed);
    	m_GreenValue.Format("%d", nGreen);
    	m_BlueValue.Format("%d", nBlue);
    
    	CRect Recto;
    	brColor = RGB(nRed, nGreen, nBlue);
    
    	m_PreviewArea.GetWindowRect(&Recto);
    	ScreenToClient(&Recto);
    
    	UpdateData(FALSE);
    	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    }
  10. Test the application
  11. After using it, close it and return to your programming environment.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值