C#为什么表单位置重新设置为(0,0)

时间:2017-04-28 13:10:09

标签: c# winforms

每当我将表单移动到最大状态并移动表单时,表单位置将默认为(0,0)。我使用MouseDown,MouseMove和MouseUp事件。

private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
    AdjustingTheForm = true;
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
    if (AdjustingTheForm)
    {
        scr = Screen.FromControl(this).WorkingArea;
        LastLocation = e.Location;
        if (FormNormalSize == false)
        {
            FormNormalSize = true;
            CustomForm_Resize(sender, e);
            this.Location = new Point(e.X - 400, e.Y - 15);
            this.Update();
        }
        AdjustingTheForm = false;
        MovingTheForm = true;
        Console.WriteLine("1. " + this.Location);
    }
    if(MovingTheForm)
    {
        this.Location = new Point((this.Location.X - LastLocation.X) + e.X, (this.Location.Y - LastLocation.Y) + e.Y);
        this.Update();
        Console.WriteLine("2. " + this.Location + " " + e.Location);
    }
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
    scr = Screen.FromControl(this).WorkingArea;
    MovingTheForm = false;

这是我放置私有实例成员的地方:

namespace CustomForm_Practice_1
{
    public partial class CustomForm : Form
    {
        bool minimizeB1MouseDown, maximizeB1MouseDown, exitB1MouseDown;
        bool FormNormalSize;
        bool AdjustingTheForm, MovingTheForm;
        Point LastLocation;
        Rectangle scr;
        .......

以下是移动表单时的结果(更新):

1. this.Location: {X=1100,Y=6}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}

1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=0,Y=0}

1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}

1. this.Location: {X=0,Y=2}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=8}

OLD 我不知道为什么x从703跳到0,y从8跳到0.然而,这个问题只发生在表单大小改变且表单是移动。当表格处于正常大小(800,600)时。这是表单调整大小事件:

这次this.Location从X = 1100开始,Y = 6,然后进入(0,0)。 e.Location做了同样的事情。这是表单调整大小事件:

  private void CustomForm_Resize(object sender, EventArgs e)
        {
            if (FormNormalSize == false)
            //Maximized Window
            {
                scr = Screen.FromControl(this).WorkingArea;
                this.Location = new Point(scr.X, scr.Y);
                this.Size = new Size(scr.Width, scr.Height);
                this.Update();
                //Panel Heights
                TopPanel.Height = 30;
                BottomPanel.Height = scr.Height - 32;
                //Panel Widths
                TopPanel.Width = scr.Width - 2;
                BottomPanel.Width = scr.Width - 2;
            }
            else if (FormNormalSize)
            //Normal Window
            {
                this.Size = new Size(800, 600);
                //Panel Heights
                TopPanel.Height = 30;
                BottomPanel.Height = this.Height - 32;
                //Panel Widths
                TopPanel.Width = this.Width - 2;
                BottomPanel.Width = this.Width - 2;
            }
            //Panel Locations
            TopPanel.Location = new Point(1, 1);
            BottomPanel.Location = new Point(1, TopPanel.Height + 1);

问题是,为什么表格位置在此行时转到(0,0) this.Location = new Point(e.X - 400, e.Y - 15);更改之前设置的位置this.Location = new Point(scr.X, scr.Y);

2 个答案:

答案 0 :(得分:1)

移动/调整这样的表单可能有点繁琐,因为MouseEventArgs中的鼠标位置是相对于表单的左上角而不是屏幕坐标而给出的。

在需要屏幕坐标时跟踪鼠标坐标的更好方法是使用MousePosition类以及通过Control.Capture = true捕获鼠标。

我演示这个的最简单方法是通过示例应用程序:

  1. 创建默认的Windows窗体应用。我将假设主窗体名为Form1
  2. 将名为Panel的{​​{1}}放到主表单上并设置其panel1 属于Dock
  3. 添加到Fill以下处理程序:panel1panel1_MouseDownpanel1_MouseMove
  4. 更改panel1_MouseUp中的代码,如下所示:

    Form1.cs
  5. 编译并运行应用程序,然后单击并拖动主窗口以移动它。当你移动它时它应该跟随鼠标。

    一旦你有了这个工作,你应该能够将相同的逻辑应用到你的应用程序。

答案 1 :(得分:0)

我明白了。 PointToClient帮助我获取LastLocation值。这是新代码:

   private void TopPanel_MouseDown(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left)
        {
            AdjustingTheForm = true;
        }
        else if (e.Button == MouseButtons.Right)
        {
            AdjustingTheForm = false;
        }
        LastLocation = PointToClient(e.Location);
    }
    private void TopPanel_MouseMove(object sender, MouseEventArgs e)
    {
        scr = Screen.FromControl(this).WorkingArea;
        if (AdjustingTheForm)
        {
            if (FormNormalSize == false)
            {
                FormNormalSize = true;
                CustomForm_Resize(sender, e);
                this.Location = new Point(Cursor.Position.X - 400, Cursor.Position.Y - 15);
                this.Update();
                LastLocation = PointToClient(e.Location);
                MovingFormMaximized = true;
            }
            else
            {
                MovingFormMinimized = true;
            }
            AdjustingTheForm = false;
        }
        if ((MovingFormMinimized && this.Location.Y > 0) || (MovingFormMinimized && this.Location.Y <= 0 && e.Y > 32))
        {
            mouseHuggingWall = false;
            this.Location = new Point(this.Location.X - LastLocation.X + e.X,
                this.Location.Y - LastLocation.Y + e.Y);
            this.Update();
        }
        else if (MovingFormMinimized && this.Location.Y <= 0)
        {
            mouseHuggingWall = true;
            this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
            this.Update();
        }
        if ((MovingFormMaximized && this.Location.Y > 0) || (MovingFormMaximized && this.Location.Y <= 0 && e.Y > 32))
        {
            mouseHuggingWall = false;
            this.Location = new Point(this.Location.X -LastLocation.X + e.X,
                this.Location.Y - LastLocation.Y + e.Y);
            this.Update();
        }
        else if (MovingFormMaximized && this.Location.Y <= 0)
        {
            mouseHuggingWall = true;
            this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
            this.Update();
        }
    }
    private void TopPanel_MouseUp(object sender, MouseEventArgs e)
    {
        scr = Screen.FromControl(this).WorkingArea;
        if(mouseHuggingWall == true)
        {
            FormNormalSize = false;
            CustomForm_Resize(sender, e);
        }
        MovingFormMinimized = false;
        MovingFormMaximized = false;
        mouseHuggingWall = false;
    }

我在代码中添加了更多内容,但主要修复程序在代码中。