See the helpful hints at the bottom of this page.


CS 307

Date THIRD TEST
Sample


Dr. Coleman
Design and implement a program in Visual Studio that meets ALL of the following requirements. Use the blank at the beginning of each requirement to check off each completed item. The image below shows what your program will look like when running:


This program demonstrates the standard RGB colors for a computer display. When one of the three check boxes is clicked it changes the background color of a rectangle to the right of the check box to the appropriate color (RED, GREEN, or BLUE). If the check box is not selected the rectangle appears dark gray. Clicking the All On button sets all check boxes selected and all the rectangles colored. Clicking the All Off button sets all check boxes not selected and all the rectangles dark gray.
  1. _____ Create a new project in Visual studio and call it Test_3_YourName. Make the project an MFC Application that is dialog based. (In the Advanced Features uncheck "Active X controls" and "Common Control Manifest". Also remember to right-click the project name, select Properties and set the Character Set property to "Not Set".
  2. _____ Open the main dialog in the resource editor and add the following widgets in order to create a dialog window that looks like the one in the above image:
    1. _____ Move the OK button and change the text to "Exit".
    2. _____ Add three check boxes with the text set respectively to "Red", "Green", and "Blue".
    3. _____ Create BN_CLICKED event handlers for each of the check boxes in the projectNameDlg class.
    4. _____ Add a variable to each of the check boxes with which to reference each.
    5. _____ Add two buttons at the bottom of the dialog. Set the text on the first to "All On". Set the text on the second to "All Off".
    6. _____ Create BN_CLICKED event handlers for each of the buttons in the projectNameDlg class.
  3. _____ In the OnInitDialog() function of the projectNameDlg class do the following:
    1. _____ Use the SetWindowText function to set the title of the dialog to "Test 3 - Your Name".
  4. _____ In each of the event handler functions for the three check boxes do the following:
    1. _____ Make an explicit call to the OnPaint() function to redraw the dialog (See details below on the OnPaint() function.)
  5. _____ In the event handler functions for the two buttons do the following:
    1. _____ For the "All On" button use the CBVar.SetCheck(1) to set each of the check boxes to selected.
    2. _____ For the "All Off" button use the CBVar.SetCheck(0) to set each of the check boxes to not selected.
    3. _____ Make an explicit call to the OnPaint() function to redraw the dialog (See details below on the OnPaint() function.)
  6. _____ In the onPaint function do the following:
    1. _____ Get a pointer to a CDC by calling the function this->GetDC().
    2. _____ For each of the three check boxes. Use the GetCheck() function to see if the checkbox is selected. If so use the cdc->FillSolidRect(x, y, width, height, COLORREF) to fill a color rectangle of the currently set color in the position shown in the above image. If the checkbox is not selected color the rectangle a dark dray. (Hint: In the sample image shown above the size parameters were (150, 30, 150, 30) for the red rectangle, (150, 80, 150, 30) for the green rectangle, and (150, 130, 150, 30) for the blue rectangle. Colors can be set with Red = RGB(255, 0, 0), Green = RGB(0, 255, 0), Blue = RGB(0, 0, 255), and Dark Gray = RGB(128, 128, 128).)
  7. _____ Compile and run your program to be sure it works as it should.
  8. _____ Copy your source files (all .cpp and .h) and executable file into a folder with your name on it then copy this folder onto the K drive into the designated folder (Labs/cs307/rcoleman).

Please fold your paper and write your name on the outside.

Helpful Hints:

  • Remember to do all drawing in an MFC Dialog application in the else part of the if statement in OnPaint.

  • To get the device context to paint with in an MFC Dialog application use the line:

    CDC *cdc = this->GetDC();

  • To draw a solid rectangle or oval without having to define a pen or brush use code like in the following example which draws a red rectangle:

    cdc->FillSolidRect(10, 10, 100, 100, RGB(255, 0, 0));

  • Do your drawing after the call to CDialog::OnPaint();