繁体   English   中英

我做到了,form1将缓慢向左移动。 如何使它从屏幕中心开始?

[英]I did that the form1 will move slowly to the left. How can i make that it will start from the center of the screen?

在Form1的顶部,我做了:

private IntPtr ID;
private int counter = 0;

在构造函数中,我做到了:

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

然后在timer2 tick事件中,我做了:

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移动

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

this.CenterToScreen();

这就是答案。

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;
        }

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

谢谢。

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

 Form1.StartPosition = FormStartPosition.CenterScreen;

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

如果您需要知道屏幕边界:

System.Windows.Forms.Screen.PrimaryScreen.Bounds

或者,考虑到任务栏:

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

...或某些变体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM