RgnChange example for C++ 5.0 and later
This code for the RgnChange event updates a static text control using the GetRgnArea method. You can test it by adding a text box for the lesson described in Outlining, Dragging, and Pasting a Region.
The first two steps describe how to add a static text control. The last step shows the sample code.
1. |
Add a Static Text control as follows: | |
|
a. |
In the Project Workspace, click the ResourceView tab. |
|
b. |
Double-click the tutor resources folder to open it. |
|
c. |
Double-click the Dialog folder to open it. |
|
d. |
Double-click IDD_TUTOR_DIALOG to design the dialog box. |
|
e. |
Click the Static Text control; then click and drag its shape in the dialog box. |
|
f. |
Right-click the new control to edit its properties. |
|
g. |
Change the ID to IDC_TEXT1. |
|
h. |
Press Ctrl-F4 to close all windows back to the Project Workspace. |
2. |
Do the following to add m_Text1 to the CTutorDlg class and link the variable to the Static Text control using dynamic data exchange: | |
|
a. |
Press Ctrl-W to go to the MFC ClassWizard. |
|
b. |
Click the Member Variables tab. |
|
c. |
In the Class Name box, select CTutorDlg. |
|
d. |
In the Control IDs list, select IDC_TEXT1. |
|
e. |
Click the Add Variable... button. |
|
f. |
Specify m_Text1 as the variable name, Control as the category, and Cstatic as the variable type. |
|
g. |
Click OK to close the dialog box, and click OK to close the MFC ClassWizard. |
3. |
Use the MFC ClassWizard to add the RgnChange event, and code it as follows: |
void CTutorDlg::OnRgnChangeLeadrasterview1()
{
TCHAR szText[128];
CRect rcWindow;
m_LEADRasterView1.GetWindowRect(rcWindow);
wsprintf(szText,
TEXT("Number of pixels in region: %u "),
m_LEADRasterView1.GetRaster().GetRgnArea());
m_Text1.SetWindowText(szText);
m_Text1.UpdateWindow();
}