Design of ActiveX Control Using MFC-Step 2

Source: Internet
Author: User
Tags getcolor
Pain: I have already finished writing it. Firefox reported an error and had to try again.

For more information about the property page, see the Introduction to the property page on msdn.
You can use the property page to operate on the properties of the control. You can set one or more property pages. Some Properties of the control can be displayed on each property page. These attributes can be from one control or multiple controls, that is, the attributes of multiple controls can be displayed on one property page.
Each ActiveX control property page is an in-process object with its own CLSID. Each property page implements the IPropertyPage interface. IPropertyPage: SetObjects is used to pass the interface pointer (IUnknown) of the control object to the property page.
The COlePropertyPage of MFC also implements the IPropertyPage interface, which saves the control object pointer in an array of IDispatch interface pointers. This array can be accessed through COlePropertyPage: GetObjectArray (), so that the control attributes can be accessed through the IDispatch interface. The headache is that the control attributes can be accessed through IDispatch, it is really troublesome. MFC provides the COleDispatchDriver to help access. Unfortunately, it can only slightly simplify some operations. In fact, for your convenience, MFC also provides a set of functions in the COlePropertyPage to help access the properties of the control, but this group of functions is not published in msdn. The following is the declaration of this group of functions:
// DDP _ property get/set helper routines
BOOL SetPropText (LPCTSTR pszPropName, BYTE & Value );
BOOL GetPropText (LPCTSTR pszPropName, BYTE * pValue );
BOOL SetPropText (LPCTSTR pszPropName, short & Value );
BOOL GetPropText (LPCTSTR pszPropName, short * pValue );
BOOL SetPropText (LPCTSTR pszPropName, int & Value );
BOOL GetPropText (LPCTSTR pszPropName, int * pValue );
BOOL SetPropText (LPCTSTR pszPropName, UINT & Value );
BOOL GetPropText (LPCTSTR pszPropName, UINT * pValue );
BOOL SetPropText (LPCTSTR pszPropName, long & Value );
BOOL GetPropText (LPCTSTR pszPropName, long * pValue );
BOOL SetPropText (LPCTSTR pszPropName, DWORD & Value );
BOOL GetPropText (LPCTSTR pszPropName, DWORD * pValue );
BOOL SetPropText (LPCTSTR pszPropName, CString & Value );
BOOL GetPropText (LPCTSTR pszPropName, CString * pValue );
BOOL SetPropText (LPCTSTR pszPropName, float & Value );
BOOL GetPropText (LPCTSTR pszPropName, float * pValue );
BOOL SetPropText (LPCTSTR pszPropName, double & Value );
BOOL GetPropText (LPCTSTR pszPropName, double * pValue );
BOOL SetPropCheck (LPCTSTR pszPropName, int Value );
BOOL GetPropCheck (LPCTSTR pszPropName, int * pValue );
BOOL SetPropRadio (LPCTSTR pszPropName, int Value );
BOOL GetPropRadio (LPCTSTR pszPropName, int * pValue );
BOOL SetPropIndex (LPCTSTR pszPropName, int Value );
BOOL GetPropIndex (LPCTSTR pszPropName, int * pValue );

In general, we do not directly access the properties of the Control. Everything is done by the framework (the framework uses DoDateExchange and DoDataExchange uses the SetPropText/GetPropText function group above ).
However, sometimes we want to directly access the properties and methods of the control in the control. For common attributes, the above GetPropText and SetPropText are enough to meet the requirements. But for attributes and methods with parameters, we have to honestly use the COleDispatchDriver to InvokeHelper.

The following is an example of toop for accessing control properties and methods on the properties page.

1. the control sets a string array CStringArray m_saItems and provides the methods long Add (LPCTSTR strItem) and get/set method attributes BSTR Item (long lItem) to access a string in the array, there is also a read-only attribute long Count (in the Add attribute wizard, set to get/set methods type attribute, and remove SetCount is set to read-only attribute)
2. The control also sets the member variable attribute OLE_COLOR Color.
3. attributes and methods are implemented as follows:

Void CToppCtrl: OnColorChanged ()
{
// TODO: Add notification handler code
SetModifiedFlag ();
}

Long CToppCtrl: Add (LPCTSTR strItem)
{
// TODO: Add your dispatch handler code here
Return m_saItems.Add (strItem );
Return 0;
}

BSTR CToppCtrl: GetItem (long nItem)
{
CString strResult;
// TODO: Add your property handler here
If (nItem> = 0 & nItem <m_saItems.GetSize ()){
StrResult = m_saItems [nItem];
}
Return strResult. AllocSysString ();
}

Void CToppCtrl: SetItem (long nItem, LPCTSTR lpszNewValue)
{
// TODO: Add your property handler here
If (nItem> = 0 & nItem <m_saItems.GetSize ()){
M_saItems [nItem] = lpszNewValue;
}
SetModifiedFlag ();
}

Long CToppCtrl: GetCount ()
{
// TODO: Add your property handler here
Return m_saItems.GetSize ();
Return 0;
}

