如何在C#中将System.Drawing.FontStyle转换为System.Windows.FontStyle? [英] How to convert System.Drawing.FontStyle to System.Windows.FontStyle in C#?

查看:272
本文介绍了如何在C#中将System.Drawing.FontStyle转换为System.Windows.FontStyle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我有一个wpf应用程序,在db中保存应用程序字体设置并获取并应用应用程序级别的字体。 />


面临以下问题。



如何将System.Drawing.FontStyle转换为System.Windows。 C#中的FontStyle?





谢谢

Hello All,

Am having a wpf application, where am saving the application font settings in db and getting and apply the font for the application level.

there am facing the below problem.

How to convert System.Drawing.FontStyle to System.Windows.FontStyle in C#?


Thanks

推荐答案

他们没有在WPF和Winforms之间有一对一的映射。 Winforms版本是一个枚举,其中包含常规粗体斜体下划线删除线。 WPF版本是一个结构,你可以使用 FontStyles 类的静态成员来获取普通斜体倾斜



重要的问题是为什么你甚至在WPF应用程序中使用 System.Drawing 枚举?也许您需要重新设计保存字体信息的方式。
They don't have a one-to-one mapping between WPF and Winforms. The Winforms version is an enum with regular, bold, italic, underline, and strikeout. The WPF version is a struct, and you can use the FontStyles class's static members for Normal, Italic, and Oblique.

The important question is why do you even use the System.Drawing enum in the WPF application? Perhaps you need to redesign how you save your font information.


简介



有时候在c#中使用常规绘图方法时,您可能希望通过实现某些WPF功能来扩展这些功能。虽然WPF很好,告诉人们简单地切换语言并不是一个可行的解决方案,因为WinForms中的某些东西更好,而WPF中的一些东西更好。 有一种方法可以更自然地收敛两者,这样你就可以利用两种方法中的最佳方法,这不是一个不合理的要求



以下是我打了一堂课让这个转换变得更容易。



将System.Drawing字体转换为System.Windows.Media/WPF等价物

Introduction

There are times when using normal drawing methods in c# where you may want to extend those capabilities by implementing some WPF features. While WPF is good, telling people to simply switch languages is not a viable solution as somethings are better in WinForms, while some things are better in WPF. Having a way to more naturally converge the two so you can harness the best of both methods, is not an unreasonable request.

Following is a class I whipped together to make this conversion easier.

Convert System.Drawing font to System.Windows.Media/WPF equivalent
namespace System.Drawing.WPF {

 class Media {
  public static class Font {
   static Font() {
    DrawingFont = new System.Drawing.Font("Arial", (float) 11.25);
    FontStretch = new System.Windows.FontStretch();
   }
   public static System.Drawing.Font DrawingFont {
    get;
    set;
   }
   public static System.Windows.Media.FontFamily FontFamily {
    get {
     return new System.Windows.Media.FontFamily(DrawingFont.Name);
    }
   }
   public static System.Windows.Media.FontFamily FallBackFontFamily {
    get {
     return new System.Windows.Media.FontFamily("Arial");
    }
   }
   public static System.Windows.FontStyle FontStyle {
    get {
     return DrawingFont.Style == System.Drawing.FontStyle.Italic ? FontStyles.Italic : FontStyles.Normal;
    }
   }
   public static System.Windows.FontWeight FontWeight {
    get {
     return DrawingFont.Style == System.Drawing.FontStyle.Bold ? FontWeights.Bold : FontWeights.Normal;
    }
   }
   public static System.Windows.FontStretch FontStretch {
    get;
    set;
   }
   private static Typeface Typeface {
    get {
     return new Typeface(FontFamily, FontStyle, FontWeight, FontStretch, FallBackFontFamily);
    }
   }
  }
 }

}





WPF兔子洞



如果像我一样,你打算将它用于WPF的 TextFormatting 方法,你应该注意 TextFormatting 所需的大多数类都是抽象的。这意味着你必须为WPF似乎有一个方法的每个漂亮函数手动编写所有代码。 虽然该方法存在,但您需要在这些方法中编写所有代码以使它们按照所声称的方式执行 ,并在此过程中,深入研究进一步需要的兔子洞抽象类。



从表面上看,WPF似乎能够提供的一些东西实际上是没有能力的,因为代码还没有编写。你需要写它。我可能会因为说明这一点而受到抨击或连续投票(即使我上面提供的代码解决方案实际工作),但有人需要让其他人意识到跳上WPF的潮流因为它更新,或似乎有更多的功能,它实际上缺少它声称拥有的一些功能,因为代码不存在实际上会执行所述功能 - 只有抽象类。



这是我在深入研究TextFormatting兔子洞时的部分实现。我停下来,因为我不忍心走得更远。对于我需要的东西,我只需要编写代码就可以比单纯依赖TextFormatting更快 - 除非我为它编写代码,否则它什么都不做。



WPF抽象类梦魇示例



The WPF Rabbit Hole

If, like me, you were intending on using this for WPF's TextFormatting methods, you should beware that most of the classes that TextFormatting requires, are abstract. This means that you have to write all the code by hand anyway for each of those pretty functions that WPF appears to have a method for. While the method exists, you need to write all the code inside those methods to make them do what they claim, and in the process, delve down the rabbit hole of further required abstract classes.

On the surface, some things WPF seems to be capable of, is in fact, not capable as the code has not been written. You need to write it. I will likely get flamed or serial down-voted for stating this (even though the code solution I have provided above ACTUALLY WORKS), however someone needs to make others aware that jumping on the bandwagon that is WPF because it is newer, or appears to have more functionality, it in fact is missing quite a few features that it claims to have due to the code not being present that would actually do said feature -- only abstract classes.

