InteropForms 2.0 Tip #1 – Font Property

VBTeam

(Starting today, we’ll be highlighting various tips that might help you when using the InteropForms 2.0 toolkit.   With the latest release of the toolkit, you can do some fairly impressive upgrades to your existing VB6 applications.  Don’t believe me?  Check this out as one cool example  – WPF in VB6 anyone?!)

 
If you don’t have it, you can download the toolkit here.
  
Adding Font Property:
Here’s a tip on extending Interop Usercontrols that some of you might find useful.   Most usercontrols have a “font” property associated with them which can inherit its values from its container and defines what font is used for the controls that it contains.   If you want to add this support to your control, here’s what you can do.
 
 
1) From your VB6 Interop Usercontrol project, goto the references dialog on your project and add a reference to “stdole”
 
2) Open up your interopusercontrol control and in the “VB6 Properties” region, add this code:
 
Public Shadows Property ControlFont() As stdole.IFontDisp

Get

Dim fntTmp As New Drawing.Font(MyBase.Font.Name, MyBase.Font.Size, MyBase.Font.Style, _

System.Drawing.GraphicsUnit.Point,

CType(0, Byte))

Return ActiveXControlHelpers.GetIFontDispFromFont(fntTmp)

End Get

Set(ByVal value As stdole.IFontDisp)

MyBase.Font = ActiveXControlHelpers.GetFontFromIFontDisp(value)

End Set

End Property

 
3) Open up ActiveXControlHelpers.vb and add this code to the activeXControlHelpers class

Friend Shared Shadows Function GetFontFromIFontDisp(ByVal obj As Object) As Font

Return AxHost.GetFontFromIFontDisp(obj)

End Function

Friend Shared Shadows Function GetFontFromIFont(ByVal obj As Object) As Font

Return AxHost.GetFontFromIFont(obj)

End Function

Friend Shared Shadows Function GetIFontDispFromFont(ByVal font As Font) As stdole.IFontDisp

Return TryCast(AxHost.GetIFontDispFromFont(font), stdole.IFontDisp)

End Function

4) You can then compile your project and reference it from VB6 and set the “controlfont” property appropriately.
 
This property was not included in the InteropForms 2.0 release since we didn’t feel like it was that common of a scenario and it can make deploying the control in the general case a bit trickier.   Specifically, you need to ensure that stdole.dll is deployed in the GAC which you can install by running a setup called vs_piaredist.exe which should be installed on your machine that has VS (typically in a program filescommon filesmerge modules directory).   It’s a silent install, so it’s pretty easy to deploy.   However, you may find that all your clients have this installed and you don’t need to worry about this.

0 comments

Leave a comment

Feedback usabilla icon