• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

解决winform自定义窗体在扩展显示器最大化不能充满屏幕

武飞扬头像
我想找个厂上班
帮助1

原因:

我开发用笔记本,缩放设置为150%;同时外接一个扩展屏,缩放设置为125%。

winform自定义窗体,FormBorderStyle = None,原本的窗体最大化代码:

  1.  
    private void labelMaximized_Click(object sender, EventArgs e)
  2.  
    {
  3.  
    if (this.WindowState == FormWindowState.Maximized)
  4.  
    {
  5.  
    this.WindowState = FormWindowState.Normal;
  6.  
    }
  7.  
    else
  8.  
    {
  9.  
    this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
  10.  
    this.WindowState = FormWindowState.Maximized;
  11.  
    }
  12.  
    }

 两个显示器缩放不一致时,在副屏无法正常最大化窗口。

解决:

舍弃微软提供的方法,自定义最大化时窗口的大小和显示位置。

代码(参考开头的博客):

  1.  
    // 窗体显示状态
  2.  
    private FormWindowState formWindowState = FormWindowState.Normal;
  3.  
    // 最大化前窗体大小
  4.  
    private Size formSizeNormal;
  5.  
    private void labelMaximized_Click(object sender, EventArgs e)
  6.  
    {
  7.  
    if (this.WindowState == FormWindowState.Maximized || this.formWindowState == FormWindowState.Maximized)
  8.  
    {
  9.  
    Screen currentSreen = Screen.FromControl(this);
  10.  
    this.MaximizedBounds = currentSreen.WorkingArea;
  11.  
    // 位置
  12.  
    int x = currentSreen.Bounds.Left (currentSreen.Bounds.Width - formSizeNormal.Width) / 2;
  13.  
    int y = (currentSreen.Bounds.Height - formSizeNormal.Height) / 2;
  14.  
    this.Location = new Point(x, y);
  15.  
    // 大小
  16.  
    this.Size = formSizeNormal;
  17.  
    this.formWindowState = FormWindowState.Normal;
  18.  
    }
  19.  
    else
  20.  
    {
  21.  
    formSizeNormal = this.Size;
  22.  
    Screen currentScreen = Screen.FromControl(this);
  23.  
    this.MaximizedBounds = currentScreen.WorkingArea;
  24.  
    this.Location = new Point(currentScreen.WorkingArea.Left, currentScreen.WorkingArea.Top);
  25.  
    this.Size = currentScreen.WorkingArea.Size;
  26.  
    this.formWindowState = FormWindowState.Maximized;
  27.  
    }
  28.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhachkg
系列文章
更多 icon
同类精品
更多 icon
继续加载