繁体   English   中英

C# 自动适应任何屏幕分辨率

[英]C# auto fit to any screen resolution

我正在使用 MS Visual Studio C# 2010 开发一个小项目。

在我的 MainFormDesigner.cs 文件中,我有以下代码。 它所做的只是从我的服务器加载一个网页。 我需要应用程序来填充 1080 x 1920 的显示。但是当我保存和构建应用程序时,一些尺寸默认为我正在处理的屏幕分辨率。

有没有办法自动调整应用程序的大小以适应应用程序运行的任何屏幕的分辨率。

namespace Impa_Browser
{
partial class MainForm
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.browser = new System.Windows.Forms.WebBrowser();
        this.connectLbl = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // browser
        // 
        this.browser.Location = new System.Drawing.Point(0, 0);
        this.browser.Margin = new System.Windows.Forms.Padding(0);
        this.browser.MinimumSize = new System.Drawing.Size(20, 20);
        this.browser.Name = "browser";
        this.browser.ScrollBarsEnabled = false;
        this.browser.Size = new System.Drawing.Size(1080, 1920); // THIS IS THE RESOLUTION OF THE  DISPLAY THE APP WILL RUN ON
        this.browser.TabIndex = 0;
        this.browser.Url = new System.Uri("example.com", System.UriKind.Absolute);
        this.browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser_DocumentCompleted);
        // 
        // connectLbl
        // 
        this.connectLbl.Dock = System.Windows.Forms.DockStyle.Fill;
        this.connectLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        this.connectLbl.Location = new System.Drawing.Point(0, 0);
        this.connectLbl.Name = "connectLbl";
        this.connectLbl.Size = new System.Drawing.Size(1080, 1092); // THIS KEEPS CHANGING TO THE RESOLUTION OF THE SCREEN I AM WORKING ON
        this.connectLbl.TabIndex = 1;
        this.connectLbl.Text = " Trying to connect ...[20] Please check your Internet router";
        this.connectLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // MainForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1080, 1092); // THIS KEEPS CHANGING TO THE RESOLUTION OF THE SCREEN I AM WORKING ON
        this.Controls.Add(this.browser);
        this.Controls.Add(this.connectLbl);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "MainForm";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Impa";
        this.Load += new System.EventHandler(this.MainForm_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.WebBrowser browser;
    private System.Windows.Forms.Label connectLbl;
}
}

非常感谢您提供的任何帮助。

如果您使用的是 WinForms,您可以像这样将 WindowState 设置为FormWindowState Maximized。

this.WindowState = FormWindowState.Maximized;

对于 WPF 用户WindowsState最大化

this.WindowState = WindowState.Maximized;

有几种解决方案可以实现这一目标:

1) 只需将 Designer 中的WindowState属性更改为Maximized

2)如果您不想更改此属性,您可以在代码中执行此操作 - 例如在您的表单的加载事件中,如下所示:

private void MainForm_Load(object sender, EventArgs e)
{
     this.WindowState = FormWindowState.Maximized;
}

3) 像这样覆盖表单的OnLoad方法:

protected override OnLoad(EventArgs e)
{
     base.OnLoad(e);
     this.WindowState = FormWindowState.Maximized;
}

4) 如果有充分的理由不使用 WindowState,您可以将Screen类对象用于显示表单的屏幕。 例如像这样:

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);

    // get the current screen
    Screen screen = Screen.FromControl(this);

    // set Location and Size of your form to fit in the WorkingArea
    Location = new Point(screen.WorkingArea.Left, screen.WorkingArea.Top);
    Size = new Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM