【问题标题】:Read locale settings of windows programmatically以编程方式读取 Windows 的区域设置
【发布时间】:2011-06-14 09:22:25
【问题描述】:

我需要知道来自 C# Winforms 应用程序的底层操作系统的当前区域设置/区域性的默认页面大小(例如 A4 或 Letter)。

我在 MSDN 上看到了一个解释这一点的页面,但我已经失去了链接。我怎样才能做到这一点?

【问题讨论】:

  • 我不认为默认纸张大小是系统区域设置的一部分。但是,MS Office 有这样的设置。
  • 我在某处看到了一个名为 ILocale.PageSize 之类的常量。我正在寻找它,

标签: c# .net culture


【解决方案1】:

【讨论】:

    【解决方案2】:
    new PrinterSettings().DefaultPageSettings.PaperSize;
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        看这个:

        使用 System.Drawing.Printing;

            private void button1_Click(object sender, EventArgs e)
            {
        
                PrintDocument doc = new PrintDocument();
                PageSettings ps = doc.DefaultPageSettings;
        
                if (ps.Landscape)
                    label1.Text = "LANDSCAPE";
                PaperSize paperSize = ps.PaperSize;
        
            }
        

        还有许多其他可用的 ps 属性可供您使用。

        【讨论】:

          【解决方案5】:

          对于懒惰的人,这是@logeeks 的答案将使用的代码:

          [DllImport("kernel32.dll", SetLastError = true)]
          static extern int GetLocaleInfo(
             uint Locale,
             uint LCType,
             [Out] StringBuilder lpLCData,
             int cchData);
          
          public enum LCType : uint
          {
              LOCALE_IPAPERSIZE = 0x0000100A,   // 1 = letter, 5 = legal, 8 = a3, 9 = a4
          }
          
          void Main()
          {
              //CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
              CultureInfo culture = CultureInfo.GetCultureInfo("de-DE"); ;
          
              var output = new StringBuilder();
          
              int result = GetLocaleInfo((uint)(culture.LCID), (uint)LCType.LOCALE_IPAPERSIZE, output, 99);
          
              if (result > 0)
              {
                  // 1 = letter, 5 = legal, 8 = a3, 9 = a4
                  Console.WriteLine(output.ToString());
              }
              else
              {
                  Console.WriteLine("fail");
              }
          }
          

          参考资料:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-03-14
            • 2016-01-17
            • 2018-03-16
            • 1970-01-01
            • 2019-03-22
            • 2022-10-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多