From ef02abc6d9c03b54ecc3141f260909c97ffad6d7 Mon Sep 17 00:00:00 2001 From: Olabamiji Oyetubo <60369677+bigboybamo@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:09:13 +0100 Subject: [PATCH] Update Class1.cs --- .../System.Drawing.FontsAndText/CS/Class1.cs | 309 +++++++++--------- 1 file changed, 155 insertions(+), 154 deletions(-) diff --git a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs index 1e0bae9110..477c39ff5a 100644 --- a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs +++ b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs @@ -131,163 +131,164 @@ public void Method41(PaintEventArgs e) public void Method51(PaintEventArgs e) { // - PointF pointF = new PointF(10, 0); - SolidBrush solidBrush = new SolidBrush(Color.Black); - - int count = 0; - string familyName = ""; - string familyNameAndStyle; - FontFamily[] fontFamilies; - PrivateFontCollection privateFontCollection = new PrivateFontCollection(); - - // Add three font files to the private collection. - privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\Arial.ttf"); - privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\CourBI.ttf"); - privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\TimesBD.ttf"); + PointF pointF = new PointF(10, 0); + SolidBrush solidBrush = new SolidBrush(Color.Black); - // Get the array of FontFamily objects. - fontFamilies = privateFontCollection.Families; - - // How many objects in the fontFamilies array? - count = fontFamilies.Length; + int count = 0; + string familyName = ""; + string familyNameAndStyle; + FontFamily[] fontFamilies; - // Display the name of each font family in the private collection - // along with the available styles for that font family. - for (int j = 0; j < count; ++j) - { - // Get the font family name. - familyName = fontFamilies[j].Name; - - // Is the regular style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular)) - { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + " Regular"; - - Font regFont = new Font( - familyName, - 16, - FontStyle.Regular, - GraphicsUnit.Pixel); - - e.Graphics.DrawString( - familyNameAndStyle, - regFont, - solidBrush, - pointF); - - pointF.Y += regFont.Height; - } - - // Is the bold style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold)) - { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + " Bold"; - - Font boldFont = new Font( - familyName, - 16, - FontStyle.Bold, - GraphicsUnit.Pixel); - - e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF); - - pointF.Y += boldFont.Height; - } - // Is the italic style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic)) - { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + " Italic"; - - Font italicFont = new Font( - familyName, - 16, - FontStyle.Italic, - GraphicsUnit.Pixel); - - e.Graphics.DrawString( - familyNameAndStyle, - italicFont, - solidBrush, - pointF); - - pointF.Y += italicFont.Height; - } - - // Is the bold italic style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) && - fontFamilies[j].IsStyleAvailable(FontStyle.Bold)) - { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + "BoldItalic"; - - Font italicFont = new Font( - familyName, - 16, - FontStyle.Italic | FontStyle.Bold, - GraphicsUnit.Pixel); - - e.Graphics.DrawString( - familyNameAndStyle, - italicFont, - solidBrush, - pointF); - - pointF.Y += italicFont.Height; - } - // Is the underline style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline)) - { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + " Underline"; - - Font underlineFont = new Font( - familyName, - 16, - FontStyle.Underline, - GraphicsUnit.Pixel); - - e.Graphics.DrawString( - familyNameAndStyle, - underlineFont, - solidBrush, - pointF); - - pointF.Y += underlineFont.Height; - } - - // Is the strikeout style available? - if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout)) + using (var privateFontCollection = new PrivateFontCollection) { - familyNameAndStyle = ""; - familyNameAndStyle = familyNameAndStyle + familyName; - familyNameAndStyle = familyNameAndStyle + " Strikeout"; - - Font strikeFont = new Font( - familyName, - 16, - FontStyle.Strikeout, - GraphicsUnit.Pixel); - - e.Graphics.DrawString( - familyNameAndStyle, - strikeFont, - solidBrush, - pointF); - - pointF.Y += strikeFont.Height; - } - - // Separate the families with white space. - pointF.Y += 10; - } // for + // Add three font files to the private collection. + privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\Arial.ttf"); + privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\CourBI.ttf"); + privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\TimesBD.ttf"); + + // Get the array of FontFamily objects. + fontFamilies = privateFontCollection.Families; + + // How many objects in the fontFamilies array? + count = fontFamilies.Length; + + // Display the name of each font family in the private collection + // along with the available styles for that font family. + for (int j = 0; j < count; ++j) + { + // Get the font family name. + familyName = fontFamilies[j].Name; + + // Is the regular style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + " Regular"; + + Font regFont = new Font( + familyName, + 16, + FontStyle.Regular, + GraphicsUnit.Pixel); + + e.Graphics.DrawString( + familyNameAndStyle, + regFont, + solidBrush, + pointF); + + pointF.Y += regFont.Height; + } + + // Is the bold style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + " Bold"; + + Font boldFont = new Font( + familyName, + 16, + FontStyle.Bold, + GraphicsUnit.Pixel); + + e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF); + + pointF.Y += boldFont.Height; + } + // Is the italic style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + " Italic"; + + Font italicFont = new Font( + familyName, + 16, + FontStyle.Italic, + GraphicsUnit.Pixel); + + e.Graphics.DrawString( + familyNameAndStyle, + italicFont, + solidBrush, + pointF); + + pointF.Y += italicFont.Height; + } + + // Is the bold italic style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) && + fontFamilies[j].IsStyleAvailable(FontStyle.Bold)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + "BoldItalic"; + + Font italicFont = new Font( + familyName, + 16, + FontStyle.Italic | FontStyle.Bold, + GraphicsUnit.Pixel); + + e.Graphics.DrawString( + familyNameAndStyle, + italicFont, + solidBrush, + pointF); + + pointF.Y += italicFont.Height; + } + // Is the underline style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + " Underline"; + + Font underlineFont = new Font( + familyName, + 16, + FontStyle.Underline, + GraphicsUnit.Pixel); + + e.Graphics.DrawString( + familyNameAndStyle, + underlineFont, + solidBrush, + pointF); + + pointF.Y += underlineFont.Height; + } + + // Is the strikeout style available? + if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout)) + { + familyNameAndStyle = ""; + familyNameAndStyle = familyNameAndStyle + familyName; + familyNameAndStyle = familyNameAndStyle + " Strikeout"; + + Font strikeFont = new Font( + familyName, + 16, + FontStyle.Strikeout, + GraphicsUnit.Pixel); + + e.Graphics.DrawString( + familyNameAndStyle, + strikeFont, + solidBrush, + pointF); + + pointF.Y += strikeFont.Height; + } + + // Separate the families with white space. + pointF.Y += 10; + } // for // } // d3a4a223-9492-4b54-9afd-db1c31c3cefd