以编程方式读取 Windows 的区域设置

发布于 2024-11-15 02:41:18 字数 124 浏览 2 评论 0 原文

我需要从 C# Winforms 应用程序了解底层操作系统的当前语言环境/文化的默认页面大小(例如 A4 或 Letter)。

我在 MSDN 上看到过一个页面对此进行了解释,但后来我失去了链接。我怎样才能做到这一点?

I need to know the default page size (such as A4 or Letter) of the current locale/culture of the underlying O/S from a C# Winforms application.

I have seen a page from MSDN explaining this, but I've since lost the link. How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦明 2024-11-22 02:41:18
new PrinterSettings().DefaultPageSettings.PaperSize;
new PrinterSettings().DefaultPageSettings.PaperSize;
讽刺将军 2024-11-22 02:41:18

请参阅:

使用 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 还有许多其他属性可供您使用。

See this:

using 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;

    }

There are many other properties of ps available which you can use.

不如归去 2024-11-22 02:41:18

对于懒惰的人来说,这是@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");
    }
}

参考文献:

For the lazy, here's the code that @logeeks' answer would use:

[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");
    }
}

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文