Feedback

Font Helper

Sprache: C#

Diese Snippets erleichtern den Umgang mit den bitflags der [b]FontStyle Enumeration.[/b] Ein übergebener Font wird auf die gesetzten flags hin ( bitweise ) überprüft und je nach dem ein neuer Font erzeugt und zurückgegeben. Hilfreich im Umgang mit der Richtextbox etc..
  static public Font BoldFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Bold) == 0)
                {
                    fontStyle |= FontStyle.Bold;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Bold;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }
      
        static public Font ItalicFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Italic) == 0)
                {
                    fontStyle |= FontStyle.Italic;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Italic;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }

        static public Font UnderlineFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Underline) == 0)
                {
                    fontStyle |= FontStyle.Underline ;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Underline ;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }
  static public Font BoldFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Bold) == 0)
                {
                    fontStyle |= FontStyle.Bold;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Bold;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }
      
        static public Font ItalicFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Italic) == 0)
                {
                    fontStyle |= FontStyle.Italic;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Italic;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }

        static public Font UnderlineFont(Font font)
        {
            if (font != null)
            {
                FontStyle fontStyle = font.Style;
                if ((fontStyle & FontStyle.Underline) == 0)
                {
                    fontStyle |= FontStyle.Underline ;
                    font = new Font(font, fontStyle);
                }
                else
                {
                    fontStyle ^= FontStyle.Underline ;
                    font = new Font(font, fontStyle);
                }
            }
            return font;
        }