Project Menu

Chapter 26 - Getting Started with ActiveX Controls

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Customizing the Initial Control
The ClassWizard can be used to modify the default custom control produced by the ControlWizard. To modify the default custom control described in the previous section, the following features will be added to the project:
  The TDCtrl control will always draw a rectangle instead of the default ellipse.
  The TDCtrl surface will be a unique color.
  The TDCtrl control will respond to a mouse event within the control and print the current system time and date within the control.
All of these new features can be added to the control by just working with the TDCtrlCtl.cpp and TDCtrlCtl.h files. In the next section, we’ll add several of the new features.
Changing the Shape, Size, and Colors of the TDCtrl
From within the Visual C++ compiler, use the View menu to select the ClassWizard menu item. The following modifications allow the shape and color properties of the control to be modified:
  1. Select the Automation tab from within the ClassWizard dialog box.
  2. Select CTDCtrlCtrl from the Class name list box.
  3. Use the Add Property button to display the Add Property dialog box. See Figure 26-9.
Figure 26-9: Use the Add Property dialog box to add the TDShape property
  4. Enter the name TDShape as the External name.
  5. Select Member variable as the Implementation.
  6. Choose BOOL from the drop-down Type list box. Notice that the Notification function edit control contains OnTDShapeChanged. The member variable
is
m_tDShape.
  7. Accept these values with the OK button, and return to the OLE Automation tab.
  8. Select the Add Property button again, and display the Add Property dialog box.
  9. In the edit control of the External name combo box, select BackColor from the drop-down list of available items.
  10. For an Implementation, select Stock.
  11. Accept these values with the OK button, and return to the Automation tab. The MFC ClassWizard dialog box should be similar to Figure 26-10.
Figure 26-10: The ClassWizard will add BackColor and TDShape properties to the project
  12. Use the OK push button to accept the choices and close the ClassWizard.
The ClassWizard will create the code to add the TDShape and BackColor properties to the CTDCtrlCtrl class. The CTDCtrlCtrl class’s dispatch map will be altered to accommodate the TDShape property. A declaration for the OnTDShapeChanged( ) function is added to the TDCTRLCTL.H header file.
The previous changes are added automatically by the ClassWizard. Now it becomes our job to write the code that reacts to these changes.
Return to the TDCTRLCTL.CPP File
The following listing shows the code we’ve modified in the TDCTRLCTL.CPP file. This file is identical to the default file returned by the ControlWizard, except for the lines of code set in a bold font. Make these changes to your file, too:
/////////////////////////////////////////////////////////////
// CTDCtrlCtrl::OnDraw - Drawing function

void CTDCtrlCtrl::OnDraw(CDC* pdc, const CRect& rcBounds,
                        const CRect& rcInvalid)
{
   CBrush* pOldBrush;
   CBrush NewBrush;  
   CPen* pOldPen;
   CPen NewPen;
  
   pdc->FillRect(rcBounds,CBrush::FromHandle((HBRUSH) \
                 GetStockObject(WHITE_BRUSH)));
 
   NewPen.CreatePen(PS_SOLID,3,RGB(0,0,0));
   pOldPen=(CPen*)pdc->SelectObject(&NewPen);
 
   // Create a yellow brush
   NewBrush.CreateSolidBrush(RGB(255,255,0));
   pOldBrush=(CBrush*)pdc->SelectObject(&NewBrush);

 // Draw and fill the rectangle

   pdc->Rectangle(rcBounds);

 pdc->SelectObject(pOldPen);

   pdc->SelectObject(pOldBrush);
}
The modified control is drawn with the Rectangle( ) function and filled with a yellow brush.
Responding to Mouse Events
In this section, you’ll learn how to make the TDCtrl control respond to a mouse event. If the cursor is on the TDCtrl control when the left mouse button is depressed, the TDCtrl will change to a light gray color and report the system date and time to the control. The color change, date, and time information are indicators that a control “hit” has occurred.
Here is a list of steps needed to implement the “hit” features:
  1. Select the Automation tab from the MFC ClassWizard dialog box.
  2. Select CTDCtrlCtrl from the Class name list box.
  3. Select the Add Property button and display the Add Property dialog box.
  4. In the edit control of the External names combo box, type HitTDCtrl.
  5. For an Implementation, check to make sure Member Variable is selected.
  6. Select OLE_COLOR from the Type list box, and clear the Notification function edit control.
  7. Close the Add Property dialog box by selecting the OK button, and return to the Automation tab. Your screen should look similar to Figure 26-11.
