【问题标题】:Remove borders winforms and WindowState Maximized without fullscreen删除边框 winforms 和 WindowState Maximized 无全屏
【发布时间】:2015-08-17 07:23:54
【问题描述】:

我有以下问题,我没有找到解决办法。

我想实现一个没有顶栏的 Winform,如果可能的话,没有边框。我尝试了几件事但没有成功,以下将完美解决问题:

        this.Text = string.Empty;
        this.ControlBox = false;
        this.FormBorderStyle = FormBorderStyle.SizableToolWindow;

产生以下结果:

小问题是当我或用户触发最大化状态时,因为会使表单进入全屏模式!我不知道如何防止这种情况:

看到了吗?你看不到windows任务栏!我正在使用

WindowState = FormWindowState.Maximized; // Makes a fullscreen that i dont want !

感谢您的帮助!

【问题讨论】:

  • this.FormBorderStyle = FormBorderStyle.None; 呢?
  • this.FormBorderStyle = FormBorderStyle.None; //当我触发最大化状态时,仍然以全屏模式显示:\
  • 是否有 Windows 设置让任务栏始终位于顶部。没有它设置最大化窗口将始终使用桌面工作区(包括任务栏)。

标签: c# winforms


【解决方案1】:

好吧!感谢您的所有回答,我终于用以下两种方法解决了

    private void MaximizeWindow() 
    {
        var rectangle = Screen.FromControl(this).Bounds;
        this.FormBorderStyle = FormBorderStyle.None;
        Size = new Size(rectangle.Width, rectangle.Height);
        Location = new Point(0, 0);
        Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
        this.Size = new Size(workingRectangle.Width, workingRectangle.Height);
    }

    private void ResizableWindow() 
    {
        this.ControlBox = false;
        this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
    }

感谢 X-TECH 和 Luïs,问题解决了!

【讨论】:

    【解决方案2】:

    试试这个动态设置大小,它可能对你有帮助。

    不要使用WindowState = FormWindowState.Maximized; 在加载表单时尝试此代码

    var rectangle = ScreenRectangle();
    Size = new Size(rectangle.Width - 100, rectangle.Height - 100);
    Location = new Point(50, 50);
    // here 100 is pixel used to reserve from edges, 
    // you can set lower value according to your requirements
    

    全尺寸

    var rectangle = ScreenRectangle();
    Size = new Size(rectangle.Width, rectangle.Height);
    Location = new Point(0, 0);
    

    屏幕矩形方法是:

    public Rectangle ScreenRectangle()
    {
        return Screen.FromControl(this).Bounds;
    }
    

    【讨论】:

    • 感谢您的回答,它帮助我更多地了解我在做什么;),我给出了解决这个问题的最终答案,但我会接受您的回答!
    【解决方案3】:

    如果您想控制 表单最大化,请这样做:

      public class MyForm {
        protected override void WndProc(ref Message m) {
          const int WM_SYSCOMMAND = 0x0112;
          const int SC_MAXIMIZE = 0xF030;
    
          // When form is going to be maximized    
          if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MAXIMIZE)) {
            m.Msg = 0; // <- we don't want a standard maximization
            //TODO: tell Windows here what to do when user want to maximize the form
    
            // Just a sample (pseudo maximization)
            Location = new Point(0, 0);
            Size = new Size(Screen.GetWorkingArea(this).Width,
                            Screen.GetWorkingArea(this).Height);
          }
    
          base.WndProc(ref m);
        } 
        ...
      }
    

    【讨论】:

    • 这个对我不起作用,它永远不会通过那个 If-then
    【解决方案4】:

    去掉边框

    this.FormBorderStyle = FormBorderStyle.None;
    

    我认为这可以通过单击最大化按钮来工作

    // Add following namespaces
    using System.Windows.Forms;
    using System.Drawing;
    
    // Retrieve the working rectangle from the Screen class using the PrimaryScreen and the 
    // WorkingArea properties.
    Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
    
    // Set the size of the form slightly less than size of working rectangle. 
    this.Size = new Size(workingRectangle.Width, workingRectangle.Height);
    

    来源:MSDN system.windows.forms.screen.primaryscreenRemove the title bar in Windows Forms

    【讨论】:

      【解决方案5】:

      您好,您只需通过 2 条说明即可做到这一点: this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size; this.WindowState = FormWindowState.Maximized;

      【讨论】:

      • 您的回答只会使窗口处于最大化状态,这不是问题所在。
      【解决方案6】:
      this.Text = string.Empty;
      this.ControlBox = false;
      this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
      
      this.Left = this.Top = 0;
      this.Height = Screen.GetWorkingArea(this).Height;
      this.Width = Screen.GetWorkingArea(this).Width;
      

      GetWorkingArea:获取显示的工作区域。工作区域是显示器的桌面区域,不包括任务栏、停靠的窗口和停靠的工具栏。MSDN

      【讨论】:

      • @Luïs,尝试运行代码,完美运行,为什么你认为这行不通?
      • 好的,我已经测试过了,它可以工作。我不知道。对不起我的错误。
      猜你喜欢
      • 1970-01-01
      • 2014-07-23
      • 2011-06-20
      • 1970-01-01
      • 2011-02-05
      • 2023-03-07
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      相关资源
      最近更新 更多