开发者

Replacing obsolete VisualBasic.Compatibility.VB6.Support

we have recently upgraded an old VB6 windows app to C# .NET 4.0. I am looking to replace references to the Microsoft.VisualBasic.Compatibility.VB6.Support class, as Visual Basic 2010 is warning me that 'Microsoft.VisualBasic.Compatibility.* classes are obsolete and supported within 32 bit processes only. http://go.microsoft.com/fwlink/?linkid=160862'

This article assures me that: 'Functions in the Compatibility namespaces were created to work around shortcomings in version 1.0 of the .NET Framework. In most cases, functionality added in later framework versions can be used t开发者_JS百科o rewrite the functions, resulting in improved performance.'

My question is, what are the additions to later framework versions that I need to use to do away with the Compatibility.* classes? I need to phase out TwipsToPixelX, TwipsToPixelY, and so forth. Also, FontChangeUnderline, FontChangeSize, and other font-related stuff.


Thanks for all the help everyone. Just to follow up, here's what I cooked up in dealing with the twips-to-pixels conversions.

    private const float TWIPS_PER_INCH = 1440f;
    private static Form _form = new Form();
    private static Graphics _graphics = _form.CreateGraphics();

    public static float TwipsPerPixelX()
    {
        return TWIPS_PER_INCH/_graphics.DpiX;
    }

    public static double TwipsToPixelsY(double twips)
    {
        float dpiy = _graphics.DpiY;
        return twips * dpiy / TWIPS_PER_INCH;
    }

    public static double TwipsToPixelsX(double twips)
    {
        float dpix = _graphics.DpiX;
        return twips * dpix / TWIPS_PER_INCH;
    }

    public static double PixelsToTwipsY(double pixels)
    {
        float dpiy = _graphics.DpiY;
        return pixels * TWIPS_PER_INCH / dpiy;
    }

    public static double PixelsToTwipsX(double pixels)
    {
        float dpix = _graphics.DpiX;
        return pixels * TWIPS_PER_INCH / dpix;
    }

Hope that someone finds this interesting and/or useful


The font related functions can be replaced easily enough. For example:

Function FontChangeBold(f As Font, bold As Boolean) As Font
    Dim alreadySet = (f.Style And FontStyle.Bold) = FontStyle.Bold
    If bold = alreadySet Then Return f
    If bold Then Return New Font(f, f.Style Or FontStyle.Bold)
    Return New Font(f, f.Style And Not FontStyle.Bold)
End Function

This checks whether the desired style is already set. If it is, it returns the old font. Otherwise it will return a new font that has the same style, except for the bold style, which is now set according to the requirement.


You can create fonts with different styles by writing new Font(oldFont, FontStyle.Underline) or new Font(oldFont, 12).


Twips are no longer necessary. You can simply use raw pixels for dimensions now.

As for fonts, check out the Font Class.


For the VB guys out there, this works for me, being able to toggle bold, italics, and/or underline.

Private Function SetNewFont(ByRef f As Font, Optional ByVal bToggleBold As Boolean = False, Optional ByVal bToggleItalics As Boolean = False, Optional ByVal bToggleUnderLine As Boolean = False) As Font

    Dim fs As FontStyle

    If bToggleBold = True Then
        If f.Bold = False Then
            fs = FontStyle.Bold
        End If
    Else
        If f.Bold = True Then
            fs = FontStyle.Bold
        End If
    End If

    If bToggleItalics = True Then
        If f.Italic = False Then
            fs += FontStyle.Italic
        End If
    Else
        If f.Italic = True Then
            fs += FontStyle.Italic
        End If
    End If

    If bToggleUnderLine = True Then
        If f.Underline = False Then
            fs += FontStyle.Underline
        End If
    Else
        If f.Underline = True Then
            fs += FontStyle.Underline
        End If
    End If

    Return New Font(f, fs)

End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