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

时间:2016-10-12 10:10:24

标签: c# winforms

我正在使用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;
}
}

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

2 个答案:

答案 0 :(得分:1)

如果您使用的是WinForms,可以将WindowState设置为最大化FormWindowState

this.WindowState = FormWindowState.Maximized;

对于WPF用户WindowsState最大化

this.WindowState = WindowState.Maximized;

答案 1 :(得分:0)

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

1)只需将Designer中的 WindowState 属性更改为最大化

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