How to Use font 4 in ActiveX Controls

Source: Internet
Author: User
Tags textout

How to Use font 4 in ActiveX Controls

Add color property page and font property page

Open the labelex0ctl. cpp file of the project and modify the following code:

BEGIN_PROPPAGEIDS(CLabelEx0Ctrl, 1)
   PROPPAGEID(CLabelEx0PropPage::guid)
END_PROPPAGEIDS(CLabelEx0Ctrl)
Modified code:Begin_proppageids (clabelex0ctrl, 3) // the actual number depends on the actual situation.
Proppageid (clabelex0proppage: guid) // system property page
Proppageid (clsid_ccolorproppage) // Add the color property page
Proppageid (clsid_cfontproppage) // Add to font attribute page
End_proppageids (clabelex0ctrl)
Modify the system property page

Open the system's built-in property page resource idd_proppage_labelex0, and adjust the size of the property page from 250x62 to 250x110. Delete the original static text box and add nine static text boxes. The content of the static text box is "captionup:", "captionmiddle:", "captiondown:", "xup :", "Yup:", "xmiddle:", "ymiddle:", "xdown:", "ydown :". Add nine corresponding edit box controls with IDs: idc_captionup, lower, idc_captiondown, idc_xup, idc_yup, idc_xmiddle, idc_ymiddle, idc_xdown, and idc_ydown. Idc_captionup, lower, and idc_captiondown are used to input the text content of the upper, middle, and lower mark, respectively, coordinates of the text. 3-1.

Figure 3-1

Press "Ctrl + W" to open classwizard, select the member variables label, and select clabelex0proppage, control IDs: Select idc_captionup, and click Add variable... Button to bring up the Add member variable dialog box.

In the Add member variable dialog box, enter m_captionup in the member variable name box, select value in category, select cstring in the variable type box, select captionup in the optional property Name box, and click OK. Repeat the preceding steps to create and add member variables for the remaining eight editing controls. The following table lists the mappings:

Control ID

Member variable name

Category

Variable type

Optional property name

Idc_captionup

M_captionup

Value

Cstring

Captionup

Idc_captionmiddle

M_captionmiddle

Value

Cstring

Captionmiddle

Idc_captiondown

M_captiondown

Value

Cstring

Captiondown

Idc_xup

M_xup

Value

Long

Xup

Idc_yup

M_yup

Value

Long

Yup

Idc_xmiddle

M_xmiddle

Value

Long

Xmiddle

Idc_ymiddle

M_ymiddle

Value

Long

Ymiddle

Idc_xdown

M_xdown

Value

Long

Xdown

Idc_ydown

M_ydown

Value

Long

Ydown

Click OK again to close classwizard. Modify the control code in the control header file (labelex0ctl. h) and add the declaration of three protected member variables:

protected:
CFontHolder m_fontUp;
CFontHolder m_fontMiddle;
CFontHolder m_fontDown;

In the control execution file (labelex0ctl. cpp), perform the following operations: Initialize in the control Constructor

M_fontup, m_fontmiddle, m_fontdown.CLabelEx0Ctrl::CLabelEx0Ctrl():m_fontUp(&m_xFontNotification),
m_fontMiddle(&m_xFontNotification),m_fontDown(&m_xFontNotification)
{
   InitializeIIDs(&IID_DLabelEx0, &IID_DLabelEx0Events);
   // TODO: Initialize your control''s instance data here.
}

Declare a static fontdesc structure that contains default font attributes.

static const FONTDESC _fontdescCaption =
  { sizeof(FONTDESC), OLESTR("MS Sans Serif"), FONTSIZE( 12 ), FW_BOLD,
   ANSI_CHARSET, FALSE, FALSE, FALSE };

In the dopropexchange member function of the control, add the call of the PX _ function.