Figure 26-11: The HitTDCtrl property will allow the control to respond to a mouse event
  8. Select the Message Maps tab.
  9. Select CTDCtrlCtrl from the Class name list box.
  10. From the Object IDs list box, select CTDCtrlCtrl and then view a list of messages in the Messages list box.
  11. Select WM_LBUTTONDOWN from the Messages list box.
  12. Choose the Add Function button.
  13. Repeat this process by selecting WM_LBUTTONUP. Your screen should look similar to Figure 26-12.
Figure 26-12: Two member functions are used to respond to mouse button events
  14. Select the OK push button to accept the choices and close the ClassWizard.
The ClassWizard will automatically create the code to add the HitTDCtrl property and the outlines for the above function implementations for the CTDCtrlCtrl class.
These changes are added by the ClassWizard, but we must now write the code that reacts to these events.
The TDCTRLCTL.H Header File
An additional insertion must be made in the header file to accommodate a new function. The function is used to change the control’s color when a hit occurs. Insert the function prototype HitTDCtrl( ), just under the destructor shown in the following partial listing of the TDCTRLCTL.H header file:
// Implementation
protected:
 ~CTDCtrlCtrl( );

void HitTDCtrl(CDC* pdc);            // Blink the color
    .
    .
    .
The code will now be added for detecting mouse clicks within the control.
Return to the TDCTRLCTL.CPP File
The control’s face will change color when the user clicks the left mouse button within the rectangular area. The event notification, in part, is handled by the DoPropExchange( ) function. Here is the DoPropExchange( ) function and modification showing the new line in a bold font:
/////////////////////////////////////////////////////////////
// CTDCtrlCtrl::DoPropExchange - Persistence support

void CTDCtrlCtrl::DoPropExchange(CPropExchange* pPX)
{
 ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
 COleControl::DoPropExchange(pPX);

 // TODO: Call PX_ functions for persistent custom property.

 // Use a light-gray color to show a hit
 PX_Long(pPX,_T(“HitTDCtrl”), (long &)m_hitTDCtrl,
                 RGB(200, 200, 200));
}
This function is responsible for initializing the m_hitTDCtrl member variable to a light gray color. The variable m_hitTDCtrl must be cast to a long since it is an unsigned long value.
The ClassWizard added the OnLButtonDown( ) and OnLButtonUp( ) function outlines. The following listing shows the modifications we made to each function in a bold font.
This code checks to make sure the left mouse button was clicked within the face of the clock. If it was, the HitTDCtrl( ) function will be called to change the color of the clock face from yellow to light gray.
When the left mouse button is released, the OnLButtonUp( ) function merely invalidates the control, forcing a repaint in the face to yellow.
The InFace( ) function is used to determine if the left mouse button was depressed within the clock face. All of the following code must be added to the end of the CLOCKCTL.CPP listing:
void CTDCtrlCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add message handler code here and/or call default
 
 CDC* pdc;

 //Blink a color change for control

 pdc = GetDC( );
 HitTDCtrl(pdc);
 ReleaseDC(pdc);

 COleControl::OnLButtonDown(nFlags, point);
}
void CTDCtrlCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
 // TODO: Add message handler code here and/or call default

 InvalidateControl( );

 COleControl::OnLButtonUp(nFlags, point);
}
This function first locates the center of the TDCtrl control and then determines if the hit occurred within the rectangle.
If the point falls within the rectangle, the HitTDCtrl( ) function is called. All of the code in the following function must be added to the end of the TDCTRLCTL.CPP listing:
void CTDCtrlCtrl::HitTDCtrl(CDC* pdc)
{

 CBrush* pOldBrush;
 CBrush hitBrush(TranslateColor(m_hitTDCtrl));
 CRect rc;
 TEXTMETRIC tm;
 struct tm *date_time;
 time_t timer;

 // Background mode to transparent
 pdc->SetBkMode(TRANSPARENT);

 GetClientRect(rc);
 pOldBrush=pdc->SelectObject(&hitBrush);

 // Draw and fill the rectangle
 pdc->Rectangle(rc);

 // Get time and date
 time(&timer);
 date_time=localtime(&timer);
 const CString& strtime = asctime(date_time);

 // Get Font information then print
 pdc->GetTextMetrics(&tm);
 pdc->SetTextAlign(TA_CENTER | TA_TOP);
 pdc->ExtTextOut((rc.left + rc.right) / 2,
                 (rc.top + rc.bottom - tm.tmHeight) /2,
                 ETO_CLIPPED, rc, strtime,
                 strtime.GetLength( ) - 1, NULL);
 pdc->SelectObject(pOldBrush);
}
The code in this function selects the light gray brush, defined earlier, and repaints the entire TDCtrl control area. The time and date information is accessed with normal C functions.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net