王朝网络
分享
 
 
 

The Color Changer

王朝厨房·作者佚名  2007-01-04
宽屏版  字体: |||超大  

Introduction

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

Prerequisites:

Dialog Boxes

Colors

Static Text Control

Slider Control

Timer

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.

Practical Learning: Starting the Exercise

Start Microsoft Visual C++

Create a new project named ColorChanger1

Create the project as a Dialog Based and set the Dialog Title as Color Changer

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

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

In the header file of the dialog, declare a private COLORREF variable as follows:

private:

COLORREF brColor;

};

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

}

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);

}

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);

}

Test the application

After using it, close it and return to your programming environment.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有