Void clabelex0ctrl: dopropexchange (cpropexchange * ppx)
{
Exchangeversion (ppx, makelong (_ wverminor, _ wvermajor ));
Colecontrol: dopropexchange (ppx );
// Todo: Call PX _ functions for each persistent custom property.
Px_font (ppx, _ T ("fontup"), m_fontup, & _ fontdesccaption );
Px_font (ppx, _ T ("fontmiddle"), m_fontmiddle, & _ fontdesccaption );
Px_font (ppx, _ T ("fontdown"), m_fontdown, & _ fontdesccaption );
Px_long (ppx, _ T ("xup"), m_xup, 0 );
Px_long (ppx, _ T ("Yup"), m_yup, 0 );
Px_long (ppx, _ T ("xmiddle"), m_xmiddle, 0 );
Px_long (ppx, _ T ("ymiddle"), m_ymiddle, 20 );
Px_long (ppx, _ T ("xdown"), m_xdown, 0 );
Px_long (ppx, _ T ("ydown"), m_ydown, 30 );
Px_string (ppx, _ T ("captionup"), m_captionup, _ T ("superscript "));
Px_string (ppx, _ T ("captionmiddle"), m_captionmiddle, _ T ("intermediate "));
Px_string (ppx, _ T ("captiondown"), m_captiondown, _ T ("subscript "));
}

Complete the member functions of the control.

void CLabelEx0Ctrl::OnCaptionUpChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnCaptionMiddleChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnCaptionDownChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnXUpChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnYUpChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnXMiddleChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnYMiddleChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnXDownChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
void CLabelEx0Ctrl::OnYDownChanged()
{
   InvalidateControl();
   SetModifiedFlag();
}
LPFONTDISP CLabelEx0Ctrl::GetFontUp()
{
   return m_fontUp.GetFontDispatch();
}
void CLabelEx0Ctrl::SetFontUp(LPFONTDISP newValue)
{
   m_fontUp.InitializeFont(&_fontdescCaption,newValue);
   OnFontChanged();
   SetModifiedFlag();
}
LPFONTDISP CLabelEx0Ctrl::GetFontMiddle()
{
   return m_fontMiddle.GetFontDispatch();
}
void CLabelEx0Ctrl::SetFontMiddle(LPFONTDISP newValue)
{
   m_fontMiddle.InitializeFont(&_fontdescCaption,newValue);
   OnFontChanged();
   SetModifiedFlag();
}
LPFONTDISP CLabelEx0Ctrl::GetFontDown()
{
   return m_fontDown.GetFontDispatch();
}
void CLabelEx0Ctrl::SetFontDown(LPFONTDISP newValue)
{
   m_fontDown.InitializeFont(&_fontdescCaption,newValue);
   OnFontChanged();
   SetModifiedFlag();
}

Modify the ondraw function. The modified ondraw function is as follows:

void CLabelExCtrl::OnDraw(
       CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
   // TODO: Replace the following code with your own drawing code.
   COLORREF colorBack=TranslateColor(GetBackColor());
   COLORREF colorFore=TranslateColor(GetForeColor());
   CBrush brush(colorBack);
   pdc->FillRect(rcBounds, &brush);
   pdc->SetBkMode(TRANSPARENT);
   pdc->SetTextColor(colorFore);
   CFont* pOldFont;
   RECT rect;
   ::CopyRect(&rect,rcBounds);
   pOldFont = SelectFontObject(pdc, m_fontUp);
   pdc->TextOut(m_xUp,m_yUp,m_captionUp);
   SelectFontObject(pdc, m_fontMiddle);
   pdc->TextOut(m_xMiddle,m_yMiddle,m_captionMiddle);
   SelectFontObject(pdc, m_fontDown);
   pdc->TextOut(m_xDown,m_yDown,m_captionDown);
   pdc->SelectObject(pOldFont);
}

The labelex0 control has been created. You can test it and use it to create tags in the shape of "m3/h.

Part 4: optimize the code and properly use multiple Fonts

In the above example, the three font objects are implemented using the same ifontnotification, and the notifications of the control font objects cannot be distinguished. To determine which font has been changed, use the following methods:

Create an ifontnotification interface for each font object of the control. This technology allows you to optimize code rendering by updating one or more strings using the recently modified font. The following describes how to modify the fontup of the fontup routine 3 labelex0 to demonstrate how to implement a separate notification interface for the second font attribute.

Implement a new font notification Interface

To differentiate notifications in two or more fonts, you must implement a new notification interface for each font used in the control. The following sections describe how to implement a new font notification interface by modifying the control header file and implementing the file.

Content added to the header file

In the control header file (clabelex0ctrl. h), add the following code:

Declare_interface_map () // declare interface ing
Begin_interface_part (fontupnotify, ipropertypolicysink) // declare the nested class of ipropertypolicysink.
Init_interface_part (clabelex0ctrl, fontupnotify)
Stdmethod (onrequestedit) (dispid );
Stdmethod (onchanged) (dispid );
End_interface_part (fontupnotify)

In this way, clabelex0ctrl has a nested class xfontupnotify and its variable m_xfontupnotify. Add the implementation of this nested class to the implementation file of clabelex0ctrl:

// The ing interface ipropertypolicysink to the corresponding nested class
Begin_interface_map (clabelex0ctrl, colecontrol)
Interface_part (clabelex0ctrl, iid_ipropertypolicysink, fontupnotify)
End_interface_map ()
Stdmethodimp _ (ulong) clabelex0ctrl: xfontupnotify: addref ()
{
Method_prologue_ex (clabelex0ctrl, fontupnotify)
Return (ulong) pthis-> externaladdref ();
}
Stdmethodimp _ (ulong) clabelex0ctrl: xfontupnotify: release ()
{
Method_prologue_ex (clabelex0ctrl, fontupnotify)
Return (ulong) pthis-> externalrelease ();
}
Stdmethodimp clabelex0ctrl: xfontupnotify: QueryInterface (refiid IID, lpvoid far * ppvobj)
{
Method_prologue_ex (clabelex0ctrl, fontupnotify)
Return (hresult) pthis-> externalqueryinterface (& IID, ppvobj );
}
Stdmethodimp clabelex0ctrl: xfontupnotify: onchanged (dispid)
{
Method_prologue_ex (clabelex0ctrl, fontupnotify)
Pthis-> invalidatecontrol ();
Return noerror;
}
Stdmethodimp clabelex0ctrl: xfontupnotify: onrequestedit (dispid)
{
Return noerror;
}

In the constructor code of the clabelex0ctrl class, change m_fontup (& m_xfontnotification) to m_fontup (& m_xfontupup Y ). After these changes are made to the project, the project is regenerated and the test container test interface is used.

Example 4: labelex. ocx

The control of Example 4 is named labelex. ocx. It is optimized by the code of Example 3 and implements the same functions as example 3. Open visual C ++ 6.0, create a labelex project, accept all default settings, and complete the labelex Project Creation. Complete all operations that are exactly the same as that of routine 3, and then make the following changes and supplements:

Add the following code to the control header file (clabelexctrl. h ):