Here is a partial implementation of what I mean when delving down the TextFormatting rabbit hole. I stopped as I couldn't bear to go further. For what I needed, it was faster for me to just write the code for that than depend on TextFormatting -- which does nothing until I write the code for it anyway.

WPF Abstract class Nightmare Example

#region textformatter
    class TextRunProperties : System.Windows.Media.TextFormatting.TextRunProperties {
        public override Windows.Media.Brush BackgroundBrush { get; set; }
        public override Windows.BaselineAlignment BaselineAlignment { get { return base.BaselineAlignment; } }
        public override CultureInfo CultureInfo { get; set; }
        public override double FontHintingEmSize { get; set; }
        public override double FontRenderingEmSize { get; set; }
        public override Windows.Media.Brush ForegroundBrush { get; set; }
        public override Windows.Media.NumberSubstitution NumberSubstitution { get { return base.NumberSubstitution; } }
        public override TextDecorationCollection TextDecorations { get; set; }
        public override Windows.Media.TextEffectCollection TextEffects { get; set; }
        public override Windows.Media.Typeface Typeface { get; set; }
        public override Windows.Media.TextFormatting.TextRunTypographyProperties TypographyProperties { get { return base.TypographyProperties; } }
    }

    class TextRun : System.Windows.Media.TextFormatting.TextRun {
        public override CharacterBufferReference CharacterBufferReference { get; set; }
        public override int Length { get; set; }
        public override Windows.Media.TextFormatting.TextRunProperties Properties { get; set; }
    }

    class TextSource : System.Windows.Media.TextFormatting.TextSource {
        public override Windows.Media.TextFormatting.TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText( int textSourceCharacterIndexLimit ) {
            return new TextSpan<CultureSpecificCharacterBufferRange>(
                textSourceCharacterIndexLimit,
                new CultureSpecificCharacterBufferRange(
                    new CultureInfo(0),
                    Windows.Media.TextFormatting.CharacterBufferRange.Empty
                )
            );
        }

        public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex( int textSourceCharacterIndex ) {
            return 0;
        }

        public override Windows.Media.TextFormatting.TextRun GetTextRun( int textSourceCharacterIndex ) {
            return new TextRun();
        }
    }

    class TextFormatter : System.Windows.Media.TextFormatting.TextFormatter {
        public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties ) {
            throw new NotImplementedException();
        }

        public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties, TextRunCache textRunCache ) {
            throw new NotImplementedException();
        }

    }

    class TextLine : System.Windows.Media.TextFormatting.TextLine {
        public override double Baseline {
            get { throw new NotImplementedException(); }
        }

        public override int DependentLength {
            get { throw new NotImplementedException(); }
        }

        public override double Extent {
            get { throw new NotImplementedException(); }
        }
        public override bool HasCollapsed {
            get { throw new NotImplementedException(); }
        }
        public override bool HasOverflowed {
            get { throw new NotImplementedException(); }
        }

        public override double Height {
            get { throw new NotImplementedException(); }
        }

        public override bool IsTruncated {
            get {
                return base.IsTruncated;
            }
        }

        public override int Length {
            get { throw new NotImplementedException(); }
        }

        public override double MarkerBaseline {
            get { throw new NotImplementedException(); }
        }
        public override double MarkerHeight {
            get { throw new NotImplementedException(); }
        }
        public override int NewlineLength {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangAfter {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangLeading {
            get { throw new NotImplementedException(); }
        }
        public override double OverhangTrailing {
            get { throw new NotImplementedException(); }
        }
        public override double Start {
            get { throw new NotImplementedException(); }
        }
        public override double TextBaseline {
            get { throw new NotImplementedException(); }
        }
        public override double TextHeight {
            get { throw new NotImplementedException(); }
        }
        public override int TrailingWhitespaceLength {
            get { throw new NotImplementedException(); }
        }
        public override double Width {
            get { throw new NotImplementedException(); }
        }
        public override double WidthIncludingTrailingWhitespace {
            get { throw new NotImplementedException(); }
        }

        // functions
        public override Windows.Media.TextFormatting.TextLine Collapse( params TextCollapsingProperties[] collapsingPropertiesList ) {
            throw new NotImplementedException();
        }
        public override void Draw( DrawingContext drawingContext, Windows.Point origin, InvertAxes inversion ) {
            throw new NotImplementedException();
        }
        public override CharacterHit GetBackspaceCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }

        public override CharacterHit GetCharacterHitFromDistance( double distance ) {
            throw new NotImplementedException();
        }
        public override double GetDistanceFromCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override IEnumerable<IndexedGlyphRun> GetIndexedGlyphRuns() {
            throw new NotImplementedException();
        }
        public override CharacterHit GetNextCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override CharacterHit GetPreviousCaretCharacterHit( CharacterHit characterHit ) {
            throw new NotImplementedException();
        }
        public override IList<TextBounds> GetTextBounds( int firstTextSourceCharacterIndex, int textLength ) {
            throw new NotImplementedException();
        }
        public override IList<TextCollapsedRange> GetTextCollapsedRanges() {
            throw new NotImplementedException();
        }
        public override TextLineBreak GetTextLineBreak() {
            throw new NotImplementedException();
        }
        public override IList<TextSpan<Windows.Media.TextFormatting.TextRun>> GetTextRunSpans() {
            throw new NotImplementedException();
        }
    }

#endregion

< br>

查看此链接



http://stackoverflow.com/questions/1297772/how-to-convert-system-drawing-font-to-system-windows-media -fonts-or-typeface [ ^ ]


这篇关于如何在C#中将System.Drawing.FontStyle转换为System.Windows.FontStyle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