Windows窗体全屏拖动到屏幕顶部?

时间:2018-02-04 10:32:36

标签: c# windows fullscreen

我做了一个带有关闭和全屏按钮的自定义窗口,工作正常。窗口也是可拖动的。

但是,如果我将窗口拖到屏幕顶部以使其全屏显示,反之亦然,那么我怎样才能实现这一目标。

  public partial class Form1 : Form
{
    public Boolean fullScreenCheck = false;
    public Point mouseLocation;
    public Form1()
    {
        InitializeComponent();
    }


    private const int SnapDist = 100;
    private bool DoSnap(int pos, int edge)
    {
        int delta = pos - edge;
        return delta > 0 && delta <= SnapDist;
    }

    protected override void OnResizeEnd(EventArgs e)
    {
        base.OnResizeEnd(e);
        Screen scn = Screen.FromPoint(this.Location);
        if (DoSnap(this.Left, scn.WorkingArea.Left)) this.Left = scn.WorkingArea.Left;
        if (DoSnap(this.Top, scn.WorkingArea.Top)) this.Top = scn.WorkingArea.Top;
        if (DoSnap(scn.WorkingArea.Right, this.Right)) this.Left = scn.WorkingArea.Right - this.Width;
        if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) this.Top = scn.WorkingArea.Bottom - this.Height;
    }

private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseLocation = new Point(-e.X, -e.Y);
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left)
        {
            Point mousePose = Control.MousePosition;
            mousePose.Offset(mouseLocation.X, mouseLocation.Y);
            Location = mousePose;
        }
    }

    private void button1_MouseClick(object sender, MouseEventArgs e)
    {
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if(fullScreenCheck == false) { 
            WindowState = FormWindowState.Maximized;
            fullScreenCheck = true;
        }
        else
        {
            WindowState = FormWindowState.Normal;
            fullScreenCheck = false;
        }
    }
}

我试图添加这个但仍然没有工作...... :(

2 个答案:

答案 0 :(得分:0)

在Windows 10上,只需创建默认窗体,当您将窗口拖动到屏幕顶部时,它将自动获得全屏。 您想要在哪个Windows版本上实现?

答案 1 :(得分:0)

我想你需要试试这个

public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    private const int SnapDist = 100;
    private bool DoSnap(int pos, int edge) {
      int delta = pos - edge;
      return delta > 0 && delta <= SnapDist;
    }
    protected override void  OnResizeEnd(EventArgs e) {
      base.OnResizeEnd(e);
      Screen scn = Screen.FromPoint(this.Location);
      if (DoSnap(this.Left, scn.WorkingArea.Left)) this.Left= scn.WorkingArea.Left;
      if (DoSnap(this.Top, scn.WorkingArea.Top)) this.Top = scn.WorkingArea.Top;
      if (DoSnap(scn.WorkingArea.Right, this.Right)) this.Left = scn.WorkingArea.Right - this.Width;
      if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) this.Top = scn.WorkingArea.Bottom - this.Height;
    }
  }