Paint event example for C++ 5.0 and later
This example uses some Windows API functions to demonstrate the following:
Paint event
Click event
Enabled property
hWnd property
1. |
Start with the project that you created in Loading and Displaying an Image. |
2. |
In the project Workspace, click the ResourceView tab . |
3. |
Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box. |
4. |
Add a new button beside the OK button. To do this, select the button control on the Controls toolbar. Then, click and drag to position the button on the dialog box. |
5. |
Double-click the button and change IDC_BUTTON1 to IDC_TOGGLE. Also, set the Caption to Toggle. |
6. |
Similarly add a label beneath the Toggle button, naming it IDC_DIRECTIONS, with no Caption. |
7. |
Close all the windows until you reach the project window, answering yes when asked whether to save the changes. |
8. |
Edit TUTORDLG.CPP by double-clicking the file. |
9. |
Near the top of the file (after the last of the preprocessor directives) add the following code: |
TCHAR ClickEnabledDir[] = TEXT("Click on the image for a splat");
TCHAR ClickDisabledDir[] = TEXT("Click does nothing");
TCHAR ClickEnabledTog[] = TEXT("&Disable Click");
TCHAR ClickDisabledTog[] = TEXT("&Enable Click");
10. |
In the function BOOL CTutorDlg::OnInitDialog(), add the following code before the return: |
// Set toggling values
m_LEADRasterView1.SetEnabled(TRUE);
GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickEnabledDir);
GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickEnabledTog);
11. |
Close the file, choosing Yes when asked whether to save the changes. |
|
12. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
In the Class name combo box, select CTutorDlg. |
|
b. |
In the Object IDs list box, select IDC_TOGGLE. |
|
c. |
In the Messages list box, select BN_CLICKED. |
|
d. |
Click the Add function button. Choose OK for the default function name (OnToggle). |
|
e. |
Click the Edit Code button to start entering the code. |
13. |
Edit the OnToggle function so that it appears as follows: |
void CTutorDlg::OnToggle()
{
if (m_LEADRasterView1.GetEnabled())
{
m_LEADRasterView1.SetEnabled(FALSE);
GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickDisabledDir);
GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickDisabledTog);
}
else
{
m_LEADRasterView1.SetEnabled(TRUE);
GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickEnabledDir);
GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickEnabledTog);
}
}
14. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
In the Class name combo box, select CTutorDlg. |
|
b. |
In the Object IDs list box, select IDC_LEADRASTERVIEW1. |
|
c. |
In the Messages list box, select Click. |
|
d. |
Click the Add function button. Choose OK for the default function name (OnClickLeadrasterview1). |
|
e. |
Click the Edit Code button to start entering the code as follows: |
void CSaveArrayDlg::OnClickLeadrasterview1()
{
HWND hCtl;
long nScaleMode;
RECT MyRect;
hCtl = (HWND)m_LEADRasterView1.GetWindow();
nScaleMode = m_LEADRasterView1.GetScaleMode();
m_LEADRasterView1.SetScaleMode(3); // Pixels
MyRect.left = 0;
MyRect.top = 0;
MyRect.bottom = (long)m_LEADRasterView1.GetScaleHeight();
MyRect.right = (long)m_LEADRasterView1.GetScaleWidth();
CWnd::FromHandle(hCtl)->InvalidateRect (&MyRect, TRUE);
m_LEADRasterView1.SetScaleMode(nScaleMode);
}
15. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
In the Class name combo box, select CTutorDlg. |
|
b. |
In the Object IDs list box, select IDC_LEADRASTERVIEW1. |
|
c. |
In the Messages list box, select Paint. |
|
d. |
Click the Add function button. Choose OK for the default function name (OnPaintLeadrasterview1). |
|
e. |
Click the Edit Code button to start entering the code as follows: |
void CTutorDlg::OnPaintLeadrasterview1(long hDC)
{
CDC *LeadClientDC;
HWND hLeadWnd;
int xStart, xDelta, xEnd;
int yStart, yDelta, yEnd;
int i;
static int seed = 2;
srand(seed);
hLeadWnd = (HWND)m_LEADRasterView1.GetWindow();
// Create a device context for the control that the GDI functions can access
LeadClientDC = CDC::FromHandle((HDC)m_LEADRasterView1.GetClientDC());
// Draw random lines on the control. This overlay does not affect the bitmap.
xStart = (int)((rand() * (long)m_LEADRasterView1.GetScaleWidth() / RAND_MAX) + 1);
yStart = (int)((rand() * (long)m_LEADRasterView1.GetScaleHeight() / RAND_MAX) + 1);
LeadClientDC->MoveTo(xStart, yStart);
for( i = 1; i < 50; i++)
{
xDelta = (int)(81L * rand() / RAND_MAX) - 40;
yDelta = (int)(81L * rand() / RAND_MAX) - 40;
xEnd = xStart + xDelta;
yEnd = yStart + yDelta;
LeadClientDC->LineTo(xEnd, yEnd);
LeadClientDC->MoveTo(xStart, yStart);
}
// Remove the lock from the device control
m_LEADRasterView1.ReleaseClientDC();
seed = rand();
}
16. |
Rebuild and run the application. |