After the control is compiled, it is the highlight of the property page:
1. interface. For the string array in the control, an edit box and a button ("Add project") are added to call the Add method of the control; added a list box to call the Item attribute of the control to fill the list box.
2. page, for the Color attribute of the control, add a button ("set Color") and a static text box (Caption is "Look at my Color", ID is IDC_STATIC_COLOR) the text background color of the text box is the obtained color.
3. the added member function BOOL AddItem (LPCTSTR lpszItem) is used to call the Add method of the control to Add a string, and the CString GetItem (long lItem) function is added to obtain the string corresponding to the lItem item in the string array, add long GetCount () to obtain the number of strings in the array. Add the COLORREF GetColor () function to obtain the Color attribute of the Control. Add the SetColor () function to set the Color attribute of the control, the implementation of these functions is as follows:

BOOL CToppPropPage: AddItem (LPCTSTR lpszItem)
{
USES_CONVERSION;
COleDispatchDriver PropDispDriver;
BOOL bResult = FALSE;
ULONG nObjects = 0;
LPDISPATCH * ppDisp = GetObjectArray (& nObjects );
For (ULONG I = 0; I <nObjects; I ++)
{
DISPID dwDispID;
LPCOLESTR lpOleStr = T2COLE ("Add ");
If (SUCCEEDED (ppDisp [I]-> GetIDsOfNames (IID_NULL, (LPOLESTR *) & lpOleStr, 1, 0, & dwDispID )))
{
PropDispDriver. AttachDispatch (ppDisp [I], FALSE );
BYTE rgbParams [] = VTS_BSTR;
Long lret = 0;
PropDispDriver. InvokeHelper (dwDispID, DISPATCH_METHOD, VT_I4, & lret, rgbParams, lpszItem );
PropDispDriver. DetachDispatch ();
BResult = TRUE;
}
}
Return bResult;
}

CString CToppPropPage: GetItem (long lItem)
{
CString str = _ T ("");
USES_CONVERSION;
COleDispatchDriver PropDispDriver;
ULONG nObjects = 0;
LPDISPATCH * ppDisp = GetObjectArray (& nObjects );
For (ULONG I = 0; I <nObjects; I ++)
{
DISPID dwDispID;
LPCOLESTR lpOleStr = T2COLE ("Item ");
If (SUCCEEDED (ppDisp [I]-> GetIDsOfNames (IID_NULL, (LPOLESTR *) & lpOleStr, 1, 0, & dwDispID )))
{
PropDispDriver. AttachDispatch (ppDisp [I], FALSE );
BYTE rgbParams [] = VTS_I4;
PropDispDriver. InvokeHelper (dwDispID, DISPATCH_PROPERTYGET, VT_BSTR, & str, rgbParams, lItem );
PropDispDriver. DetachDispatch ();
}
}
Return str;
}

COLORREF CToppPropPage: GetColor ()
{
Long l = 0;
GetPropText ("Color", & l );
COLORREF cr = RGB (0, 0, 0 );
: OleTranslateColor (OLE_COLOR) l, NULL, & cr );
Return cr;
}

Void CToppPropPage: SetColor (COLORREF cr)
{
SetPropText ("Color", cr );
}

Long CToppPropPage: GetCount ()
{
Long lcount = 0;
GetPropText ("Count", & lcount );
Return lcount;
}

4. Add response function ("add project ")

Void CToppPropPage: OnButtonAdditem ()
{
// TODO: Add your control notification handler code here
UpdateData ();
AddItem (m_strItem); // m_strItem is the CString object associated with the editing box.

// Fill the list box
Long lcount = 0;
GetPropText ("Count", & lcount );
M_list.ResetContent (); // m_list is the CListBox object associated with the list box.
For (int I = 0; I <lcount; I ++ ){
M_list.AddString (GetItem (I ));
}
}

5. Add the button ("set Color") Response Function to set the Color attribute of the control.
Void CToppPropPage: OnButtonSetcolor ()
{
// TODO: Add your control notification handler code here
CColorDialog dlg;
If (dlg. DoModal () = IDOK ){
Long lcolor = dlg. GetColor ();
SetPropText ("Color", lcolor );
GetDlgItem (IDC_STATIC_COLOR)-> Invalidate ();
}
}

6. Add the WM_CTLCOLOR message response function, obtain the Color attribute of the control in the function, and use the Color to set the text background Color of the static text box.
HBRUSH CToppPropPage: OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
HBRUSH hbr = COlePropertyPage: OnCtlColor (pDC, pWnd, nCtlColor );

// TODO: Change any attributes of the DC here
If (nCtlColor = CTLCOLOR_STATIC ){
COLORREF cr = GetColor ();
PDC-> SetBkColor (cr );
}
// TODO: Return a different brush if the default is not desired
Return hbr;
}

7. OK. Compile. You can test it in ActiveX Control Container.

The following is the control and property page, because the following is added with many other effects, please refer to this series of subsequent articles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.