Declare_interface_map () // declare interface ing
Begin_interface_part (fontupnotify, ipropertypolicysink)
Init_interface_part (clabelexctrl, fontup1_y)
Stdmethod (onrequestedit) (dispid );
Stdmethod (onchanged) (dispid );
End_interface_part (fontupnotify)
Begin_interface_part (fontmiddlenotify, ipropertypolicysink)
Init_interface_part (clabelexctrl, fontmiddlenotify)
Stdmethod (onrequestedit) (dispid );
Stdmethod (onchanged) (dispid );
End_interface_part (fontmiddlenotify)
Begin_interface_part (fontdownnotify, ipropertypolicysink)
Init_interface_part (clabelexctrl, fontdown1_y)
Stdmethod (onrequestedit) (dispid );
Stdmethod (onchanged) (dispid );
End_interface_part (fontdownnotify)
Add the implementation of this nested class to the implementation file of clabelexctrl
Begin_interface_map (clabelexctrl, colecontrol)
Interface_part (clabelexctrl, iid_ipropertypolicysink, fontupnotify)
Interface_part (clabelexctrl, iid_ipropertypolicysink, fontmiddlenosink)
Interface_part (clabelexctrl, iid_ipropertypolicysink, fontdownnotify)
End_interface_map ()
Stdmethodimp _ (ulong) clabelexctrl: xfontupnotify: addref ()
{
Method_prologue_ex (clabelexctrl, fontupnotify)
Return (ulong) pthis-> externaladdref ();
}
Stdmethodimp _ (ulong) clabelexctrl: xfontupnotify: release ()
{
Method_prologue_ex (clabelexctrl, fontupnotify)
Return (ulong) pthis-> externalrelease ();
}
Stdmethodimp clabelexctrl: xfontupnotify: QueryInterface (refiid IID, lpvoid far * ppvobj)
{
Method_prologue_ex (clabelexctrl, fontupnotify)
Return (hresult) pthis-> externalqueryinterface (& IID, ppvobj );
}
Stdmethodimp clabelexctrl: xfontupnotify: onchanged (dispid)
{
Method_prologue_ex (clabelexctrl, fontupnotify)
Pthis-> invalidatecontrol ();
Return noerror;
}
Stdmethodimp clabelexctrl: xfontupnotify: onrequestedit (dispid)
{
Return noerror;
}
Stdmethodimp _ (ulong) clabelexctrl: xfontmiddlenovel: addref ()
{
Method_prologue_ex (clabelexctrl, fontmiddlenotify)
Return (ulong) pthis-> externaladdref ();
}
Stdmethodimp _ (ulong) clabelexctrl: xfontmiddlenovel: release ()
{
Method_prologue_ex (clabelexctrl, fontmiddlenotify)
Return (ulong) pthis-> externalrelease ();
}
Stdmethodimp clabelexctrl: xfontmiddlenovel: QueryInterface (refiid IID, lpvoid far * ppvobj)
{
Method_prologue_ex (clabelexctrl, fontmiddlenotify)
Return (hresult) pthis-> externalqueryinterface (& IID, ppvobj );
}
Stdmethodimp clabelexctrl: xfontmiddlenovel: onchanged (dispid)
{
Method_prologue_ex (clabelexctrl, fontmiddlenotify)
Pthis-> invalidatecontrol ();
Return noerror;
}
Stdmethodimp clabelexctrl: xfontmiddlenovel: onrequestedit (dispid)
{
Return noerror;
}
Stdmethodimp _ (ulong) clabelexctrl: xfontdownnotify: addref ()
{
Method_prologue_ex (clabelexctrl, fontdown1_y)
Return (ulong) pthis-> externaladdref ();
}
Stdmethodimp _ (ulong) clabelexctrl: xfontdownnotify: release ()
{
Method_prologue_ex (clabelexctrl, fontdown1_y)
Return (ulong) pthis-> externalrelease ();
}
Stdmethodimp clabelexctrl: xfontdownnotify: QueryInterface (refiid IID, lpvoid far * ppvobj)
{
Method_prologue_ex (clabelexctrl, fontdown1_y)
Return (hresult) pthis-> externalqueryinterface (& IID, ppvobj );
}
Stdmethodimp clabelexctrl: xfontdownnotify: onchanged (dispid)
{
Method_prologue_ex (clabelexctrl, fontdown1_y)
Pthis-> invalidatecontrol ();
Return noerror;
}
Stdmethodimp clabelexctrl: xfontdownnotify: onrequestedit (dispid)
{
Return noerror;
}
The code of the constructor of the clabelexctrl class is modified as follows:CLabelExCtrl::CLabelExCtrl():m_fontUp(&m_xFontUpNotify),
m_fontMiddle(&m_xFontMiddleNotify),m_fontDown(&m_xFontDownNotify)
{
   InitializeIIDs(&IID_DLabelEx, &IID_DLabelExEvents);
   // TODO: Initialize your control''s instance data here.
}
 

 

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.