• No se han encontrado resultados

When you design the main dialog, you’ll add the second window that you’ll use as a canvas to paint your graphics on. This dialog will be a modeless dialog, which will remain open the entire time the application is running. You will put no controls on the dialog, providing a clean canvas for drawing.

To create this second dialog, go to the Resources tab in the workspace pane. Right-click the Dialogs folder in the resource tree. Select Insert Dialog from the pop-up menu. When the new dialog is open in the window designer, remove all of the controls from the win- dow. After you remove all of the controls, open the properties dialog for the window and uncheck the System Menu option on the second tab of properties. This will prevent the user from closing this dialog without exiting the application. You’ll also want to give this dialog window an object ID that will describe its function, such as IDD_PAINT_DLG.

After you finish designing the second dialog, create a new class for this window by opening the Class Wizard. When you try to open the Class Wizard, you’ll be asked if you want to create a new class for the second dialog window. Leave this option at its default setting and click the OK button. When asked to specify the name of the new class on the next dialog, give the class a suitable name, such as CPaintDlg, and be sure that the base

class is set to CDialog. After you click OK on this dialog and create the new class, you

8

You need to make sure that the new dialog is selected when you try to open the Class Wizard. If the dialog is not selected, and you’ve switched to another object, or even some code in your application, the Class Wizard will not know that you need a class for the second dialog in your application.

Note

Now that you have the second dialog defined, you need to add the code in the first dia- log window to open the second dialog. You can accomplish this by adding two lines of code to the OnInitDialogfunction in the first window’s class. First, create the dialog using

the Createmethod of the CDialogclass. This function takes two arguments: the object ID

of the dialog and a pointer to the parent window, which will be the main dialog. The sec- ond function will be the ShowWindowfunction, passing the value S W _ S H O Was the only

argument. This function displays the second dialog next to the first dialog. Add a couple of lines of variable initialization to make your OnInitDialogfunction resemble Listing 8.1.

LISTING8.1.THEOnInitDialogFUNCTION.

1: BOOL CGraphicsDlg::OnInitDialog() 2: { 3: CDialog::OnInitDialog(); 4: . . . 27:

28: // TODO: Add extra initialization here 29:

30: /////////////////////// 31: // MY CODE STARTS HERE 32: /////////////////////// 33:

34: // Initialize the variables and update the dialog window 35: m_iColor = 0;

36: m_iShape = 0; 37: m_iTool = 0; 38: UpdateData(FALSE); 39:

40: // Create the second dialog window 41: m_dlgPaint.Create(IDD_PAINT_DLG, this); 42: // Show the second dialog window 43: m_dlgPaint.ShowWindow(SW_SHOW);

LISTING8.1.CONTINUED

44:

45: /////////////////////// 46: // MY CODE ENDS HERE 47: /////////////////////// 48:

49: return TRUE; // return TRUE unless you set the focus to a control 50: }

Before you can compile and run your application, you’ll need to include the header for the second dialog class in the source code for the first dialog. You’ll also need to add the second dialog class as a variable to the first—which is a simple matter of adding a mem- ber variable to the first dialog class, specifying the variable type as the class type, in this case CPaintDlg, giving the variable the name that you used in Listing 8.1,m_dlgPaint, and

specifying the variable access as private. To include the header file in the first dialog, scroll to the top of the source code for the first dialog and add an include statement, as in Listing 8.2.

LISTING8.2.THE INCLUDE STATEMENT OF THE MAIN DIALOG.

1: // GraphicsDlg.cpp : implementation file 2: // 3: 4: #include “stdafx.h” 5: #include “Graphics.h” 6: #include “PaintDlg.h” 7: #include “GraphicsDlg.h” 8:

Conversely, you’ll need to include the header file for the main dialog in the source code for the second dialog. You can edit this file,PaintDlg.cpp, making the include statements

match those in Listing 8.2.

If you compile and run your application, you should see your second dialog window open along with the first window. What you’ll also noticed is that when you close the first dialog, and thus close the application, the second dialog window also closes, even though you didn’t add any code to make this happen. The second dialog is a child win- dow to the first dialog. When you created the second dialog, on line 41 of the code list- ing, you passed a pointer to the first dialog window as the parent window for the second window. This set up a parent-child relationship between these two windows. When the

8

parent closes, so does the child. This is the same relationship the first dialog window has with all of the controls you placed on it. Each of those controls is a child window of the dialog. In a sense, what you’ve done is make the second dialog just another control on the first dialog.