Sunday, August 24, 2008

ActiveX Directory Control

This article demonstrates use of stock property pages in an ActiveX control. The system provides three stock property pages that ActiveX controls can use: a color page for color properties, a picture page for picture properties and a font page for font properties. Here we have created an edit control whose properties can be changed using the color and a font property page. This control has been developed using 'MFC ActiveX Control Wizard'. While going through the steps of the Wizard we chose 1 control for ' ' and 'EDIT' window as a subclass. To add property pages to the control do the following:

  1. Open 'MFC ClassWizard' dialog box. Select the 'Automation' tab sheet. Click on the 'Add Property' button.

  2. From the 'External Name' combo box select 'Font' and 'Back Color'. By adding external names we can interact with the property pages.

  3. Add the following two lines:

PROPPAGEID(CLSID_CFontPropPage)
PROPPAGEID(CLSID_CColorPropPage)
between
BEGIN_PROPPAGEIDS( )
END_PROPPAGEIDS( )

This helps in adding property pages to the 'Properties' dialog of our control. We have used the color property page to select the color for the background of the edit window and font property page to select the style of text which appears in the edit window. To paint the background of the edit control we have added following lines in the OnDraw( ) function of the CStockpagesCtrl class.

COLORREF clrBackGround = TranslateColor ( GetBackColor( ) ) ;
CBrush* prevbrush ;
CBrush brBackGround ( clrBackGround ) ;
pdc -> FillRect ( rcBounds, &brBackGround ) ;
prevbrush = pdc -> SelectObject ( &brBackGround ) ;
pdc -> SelectObject ( prevbrush ) ;
CRect rc ( rcBounds ) ;
pdc -> DrawEdge ( rc, EDGE_SUNKEN, BF_RECT ) ;

As we type in the edit window we get the default backgorund color of the text. This brings a bad effect of erasing the background color of the edit window. To prevent this we have to make the default background color of the text as transparent. For this we have added a message handler CtlColor( ) for the message ON_WM_CTLCOLOR_REFLECT( ) and have added the following code to it:

pDC->SetBkMode(TRANSPARENT);

To change the font of text we just have to select the type from the property page and the effect is seen without writing a single line of code.


No comments:

Your Title