【问题标题】:Why does C# Winforms GUI font get italicized on different desktop machines?为什么 C# Winforms GUI 字体在不同的桌面机器上会变成斜体?
【发布时间】:2019-07-11 22:30:06
【问题描述】:

我发布了一个 C# winforms GUI,一切在我的机器上看起来都符合预期。我去另一台机器上安装,我的所有文字都变成了斜体。

两台机器都运行 Windows 10 并具有相同的屏幕分辨率设置。我还在第三台机器上安装了我的 GUI,一切正常。

我必须在 Visual Studio 中进行一些设置以使所有机器上的字体看起来都相同吗?或者我需要添加特定的代码吗?

Here is a snippet of what the GUI should look like (no italics)

GUI on machine #2 (font gets italicized)

【问题讨论】:

  • 离题,但仍然。据我所知,您显然正在尝试进行一些设计动作。那么为什么不使用 WPF 呢? winforms 现在已经过时了,afaik。
  • 顺便说一句...我使用的是 Century Gothic 字体。没什么疯狂的......所以我不需要在每台机器上安装新字体。
  • @AgentFire GUI 是在 WinForms 中构建的,不想在 WPF 中重写整个东西......
  • 如果您使用标准字体,如Segoe UI,会发生什么?
  • 打开字体设置,看看你能不能在那台有问题的电脑上找到非斜体版本

标签: c# winforms user-interface


【解决方案1】:

好的,这样你就可以结束这个问题了:

检查字体设置并确保安装了所需的字体。

那里。

【讨论】:

    【解决方案2】:

    好吧,在基于之前收到的 cmets 进行了大量 SO 和 Google 之后,我能够将字体嵌入到我的代码中。现在,在启动时,我的应用程序会检查机器上是否安装了字体。如果没有安装字体,我的应用会安装它。

    这种方式无需手动检查字体是否安装在将运行应用程序的每台机器上。

    注意:需要管理员权限才能工作。

    首先:将字体文件作为资源嵌入

    1. 双击 Resources.resx,然后在设计器的工具栏中单击添加资源/添加现有文件并选择您的 .ttf 文件
    2. 在解决方案资源管理器中,右键单击您的 .ttf 文件并转到“属性”。将“构建操作”设置为“内容”,将“复制到输出目录”属性设置为“始终复制”

    第二:添加这段代码

    using Microsoft.Win32;                  
    using System;                           
    using System.Drawing.Text;              
    using System.IO;                        
    using System.Runtime.InteropServices;   
    
    
    namespace TestAutomation
    {
        public partial class SplashScreen : Form
        {
            [DllImport("gdi32.dll", EntryPoint = "AddFontResource")]
            public static extern int AddFontResource(string lpFileName);
            [DllImport("gdi32.dll")]
            private static extern int CreateScalableFontResource(uint fdwHidden, string
            lpszFontRes, string lpszFontFile, string lpszCurrentPath);
    
            // <summary>
            // Installs font on the user's system and adds it to the registry so it's available on the next session
            // Your font must be embedded as a resource in your project with its 'Build Action' property set to 'Content' 
            // and its 'Copy To Output Directory' property set to 'Copy Always'
            // </summary>
            private void RegisterFont(string contentFontName)
            {
                DirectoryInfo dirWindowsFolder = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System));
    
                // Concatenate Fonts folder onto Windows folder.
                string strFontsFolder = Path.Combine(dirWindowsFolder.FullName, "Fonts");
    
                // Creates the full path where your font will be installed
                var fontDestination = Path.Combine(strFontsFolder, contentFontName);
    
                // Check if file exists in destination folder. If not, then copy the file from project directory to destination
                if (!File.Exists(fontDestination))
                {
                    try
                    {
                        // Copies font to destination
                        File.Copy(Path.Combine(Directory.GetCurrentDirectory(), contentFontName), fontDestination);
    
                        // Retrieves font name
                        PrivateFontCollection fontCol = new PrivateFontCollection();
                        fontCol.AddFontFile(fontDestination);
                        var actualFontName = fontCol.Families[0].Name;
    
                        // Add font
                        AddFontResource(fontDestination);
                        // Add registry entry   
                        Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                            actualFontName, contentFontName, RegistryValueKind.String);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message + "\n\nThe required font(s) have not been installed."
                            + "\n\nPlease contact your systems administrator for help.");
                        Start();
                    }
    
                }
    
                // If file exists in destination folder, then start program.
                else
                {
                    Start();
                }
    
            }
    
            public SplashScreen()
            {
                RegisterFont("GOTHIC.TTF");
            }
    
            private void Start()
            {
                InitializeComponent();
            }
        }
    }
    

    我修改了代码以满足我的需要。以下是我找到我的信息的链接:

    How to quickly and easily embed fonts in winforms app in C#

    https://csharp.hotexamples.com/examples/System.Drawing.Text/PrivateFontCollection/AddFontFile/php-privatefontcollection-addfontfile-method-examples.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 2023-03-30
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多