ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾C/C++±à³Ì£¾Teach Yourself Visual C++ 6 in 21 Days


Teach Yourself Visual C++ 6 in 21 Days

Previous chapterNext chapterContents


- B -
Answers (day 1-10)

Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7
Day 8 Day 9 Day 10 Day 11 Day 12 Day 13 Day 14
Day 15 Day 16 Day 17 Day 18 Day 19 Day 20 Day 21



This appendix provides the answers to the quiz questions and exercises at the end of each chapter.

Day 1

Quiz

1. How do you change the caption on a button?

In the window layout editor, select the button to be changed. Right-click the mouse and select Properties from the pop-up menu. Change the value in the Caption field.

2. What can you do with the Visual C++ AppWizard?

You can use it to build a shell for your application, based on the type of application and the functionality needs of the application. The shell will have support for the desired functionality already built in.

3. How do you attach functionality to the click of a button?

By using the Class Wizard, you can create a function and attach it to an object for handling a specific Windows message. The Class Wizard creates the function and can take you right to the spot in the function's code where you need to begin adding your own code.

Exercise

Add a second button to the About window in your application. Have the button display a different message from the one on the first window.

1. In the workspace pane, select the Resource View tab.

2. Expand the dialog tree branch and double-click the IDD_ABOUTBOX dialog, bringing it into the Developer Studio editor.

3. Click the button control on the toolbar.

4. Click and drag the mouse on the window where you want the button to be placed.

5. Open the properties dialog for the new button, changing the ID and caption to describe the message to be displayed by the button. Close the properties dialog.

6. Open the Class Wizard and add a new function for the clicked message for your new button.

7. Click the Edit Code button in the Class Wizard to take you to the spot in your code where your new function is.

8. Add the MessageBox function to display a message to the user.

9. Compile and run your application to test your new button.

Day 2

Quiz

1. Why do you need to specify the tab order of the controls on your application windows?

By specifying the tab order of the controls on your application windows, you can control the order in which the user navigates the application window. If the user is using the keyboard to navigate around the application window, then the two primary means of navigating between controls are the tab key and mnemonics that jump directly to specific controls. The tab order helps provide the user with a consistent and predictable experience when using your application.

2. How can you include a mnemonic in a static text field that will take the user to the edit box or combo box beside the text control?

If you place a mnemonic in a static text control and then make sure that the static text control is just before the edit control associated with the static text, the user can select the mnemonic in the static text control to jump directly to the edit box control.

3. Why do you need to give unique object IDs to the static text fields in front of the edit box and combo boxes?

The unique object IDs on the two static text controls were necessary because you need to manipulate those two controls with the check boxes that enable or disable and show or hide sets of controls.

4. Why do you need to call the UpdateData function before checking the value of one of the controls?

If the user has changed the value of the control on the screen, the UpdateData function must be called, passing it TRUE as the function argument, to copy the values from the controls on the window to the variables that are associated with those controls. If UpdateData is not called, then the values of the variables may not correctly reflect what the user has changed on the screen.

Exercises

1. Add code to the Default Message button to reset the edit box to say Enter a message here.

Using the Class Wizard, add a function to the Default Message button's clicked event. In this function, add the code in Listing B.1.

LISTING B.1. DAY2DLG.CPP--THE CODE TO PLACE A DEFAULT MESSAGE IN THE EDIT BOX.

 1: void CDay2Dlg::OnDfltmsg()
 2: {
 3:     // TODO: Add your control notification handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Set the message to a default message
10:     m_strMessage = "Enter a message here";
11:
12:     // Update the screen
13:     UpdateData(FALSE);
14:
15:     ///////////////////////
16:     // MY CODE ENDS HERE
17:     ///////////////////////
18: }
2. Add code to enable or disable and show or hide the controls used to select and run another application.

Add functions to the Enable and Show Program Action check boxes. In these functions, add the code in Listing B.2.

