Paint event, Enabled property, and hWnd property example for Visual C++ 4.0

This example uses some Windows C DLL functions to demonstrate the following:

1. Start with the project that you created in Loading and Displaying an Image.

3. In the project Workspace, click the ResourceView tab .

4. Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box.

5. image\btncmd.gif 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.

6. Double-click the button and change IDC_BUTTON1 to IDC_TOGGLE. Also, set the Caption to Toggle.

7. Similarly add a label beneath the Toggle button, naming it IDC_DIRECTIONS, with no Caption.

8. Close all the windows until you reach the project window, answering yes when asked whether to save the changes.

9. Edit TUTORDLG.CPP by double-clicking the file.

10. Near the top of the file (after the last of the preprocessor directives) add the following code:

char ClickEnabledDir[] = "Click on the image for a splat";
char ClickDisabledDir[] = "Click does nothing";
char ClickEnabledTog[] = "&Disable Click";
char ClickDisabledTog[] = "&Enable Click";

11. In the function BOOL CTutorDlg::OnInitDialog(), add the following code before the return:

// Set toggling values
m_Lead1.SetEnabled(TRUE);
GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickEnabledDir);
GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickEnabledTog);

12. Close the file, choosing Yes when asked whether to save the changes.

13. 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.

14. Edit the OnToggle function so that it appears as follows:

void CTutorDlg::OnToggle() 
{
  if (m_Lead1.GetEnabled())
  {
    m_Lead1.SetEnabled(FALSE);
    GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickDisabledDir);
    GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickDisabledTog);
  }
  else
  {
    m_Lead1.SetEnabled(TRUE);
    GetDlgItem(IDC_DIRECTIONS)->SetWindowText(ClickEnabledDir);
    GetDlgItem(IDC_TOGGLE)->SetWindowText(ClickEnabledTog);
  }
}

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_LEAD1.

c. In the Messages list box, select Click.

d. Click the Add function button. Choose OK for the default function name (OnClickLead1).

e. Click the Edit Code button to start entering the code as follows:

void CTutorDlg::OnClickLead1()
{
  HWND hCtl;
  short nScaleMode;
  RECT  MyRect;

  hCtl = (HWND)m_Lead1.GetHWnd();

  nScaleMode = m_Lead1.GetScaleMode();
  m_Lead1.SetScaleMode(3); // Pixels
  MyRect.left = 0;
  MyRect.top = 0;
  MyRect.bottom = (long)m_Lead1.GetScaleHeight();
  MyRect.right = (long)m_Lead1.GetScaleWidth();
  CWnd::FromHandle(hCtl)->InvalidateRect (&MyRect, TRUE);
  m_Lead1.SetScaleMode(nScaleMode);
}

16. 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_LEAD1.

c. In the Messages list box, select Paint.

d. Click the Add function button. Choose OK for the default function name (OnPaintLead1).

e. Click the Edit Code button to start entering the code as follows:

void CTutorDlg::OnPaintLead1()
{
  CDC *LeadClientDC;
  HWND hLeadWnd;
  int xStart, xDelta, xEnd;
  int yStart, yDelta, yEnd;
  int i;
  static int seed = 2;
  srand(seed);
  hLeadWnd = (HWND)m_Lead1.GetHWnd();

  // Create a device context for the control that the GDI functions can access
  LeadClientDC = CDC::FromHandle((HDC)m_Lead1.GetClientDC());

  // Draw random lines on the control. This overlay does not affect the bitmap.
  xStart = (int)((rand() * (long)m_Lead1.GetScaleWidth() / RAND_MAX) + 1);
  yStart = (int)((rand() * (long)m_Lead1.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_Lead1.ReleaseClientDC();
  seed = rand();
}

17. Rebuild and run the application.