Activity 2: Colour Changer
· Open a new project in a new workspace using the MFC AppWizard (exe) and call it
ColourChanger.
· In AppWizard Step 1, select dialog based and accept the defaults in the other steps.
· Click on the TODO .. and delete. Then add three horizontal scroll bars from the toolbox.
Select all and use the layout menu to make them the same size, align them and arrange
them evenly. Next, add three edit boxes beside them and a label across the top.
Arrange them neatly as shown in the diagram. Leave some space between the top label
and the scroll bars as shown. The Layout menu will help with this.
· Right click each label to bring up the properties box and for each (under the styles tab),
align text:centre, centre vertically and give them a border. Give the top label the
caption Colour Changer and the other three Red, Green and Blue respectively.
Right click the background of the dialog box to bring up its Properties and use this to
select a font. Arial Black 12 works well. This will apply to each label on the dialog box.
· Use the View menu to open the Class Wizard. Make sure the Message Maps tab is
selected and that CColourChangerDlg is the class name selected. Then scroll down the
Messages until you find WM_HSCROLL. Double click to add OnHScroll to the member
functions. Note that this does not relate to a particular Scroll Bar.
· Now change to the Member Variables tab in Class Wizard. Click on IDC_EDIT1 in the
Control Ids box, then Add Variable. Name it m_text1, accept the default (value) category
and choose CString for your variable type. Repeat this step for IDC_EDIT2 (using
m_text2) and IDC_EDIT3 (with m_text3).
· Still in the Member Variables tab of the Class Wizard, give a name to each scroll bar,
using Add Variable. Name them m_slide1, m_slide2 and m_slide3 respectively, choose
Control for the Category and CscrollBar for the Variable type. This will be used to find
which scroll bar is moved each time.
· Add a button and change its Caption to Click to see the colour by right clicking and
opening Properties. Then open the Class Wizard again, select the Message Maps tab,
make sure CColourChangerDlg is the class selected, select IDC_BUTTON1 and double
click the message BN_CLICKED to add the OnButton1 member function.
· Now add some code to the CColourChangerDlg.cpp source code file. Find the class
initialisation and add the code in bold.
BOOL CColourChangerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_slide1.SetScrollRange(1,255, true);
m_text1 = "1";
m_slide2.SetScrollRange(1,255, true);
m_text2 = "1";
m_slide3.SetScrollRange(1,255, true);
m_text3 = "1";
UpdateData(false);
n_red = 1;
n_green = 1;
n_blue = 1;
// Add "About..." menu item to system
menu.
//Set scroll bar range and
//intitialise edit boxes to 1
//This updates data from the
//variable to the edit box.
//(True) updates data from
//the textbox to the variable.
//Declare and initialise three
//integer variables.
No comments:
Post a Comment