LISTING B.2. DAY2DLG.CPP--THE CODE TO ENABLE OR DISABLE AND SHOW OR HIDE THE RUN PROGRAM CONTROLS.

 1: void CDay2Dlg::OnCkenblpgm()
 2: {
 3:     // TODO: Add your control notification handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Get the current values from the screen
10:     UpdateData(TRUE);
11:
12:     // Is the Enable Program Action check box checked?
13:     if (m_bEnablePgm == TRUE)
14:     {
15:         // Yes, so enable all controls that have anything
16:         // to do with running a program
17:         GetDlgItem(IDC_PROGTORUN)->EnableWindow(TRUE);
18:         GetDlgItem(IDC_RUNPGM)->EnableWindow(TRUE);
19:         GetDlgItem(IDC_STATICPGM)->EnableWindow(TRUE);
20:     }
21:     else
22:     {
23:         // No, so disable all controls that have anything
24:         // to do with running a program
25:         GetDlgItem(IDC_PROGTORUN)->EnableWindow(FALSE);
26:         GetDlgItem(IDC_RUNPGM)->EnableWindow(FALSE);
27:         GetDlgItem(IDC_STATICPGM)->EnableWindow(FALSE);
28:     }
29:
30:     ///////////////////////
31:     // MY CODE ENDS HERE
32:     ///////////////////////
33: }
34:
35: void CDay2Dlg::OnCkshwpgm()
36: {
37:     // TODO: Add your control notification handler code here
38:
39:     ///////////////////////
40:     // MY CODE STARTS HERE
41:     ///////////////////////
42:
43:     // Get the current values from the screen
44:     UpdateData(TRUE);
45:
46:     // Is the Show Program Action check box checked?
47:     if (m_bShowPgm == TRUE)
48:     {
49:         // Yes, so show all controls that have anything
50:         // to do with running a program
51:         GetDlgItem(IDC_PROGTORUN)->ShowWindow(TRUE);
52:         GetDlgItem(IDC_RUNPGM)->ShowWindow(TRUE);
53:         GetDlgItem(IDC_STATICPGM)->ShowWindow(TRUE);
54:     }
55:     else
56:     {
57:         // No, so hide all controls that have anything
58:         // to do with running a program
59:         GetDlgItem(IDC_PROGTORUN)->ShowWindow(FALSE);
60:         GetDlgItem(IDC_RUNPGM)->ShowWindow(FALSE);
61:         GetDlgItem(IDC_STATICPGM)->ShowWindow(FALSE);
62:     }
63:
64:     ///////////////////////
65:     // MY CODE ENDS HERE
66:     ///////////////////////
67: }
3. Extend the code in the OnRunpgm function to allow the user to enter his own program name to be run.

Modify the OnRunpgm function as in Listing B.3.

LISTING B.3. DAY2DLG.CPP--THE CODE TO RUN ANY PROGRAM NAME TYPED INTO THE RUN PROGRAM COMBO BOX.

 1: void CDay2Dlg::OnRunpgm()
 2: {
 3:     // TODO: Add your control notification handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Get the current values from the screen
10:     UpdateData(TRUE);
11:
12:     // Declare a local variable for holding the program name
13:     CString strPgmName;
14:
15:     // Copy the program name to the local variable
16:     strPgmName = m_strProgToRun;
17:
18:     // Make the program name all uppercase
19:     strPgmName.MakeUpper();
20:
21:     // Did the user select to run the Paint program?
22:     if (strPgmName == "PAINT")
23:         // Yes, run the Paint program
24:         WinExec("pbrush.exe", SW_SHOW);
25:
26:     // Did the user select to run the Notepad program?
27:     if (strPgmName == "NOTEPAD")
28:         // Yes, run the Notepad program
29:         WinExec("notepad.exe", SW_SHOW);
30:
31:     // Did the user select to run the Solitaire program?
32:     if (strPgmName == "SOLITAIRE")
33:         // Yes, run the Solitaire program
34:         WinExec("sol.exe", SW_SHOW);
35:
36:         // Run any other program name typed into the combo box
37:         if ((strPgmName != "PAINT") && (strPgmName != "NOTEPAD") &&
38:             (strPgmName != "SOLITAIRE"))
39:               // Yes, run the program typed into the combo box
40:               WinExec(strPgmName, SW_SHOW);
41:
42:     ///////////////////////
43:     // MY CODE ENDS HERE
44:     ///////////////////////

45: }

Day 3

Quiz

1. What are the possible mouse messages that you can add functions for?

WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, WM_RBUTTONDOWN, WM _RBUTTONUP, WM_RBUTTONDBLCLK, WM_MOUSEMOVE, and WM_MOUSEWHEEL.

2. How can you tell if the left mouse button is down on the WM_MOUSEMOVE event message?

You can mask the flags passed to the OnMouseMove function with the MK_LBUTTON flag, as follows:

((nFlags & MK_LBUTTON) == MK_LBUTTON)
3. How can you prevent the cursor from changing back to the default cursor after you set it to a different one?

Return TRUE in the OnSetCursor event function, preventing the ancestor OnSetCursor function from being called.

Exercises

1. Modify your drawing program so that the left mouse button can draw in red and the right mouse button can draw in blue.

Add a function for the WM_RBUTTONDOWN event message and write the code for it as in Listing B.4.

LISTING B.4. MOUSEDLG.CPP--THE OnRButtonDown FUNCTION.

 1: void CMouseDlg::OnRButtonDown(UINT nFlags, CPoint point)
 2: {
 3:     // TODO: Add your message handler code here and/or call default
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:         // Set the current point as the starting point
10:     m_iPrevX = point.x;
11:     m_iPrevY = point.y;
12:
13:     ///////////////////////
14:     // MY CODE ENDS HERE
15:     ///////////////////////
16:
17:     CDialog::OnRButtonDown(nFlags, point);
18: }
Extend the OnMouseMove function as in Listing B.5.

