我这样做,form1将慢慢向左移动。我怎么能让它从屏幕中心开始?

时间:2013-09-29 09:12:27

标签: c# winforms

在Form1的顶部我做了:

private IntPtr ID;
private int counter = 0;

在构造函数中我做了:

ID = this.Handle;
timer2.Enabled = true;

然后在timer2嘀嗒事件我做了:

private void timer2_Tick(object sender, EventArgs e)
        {
            if (counter <= Screen.PrimaryScreen.Bounds.Right)
                MoveWindow(ID, counter++, 0, this.Width, this.Height, true);
            else
                counter = 0;
        }

但是表格开始从左上角0,0移动到右边。 我希望表单将开始从屏幕中心向左移动,直到它触及左边界/绑定并停在那里。

我该怎么做?

我现在发现如何让它向左移动并停在左边界/边界上:

private void timer2_Tick(object sender, EventArgs e)
        {
            if (counter >= Screen.PrimaryScreen.Bounds.Left)
                MoveWindow(ID, counter--, 0, this.Width, this.Height, true);
            else
                counter = 0;
        }

但是我该如何让它开始从屏幕的中间/中心移动? 我在设计器中更改了属性:StartPosition到CenterScreen 但是表格开始从左上角移动0,0

3 个答案:

答案 0 :(得分:1)

有一个更好的解决方案,只需使用protected方法CenterToScreen(),如下所示:

this.CenterToScreen();

答案 1 :(得分:0)

这就是答案。

x = Screen.PrimaryScreen.Bounds.Bottom - this.Width * 2;
y = Screen.PrimaryScreen.Bounds.Bottom - this.Height * 2;
counter = x;

然后在计时器刻度事件中:

private void timer2_Tick(object sender, EventArgs e)
        {
            if (counter >= Screen.PrimaryScreen.Bounds.Left)
                MoveWindow(ID, counter--, y, this.Width, this.Height, true);
            else
                counter = 0;
        }

在它之前,counter--被设置为0.而y也是0。 所以它是0,0这是位置。 现在,计数器和y开始位置是屏幕的中间位置。

感谢。

答案 2 :(得分:0)

您可以将主WinForm的StartPosition属性设置为CenterScreen

 Form1.StartPosition = FormStartPosition.CenterScreen;

然后,如果您希望它以某种方式显示在相对于此屏幕中心的不同位置,您可以使用TopLeft属性来添加或减去所需的像素数。

如果您需要了解屏幕边界:

System.Windows.Forms.Screen.PrimaryScreen.Bounds

或考虑到任务栏:

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

......或某些变体

相关问题