LISTING B.5. MOUSEDLG.CPP--THE MODIFIED OnMouseMove FUNCTION.

 1: void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point)
 2: {
 3:     // TODO: Add your message handler code here and/or call default
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:         // Check to see if the left mouse button is down
10:     if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
11:     {
12:         // Get the Device Context
13:         CClientDC dc(this);
14:
15:         // Create a new pen
16:         CPen lpen(PS_SOLID, 16, RGB(255, 0, 0));
17:
18:         // Use the new pen
19:         dc.SelectObject(&lpen);
20:
21:         // Draw a line from the previous point to the current point
22:         dc.MoveTo(m_iPrevX, m_iPrevY);
23:         dc.LineTo(point.x, point.y);
24:
25:         // Save the current point as the previous point
26:         m_iPrevX = point.x;
27:         m_iPrevY = point.y;
28:     }
29:
30:     // Check to see if the right mouse button is down
31:     if ((nFlags & MK_RBUTTON) == MK_RBUTTON)
32:     {
33:         // Get the Device Context
34:         CClientDC rdc(this);
35:
36:         // Create a new pen
37:         CPen rpen(PS_SOLID, 16, RGB(0, 0, 255));
38:
39:         // Use the new pen
40:         rdc.SelectObject(&rpen);
41:
42:         // Draw a line from the previous point to the current point
43:         rdc.MoveTo(m_iPrevX, m_iPrevY);
44:         rdc.LineTo(point.x, point.y);
45:
46:         // Save the current point as the previous point
47:         m_iPrevX = point.x;
48:         m_iPrevY = point.y;
49:     }
50:
51:     ///////////////////////
52:     // MY CODE ENDS HERE
53:     ///////////////////////
54:
55:     CDialog::OnMouseMove(nFlags, point);
56: }
2. Extend the OnKeyDown function to add some of the following standard cursors:

  • IDC_CROSS

  • IDC_UPARROW

  • IDC_SIZEALL

  • IDC_SIZENWSE

  • IDC_SIZENESW

  • IDC_SIZEWE

  • IDC_SIZENS

  • IDC_NO

  • IDC_APPSTARTING

  • IDC_HELP
Your modified OnKeyDown function can look something like the following:

void CMouseDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    char lsChar;        // The current character being pressed
    HCURSOR lhCursor;    // The handle to the cursor to be displayed
    // Convert the key pressed to a character
    lsChar = char(nChar);
    // Is the character "A"
    if (lsChar == `A')
    {
        // Load the arrow cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "B"
    if (lsChar == `B')
    {
        // Load the I beam cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_IBEAM);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "C"
    if (lsChar == `C')
    {
        // Load the hourglass cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_WAIT);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "D"
    if (lsChar == `D')
    {
        // Load the cross hair cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "E"
    if (lsChar == `E')
    {
        // Load the up arrow cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_UPARROW);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "F"
    if (lsChar == `F')
    {
        // Load the size cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "G"
    if (lsChar == `G')
    {
        // Load the up/right-down/left size cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "H"
    if (lsChar == `H')
    {
        // Load the up/left-down/right size cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZENESW);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "I"
    if (lsChar == `I')
    {
        // Load the left-right size cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "J"
    if (lsChar == `J')
    {
        // Load the up-down size cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZENS);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    if (lsChar == `K')
    {
        // Load the no cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_NO);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    if (lsChar == `L')
    {
        // Load the app starting cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    if (lsChar == `M')
    {
        // Load the help cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_HELP);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
    }
    // Is the character "X"
    if (lsChar == `X')
    {
        // Load the arrow cursor
        lhCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        // Set the cursor flag
        m_bCursor = TRUE;
        // Set the screen cursor
        SetCursor(lhCursor);
        // Exit the application
        OnOK();
    }
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

Day 4

Quiz

1. What did you accomplish by adding the two timer IDs to the resource symbols?

You defined the two IDs so that they were available as constants throughout the application.

2. What is another way to add these two IDs to the application?

Add them as #define constants in the class header file (Day2Dlg.h), as follows:

/////////////////////////////////////////////////////////////////////// CTimersDlg dialog
#define ID_CLOCK_TIMER 1
#define ID_COUNT_TIMER 2
class CTimersDlg : public CDialog
{
.
.
.
3. How can you tell two timers apart in the OnTimer function?

You use the timer ID to determine which timer triggered the event.

4. How many timer events does your application receive if the timer is set for one second and your application has been busy for one minute, preventing it from receiving any timer event messages?

One.

Exercise

Update your application so that when the counter timer is started, the clock timer is reset to run at the same interval as the counter timer. When the counter timer is stopped, return the clock timer to a one-second interval.

To change the interval at which a timer is running, you need to first stop the timer and then restart it, as in Listing B.6.

LISTING B.6. THE REVISED OnStarttime AND OnStoptimer FUNCTIONS.

 1: void CTimersDlg::OnStarttime()
 2: {
 3:     // TODO: Add your control notification handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Update the variables
10:     UpdateData(TRUE);
11:
12:     // Initialize the count
13:     m_iCount = 0;
14:     // Format the count for displaying
15:     m_sCount.Format("%d", m_iCount);
16:
17:     // Update the dialog
18:     UpdateData(FALSE);
19:     // Start the timer
20:     SetTimer(ID_COUNT_TIMER, m_iInterval, NULL);
21:
22:     // Stop the clock timer
23:     KillTimer(ID_CLOCK_TIMER);
24:     // Restart the clock timer with the counter interval
25:     SetTimer(ID_CLOCK_TIMER, m_iInterval, NULL);
26:
27:     // Enable the Stop Timer button
28:     m_cStopTime.EnableWindow(TRUE);
29:     // Disable the Start Timer button
30:     m_cStartTime.EnableWindow(FALSE);
31:
32:     ///////////////////////
33:     // MY CODE ENDS HERE
34:     ///////////////////////
35: }
36:
37: void CTimersDlg::OnStoptimer()
38: {
39:     // TODO: Add your control notification handler code here
40:
41:     ///////////////////////
42:     // MY CODE STARTS HERE
43:     ///////////////////////
44:
45:     // Stop the timer
46:     KillTimer(ID_COUNT_TIMER);
47:
48:     // Stop the clock timer
49:     KillTimer(ID_CLOCK_TIMER);
50:     // Restart the clock timer with 1 second interval
51:     SetTimer(ID_CLOCK_TIMER, 1000, NULL);
52:
53:     // Disable the Stop Timer button
54:     m_cStopTime.EnableWindow(FALSE);
55:     // Enable the Start Timer button
56:     m_cStartTime.EnableWindow(TRUE);
57:
58:     ///////////////////////
59:     // MY CODE ENDS HERE
60:     ///////////////////////
61: }

Day 5

Quiz

1. What are the possible return codes that your application might receive from the MessageBox function call when you specify the MB_RETRYCANCEL button combination?

IDRETRY and IDCANCEL.

2. What are the common dialogs that are built into the Windows operating systems that are defined as MFC classes?

The common Windows dialogs that are defined as MFC classes are

  • File selection

  • Font selection

  • Color selection

  • Page setup for printing

  • Printing

  • Find and replace
3. What is the difference between a modal dialog and a modeless dialog?

A modal dialog stops all application processing until the user responds to the dialog. A modeless dialog allows the user to continue working with the rest of the application while the dialog is open for use.

4. How can you display a File Save dialog for the user instead of the File Open dialog that you did have in your application?
In the class instance variable declaration, pass FALSE instead of TRUE. This makes the variable declaration look like this:

CFileDialog m_ldFile(FALSE);
5. Why did you not need to create any functions and add any code to your custom dialog?

The only functionality that was needed on the custom dialog was calling UpdateData before closing the dialog. Because the OK and Cancel buttons were never deleted from the dialog, the OK button automatically performed this functionality.

Exercises

1. Modify your application so that it includes the directory with the filename in the application. (Hint: The GetFileName function returns the path and filename that was selected in the File Open dialog.)

Modify the OnFileopen function as follows:

void CDialogsDlg::OnFileopen()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    CFileDialog m_ldFile(TRUE);
    // Show the File open dialog and capture the result
    if (m_ldFile.DoModal() == IDOK)
    {
        // Get the filename selected
        m_sResults = m_ldFile.GetPathName();
        // Update the dialog
        UpdateData(FALSE);
    }
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
The GetPathName function returns the path and filename, so changing the function call from GetFileName to GetPathName alters the display to include the path with the filename.

2. Add a button on the custom dialog that calls the MessageBox function with a Yes or No selection. Pass the result back to the main application dialog.

Follow these steps:

1. Using the Class View, add a member variable to the CMsgDlg class. Specify the variable type as int, the name as m_iYesNo, and the access as Public.

2. Using the Resource View, bring the custom dialog into the editor area. Add a command button to the window, named IDC_YESNO with a caption &Yes or No.

3. Using the Class Wizard, add a function to the new button you just added and edit the function. Include the following code:

void CMsgDlg::OnYesno()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Ask the user
    m_iYesNo = MessageBox("Choose Yes or No", "Yes or No", ÂMB_YESNO);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
4. Add a button to the main dialog window named IDC_YESNO with the caption Y&es or No.

5. Using the Class Wizard, add a function to the new button, including the following code:

void CDialogsDlg::OnYesno()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // What did the user answer
    switch (m_dMsgDlg.m_iYesNo)
    {
    case IDYES:    // Did the user answer YES?
        m_sResults = "Yes!";
        break;
    case IDNO:     // Did the user answer NO?
        m_sResults = "No!";
        break;
}

    // Update the dialog
    UpdateData(FALSE);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}

Day 6

Quiz

1. What event message does a menu selection send to the window message queue?

COMMAND.

2. How do you attach a menu to a dialog window?

In the dialog designer, open the properties dialog for the window, and choose the menu from the drop-down list of menus.

3. Which existing class do you specify for handling event messages for the menu?

The dialog class for the window on which the menu appears.

4. What event message should a pop-up menu be triggered by?

The WM_CONTEXTMENU event.

Exercises

1. Add a button to the main window and have it call the same function as the Hello menu entry.

Follow these steps:

1. Add a button to the dialog screen. Supply a button ID of IDC_HELLO and a caption of &Hello.

2. Using the Class Wizard, add a function to the button. Name the function OnHello.

2. Add a pop-up menu to your application that uses the Help drop-down menu as the pop-up menu.

Follow these steps:

1. Using the Class Wizard, add a function for the WM_CONTEXTMENU event message in your dialog window.

2. Edit the function, adding the following code:

void CMenusDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
    // TODO: Add your message handler code here and/or call Âdefault
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Declare local variables
    CMenu *m_lMenu;    // A pointer to the menu
    CPoint m_pPoint;    // A copy of the mouse position
    // Copy the mouse position to a local variable
    m_pPoint = point;
    // Convert the position to a screen position
    ClientToScreen(&m_pPoint);
    // Get a pointer to the window menu
    m_lMenu - GetMenu();
    // Get a pointer to the first submenu
    m_lMenu = m_lMenu->GetSubMenu(1);
    // Show the Pop-up Menu
    m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON,
        m_pPoint.x, m_pPoint.y, this, NULL);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
    CDialog::OnRButtonDown(nFlags, point);
}

Day 7

Quiz

1. How can you specify that the text is to be underlined?

Pass 1 as the value for the bUnderline argument to the CreateFont function.

2. How can you print your text upside down?

Pass 1800 as the nEscapement argument to the CreateFont function.

3. How many times is the EnumFontFamProc callback function called by the operating system?

The function is called once for each font that is available in the system, unless the callback function returns 0 and stops the listing of fonts.

Exercises

1. Add a check box to switch between using the entered text to display the font and using the font name to display the font, as in Figure 7.4.

Add the check box to the dialog. Set its properties as follows:

ID: IDC_CBUSETEXT

Caption: &Use Entered Text

Using the Class Wizard, attach a variable to this control. Specify the variable type as a boolean with the name m_bUseText.

Using the Class Wizard, add a function for the BN_CLICKED event message for the check box. Edit the function, adding the following code:

void CDay7Dlg::OnCbusetext()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Update the variables with the dialog controls
    UpdateData(TRUE);
    // Using the font name for the font sample?
    if (!m_bUseText)
        // Using the font name
        m_strDisplayText = m_strFontName;
    else
        // Using the entered text
        m_strDisplayText = m_strSampText;
    // Update the dialog
    UpdateData(FALSE);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
Modify the OnInitDialog function to initialize the check box as follows:

BOOL CDay7Dlg::OnInitDialog()
{
    CDialog::OnInitDialog();
.
.
.
    // TODO: Add extra initialization here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Fill the font list box
    FillFontList();
    // Initialize the text to be entered
    m_strSampText = "Testing";
    // Copy the text to the font sample area
    m_strDisplayText = m_strSampText;
    // Initialize the check box
    m_bUseText = TRUE;
    // Update the dialog
    UpdateData(FALSE);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
    return TRUE;  // return TRUE  unless you set the focus
                  // to a control
}
Modify the OnSelchangeLfonts function as follows:

void CDay7Dlg::OnSelchangeLfonts()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Update the variables with the dialog controls
    UpdateData(TRUE);
    // Using the font name for the font sample?
    if (!m_bUseText)
    {
        // Copy the font name to the font sample
        m_strDisplayText = m_strFontName;
        // Update the dialog with the variables
        UpdateData(FALSE);
    }
    // Set the font for the sample
    SetMyFont();
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
Finally, modify the OnChangeEsamptext function as follows:

void CDay7Dlg::OnChangeEsamptext()
{
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the
    // CDialog::OnInitialUpdate()
    // function and call CRichEditCrtl().SetEventMask()
    // with the EN_CHANGE flag ORed into the mask.
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Update the variables with the dialog controls
    UpdateData(TRUE);
    // Using the text for the font sample?
    if (m_bUseText)
    {
        // Copy the current text to the font sample
        m_strDisplayText = m_strSampText;
        // Update the dialog with the variables
        UpdateData(FALSE);
    }
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
2. Add a check box to display the font sample in italics, as in Figure 7.5.

Add the check box to the dialog. Set its properties as follows:

ID: IDC_CBITALIC

Caption: &Italic

Using the Class Wizard, attach a variable to this control. Specify the variable type as a boolean with the name m_bItalic.

Using the Class Wizard, add a function for the BN_CLICKED event message for the check box. Edit the function, adding the following code:

void CDay7Dlg::OnCbitalic()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Update the variables with the dialog controls
    UpdateData(TRUE);
    // Set the font for the sample
    SetMyFont();
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}
Modify the SetMyFont function as in the following code:

void CDay7Dlg::SetMyFont()
{
    CRect rRect;        // The rectangle of the display area
    int iHeight;        // The height of the display area
    int iItalic = 0;    // Italicize the font?
    // Has a font been selected?
    if (m_strFontName != "")
    {
        // Get the dimensions of the font sample display area
        m_ctlDisplayText.GetWindowRect(&rRect);
        // Calculate the area height
        iHeight = rRect.top - rRect.bottom;
        // Make sure the height is positive
        if (iHeight < 0)
            iHeight = 0 - iHeight;
        // Should the font be italicized?
        If (m_bItalic)
            iItalic = 1;
        // Create the font to be used
        m_fSampFont.CreateFont((iHeight - 5), 0, 0, 0,
                FW_NORMAL, iItalic, 0, 0, DEFAULT_CHARSET,
                OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
                DEFAULT_QUALITY, DEFAULT_PITCH |
                FF_DONTCARE, m_strFontName);
        // Set the font for the sample display area
        m_ctlDisplayText.SetFont(&m_fSampFont);
    }
}

Day 8

Quiz

1. What are the three values that are combined to specify a color?

Red, green, and blue.

2. What do you use to draw on windows without needing to know what graphics card the user has?

The device context.

3. What size bitmap can you use to make a brush from it?

8 pixels by 8 pixels.

4. What event message is sent to a window to tell it to redraw itself?

The WM_PAINT message.

5. How can you cause a window to repaint itself?

Use the Invalidate function on it.

Exercises

1. Make the second dialog window resizable, and make it adjust the figures drawn on it whenever it's resized.

Open the second dialog in the dialog layout designer. Open the properties for the window. Select the Style tab. Change the border to Resizing. Open the Class Wizard and add an event-handler function for the WM_SIZE event message. Edit the function that you just created and call the Invalidate function, as in Listing B.7.

LISTING B.7. THE OnSize FUNCTION.

1: void CPaintDlg::OnSize(UINT nType, int cx, int cy)
2: {
3:     CDialog::OnSize(nType, cx, cy);
4:
5:     // TODO: Add your message handler code here
6:     // Redraw the window
7:     Invalidate();
8:}
2. Add a bitmap brush to the set of brushes used to create the rectangles and ellipses.

Open the Resources View tab on the workspace pane. Right-click on the top folder of the resource tree. Select Insert from the pop-up menu. Select Bitmap from the list of available resources to add. Paint a pattern on the bitmap that you just created. Right-click on the bitmap ID in the workspace pane. Open the properties dialog and change the object ID to IDB_BITMAPBRUSH. Open the source code for the DrawRegion function. Add the code in Listing B.8, lines 22 through 24 and lines 105 through 112. Increase the number of loops in the for statement on line 39.

LISTING B.8. THE DrawRegion FUNCTION.

1:  void CPaintDlg::DrawRegion(CPaintDC *pdc, int iColor, int iTool, int           ÂiShape)
2:  {
3:      // Declare and create the pens
.
.
.
19:     CBrush lVertBrush(HS_VERTICAL, m_crColors[iColor]);
20:     CBrush lNullBrush(RGB(192, 192, 192));
21:
22:     CBitmap lBitmap;
23:     lBitmap.LoadBitmap(IDB_BITMAPBRUSH);
24:     CBrush lBitmapBrush(&lBitmap);
25:
26:     // Calculate the size of the drawing regions
27:     CRect lRect;
28:     GetClientRect(lRect);
.
.
.
37:     int i;
38:     // Loop through all of the brushes and pens
39:     for (i = 0; i < 8; i++)
40:     {
41:         switch (i)
42:         {
.
.
.
103:            pdc->SelectObject(&lVertBrush);
104:            break;
105:        case 7:    // Null - Bitmap
106:            // Determine the location for this figure.
107:            lDrawRect.left = lDrawRect.left + liHorz;
108:            lDrawRect.right = lDrawRect.left + liWidth;
109:            // Select the appropriate pen and brush
110:            pdc->SelectObject(&lNullPen);
111:            pdc->SelectObject(&lBitmapBrush);
112:            break;
113:        }
114:        // Which tool are we using?
.
.
.
126:    pdc->SelectObject(lOldBrush);
127:    pdc->SelectObject(lOldPen);
128:}

Day 9

Quiz

1. How does an ActiveX container call methods in an ActiveX control?

By using the IDispatch interface, the container can call the Invoke method, passing the DISPID of the control's method that the container wants to run.

2. How does an ActiveX control trigger events in the container application?

The container application has its own IDispatch interface, through which the control can trigger events.

3. What AppWizard option must be selected for ActiveX controls to work properly in a Visual C++ application?

You select the ActiveX Controls check box in the second step of the AppWizard.

4. How does Visual C++ make it easy to work with ActiveX controls?

It generates C++ classes that encapsulate the control's functionality.

5. Why might it be difficult to work with older controls in Visual C++?

Older controls might not contain the information necessary for Visual C++ to generate the C++ classes that are used to encapsulate the control's functionality.

Exercise

Modify the application so that the user can double-click a column header and make it the first column in the grid.

Using the Class Wizard, add a function to the DblClick event message for the grid control.

Edit the function in exercise 1 to add the following code:

void CActiveXDlg::OnDblClickMsfgrid()
{
    // TODO: Add your control notification handler code here
    ///////////////////////
    // MY CODE STARTS HERE
    ///////////////////////
    // Did the user click on a data row and not the
    // header row?
    if (m_ctlFGrid.GetMouseRow() != 0)
    {
        // If so, then zero out the column variable
        // and exit
        m_iMouseCol = 0;
        return;
    }
    // Save the column clicked on
    m_iMouseCol = m_ctlFGrid.GetMouseCol();
    // If the selected column was the first column,
    // there's nothing to do
    if (m_iMouseCol == 0)
        return;
    // Turn the control redraw off
    m_ctlFGrid.SetRedraw(FALSE);
    // Change the selected column position
    m_ctlFGrid.SetColPosition(m_iMouseCol, 0);
    // Resort the grid
    DoSort();
    // Turn redraw back on
    m_ctlFGrid.SetRedraw(TRUE);
    ///////////////////////
    // MY CODE ENDS HERE
    ///////////////////////
}

Day 10

Quiz

1. What does SDI stand for?

Single Document Interface.

2. What functionality is in the view class?

The view class is responsible for displaying the document for the user.

3. What function is called to redraw the document if the window has been hidden behind another window?

The OnDraw function in the view class is called to redraw the document.

4. Where do you place code to clear out the current document before starting a new document?

The DeleteContents function in the document class is where you place code to clear the current document.

5. What is the purpose of the document class?

The document class is where the data is managed and manipulated. It maintains the abstract representation of the document being edited and processed.

Exercise

Add another pull-down menu to control the width of the pen used for drawing. Give it the following settings:

Menu Entry Width Setting
Very Thin 1
Thin 8
Medium 16
Thick 24
Very Thick 32

Follow these steps:

1. Select the CLine class in the Class View tab of the workspace pane. Right-click the mouse and select Add Member Variable from the pop-up menu.

2. Specify the variable type as UINT, the name as m_nWidth, and the access as private. Click OK to add the variable.

3. Right-click the CLine constructor in the Class View tree. Select Go to Declaration from the pop-up menu.

4. Add UINT nWidth as a fourth argument to the constructor declaration.

5. Right-click the CLine constructor in the Class View tree. Select Go to Definition from the pop-up menu.

6. Modify the constructor to add the fourth argument and to set the m_nWidth member to the new argument, as in Listing B.9.

LISTING B.9. THE MODIFIED CLine CONSTRUCTOR.

1: CLine::CLine(CPoint ptFrom, CPoint ptTo, COLORREF crColor, UINT nWidth)
2: {
3:     //Initialize the from and to points
4:     m_ptFrom = ptFrom;
5:     m_ptTo = ptTo;
6:     m_crColor = crColor;
7:     m_nWidth = nWidth;
8: }
7. Scroll down to the Draw function and modify it as in Listing B.10.

LISTING B.10. THE MODIFIED Draw FUNCTION.

 1: void CLine::Draw(CDC * pDC)
 2: {
 3:     // Create a pen
 4:     CPen lpen (PS_SOLID, m_nWidth, m_crColor);
 5:
 6:     // Set the new pen as the drawing object
 7:     CPen* pOldPen = pDC->SelectObject(&lpen);
 8:     // Draw the line
 9:     pDC->MoveTo(m_ptFrom);
10:     pDC->LineTo(m_ptTo);
11:     // Reset the previous pen
12:     pDC->SelectObject(pOldPen);
13: }
8. Scroll down to the Serialize function and modify it as in Listing B.11.

LISTING B.11. THE MODIFIED Serialize FUNCTION.

 1: void CLine::Serialize(CArchive &ar)
 2: {
 3:     CObject::Serialize(ar);
 4:
 5:     if (ar.IsStoring())
 6:         ar << m_ptFrom << m_ptTo << (DWORD) m_crColor << m_nWidth;
 7:     else
 8:         ar >> m_ptFrom >> m_ptTo >> (DWORD) m_crColor >> m_nWidth;
9: }
9. Select the CDay10Doc class in the Class View tab on the workspace pane. Right-click the mouse and choose Add Member Variable from the pop-up menu.

10. Specify the variable type as UINT, the name as m_nWidth, and the access as private. Click OK to add the variable.

11. Open the CDay10Doc source code (Day10Doc.cpp), scroll down to the OnNewDocument function, and edit it as in Listing B.12.

LISTING B.12. THE MODIFIED OnNewDocument FUNCTION.

 1: BOOL CDay10Doc::OnNewDocument()
 2: {
 3:     if (!CDocument::OnNewDocument())
 4:         return FALSE;
 5:
 6:     // TODO: add reinitialization code here
 7:     // (SDI documents will reuse this document)
 8:
 9:     ///////////////////////
10:     // MY CODE STARTS HERE
11:     ///////////////////////
12:
13:     // Initialize the color to black
14:     m_nColor = ID_COLOR_BLACK - ID_COLOR_BLACK;
15:     // Initialize the width to thin
16:     m_nWidth = ID_WIDTH_VTHIN - ID_WIDTH_VTHIN;
17:
18:     ///////////////////////
19:     // MY CODE ENDS HERE
20:     ///////////////////////
21:
22:     return TRUE;
23: }
12. Scroll down to the AddLine function, and modify it as in Listing B.13.

LISTING B.13. THE MODIFIED AddLine FUNCTION.

 1: CLine * CDay10Doc::AddLine(CPoint ptFrom, CPoint ptTo)
 2: {
 3:     static UINT nWidths[5] = { 1, 8, 16, 24, 32};
 4:
 5:     // Create a new CLine object
 6:     CLine *pLine = new CLine(ptFrom, ptTo,
                  Âm_crColors[m_nColor], nWidths[m_nWidth]);
 7:     try
 8:     {
 9:         // Add the new line to the object array
10:         m_oaLines.Add(pLine);
11:         // Mark the document as dirty
12:         SetModifiedFlag();
13:     }
14:     // Did we run into a memory exception?
15:     catch (CMemoryException* perr)
16:     {
17:         // Display a message for the user, giving him or her the
18:         // bad news
19:         AfxMessageBox("Out of memory", MB_ICONSTOP | MB_OK);
20:         // Did we create a line object?
21:         if (pLine)
22:         {
23:             // Delete it
24:             delete pLine;
25:             pLine = NULL;
26:         }
27:         // Delete the exception object
28:         perr->Delete();
29:     }
30:     return pLine;
31: }
13. Add a new member function to the CDay10Doc class. Specify the function type as UINT, the declaration as GetWidth, and the access as public.

14. Edit the GetWidth function, adding the code in Listing B.14.

LISTING B.14. THE GetWidth FUNCTION.

1: UINT CDay10Doc::GetWidth()
2: {
3:     // Return the current width
4:     return ID_WIDTH_VTHIN + m_nWidth;
5: }
15. Select the Resource View tab in the workspace pane. Expand the tree so that you can see the contents of the Menu folder. Double-click the menu resource.

16. Grab the blank top-level menu (at the right end of the menu bar) and drag it to the left, dropping it in front of the View menu entry.

17. Open the properties for the blank menu entry. Specify the caption as &Width. Close the properties dialog.

18. Add submenu entries below the Width top-level menu. Specify the submenus in order, setting their properties as specified in Table B.1.

TABLE B.1. MENU PROPERTY SETTINGS.

Object Property Setting
Menu Entry ID ID_WIDTH_VTHIN
Caption &Very Thin
Menu Entry ID ID_WIDTH_THIN
Caption Thi&n
Menu Entry ID ID_WIDTH_MEDIUM
Caption &Medium
Menu Entry ID ID_WIDTH_THICK
Caption Thic&k
Menu Entry ID ID_WIDTH_VTHICK
Caption Very &Thick

19. Open the Class Wizard. Select the CDay10Doc in the Class Name combo box.

20. Add functions for both the COMMAND and UPDATE_COMMAND_UI event messages for all the width menu entries.

21. After you add the final menu entry function, click Edit Code.

22. Edit the Very Thin menu functions as in Listing B.15.

LISTING B.15. THE VERY THIN MENU FUNCTIONS.

 1: void CDay10Doc::OnWidthVthin()
 2: {
 3:     // TODO: Add your command handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Set the current width to Very Thin
10:     m_nColor = ID_WIDTH_VTHIN - ID_WIDTH_VTHIN;
11:
12:     ///////////////////////
13:     // MY CODE ENDS HERE
14:     ///////////////////////
15: }
16:
17: void CDay10Doc::OnUpdateWidthVthin(CCmdUI* pCmdUI)
18: {
19:     // TODO: Add your command update UI handler code here
20:
21:     ///////////////////////
22:     // MY CODE STARTS HERE
23:     ///////////////////////
24:
25:     // Determine if the Very Thin menu entry should be checked
26:     pCmdUI->SetCheck(GetWidth() == ID_WIDTH_VTHIN ? 1 : 0);
27:
28:     ///////////////////////
29:     // MY CODE ENDS HERE
30:     ///////////////////////
31: }
23. Edit the Thin menu functions as in Listing B.16. Edit the remaining menu functions in the same way, substituting their menu IDs for ID_WIDTH_THIN.

LISTING B.16. THE THIN MENU FUNCTIONS.

 1: void CDay10Doc::OnWidthThin()
 2: {
 3:     // TODO: Add your command handler code here
 4:
 5:     ///////////////////////
 6:     // MY CODE STARTS HERE
 7:     ///////////////////////
 8:
 9:     // Set the current width to Thin
10:     m_nColor = ID_WIDTH_THIN - ID_WIDTH_VTHIN;
11:
12:     ///////////////////////
13:     // MY CODE ENDS HERE
14:     ///////////////////////
15: }
16:
17: void CDay10Doc::OnUpdateWidthThin(CCmdUI* pCmdUI)
18: {
19:     // TODO: Add your command update UI handler code here
20:
21:     ///////////////////////
22:     // MY CODE STARTS HERE
23:     ///////////////////////
24:
25:     // Determine if the Thin menu entry should be checked
26:     pCmdUI->SetCheck(GetWidth() == ID_WIDTH_THIN ? 1 : 0);
27:
28:     ///////////////////////
29:     // MY CODE ENDS HERE
30:     ///////////////////////
31: }


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.