[英]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?
At the top of Form1 i did: 在Form1的顶部,我做了:
private IntPtr ID;
private int counter = 0;
In the constructor i did: 在构造函数中,我做到了:
ID = this.Handle;
timer2.Enabled = true;
Then in timer2 tick event i did: 然后在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;
}
But the form start to move from the top left corner at 0,0 to the right. 但是表格开始从0,0的左上角向右移动。 I want the form will start to move from the center of the screen to the left untill it hit the left border/bound and stop and stay there.
我希望表单将开始从屏幕的中心向左移动,直到它碰到左边框/边界并停止并停留在那里。
How can i do it ? 我该怎么做 ?
I found now how to make it to move to the left and stop on the left border/bound: 我现在发现了如何使其移至左侧并在左侧边框/边界处停止:
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;
}
But how do i make that it will start to move from the middle/center of the screen ? 但是如何使它从屏幕的中间/中央开始移动? I did in the designer change the property: StartPosition to CenterScreen But the form start to move from the top left corner 0,0
我在设计器中做了更改属性:StartPosition到CenterScreen但该窗体开始从左上角0,0移动
有一个更好的解决方案,只需使用如下protected
方法CenterToScreen()
:
this.CenterToScreen();
This is the answer. 这就是答案。
x = Screen.PrimaryScreen.Bounds.Bottom - this.Width * 2;
y = Screen.PrimaryScreen.Bounds.Bottom - this.Height * 2;
counter = x;
Then in the timer tick event: 然后在计时器滴答事件中:
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;
}
Before it the counter-- was set to 0. And instead y it was 0 too. 在它之前,计数器-设置为0。相反,y也为0。 So it's 0,0 this is the location.
所以它是0,0,这是位置。 Now counter and y start location is the middle of the screen.
现在counter和y的起始位置在屏幕中间。
Thanks. 谢谢。
You could set the StartPosition
property of your main WinForm to CenterScreen
: 您可以将主WinForm的
StartPosition
属性设置为CenterScreen
:
Form1.StartPosition = FormStartPosition.CenterScreen;
Then if you want it to appear somehow on a different position relative to this screen center, you can play with the Top
and Left
properties to add or subtract the required number of pixels. 然后,如果希望它以某种方式相对于此屏幕中心出现在不同的位置,则可以使用“
Top
和“ Left
属性来添加或减去所需的像素数。
if you need to know the screen bounds: 如果您需要知道屏幕边界:
System.Windows.Forms.Screen.PrimaryScreen.Bounds
or, taking into account the task bar: 或者,考虑到任务栏:
System.Windows.Forms.Screen.PrimaryScreen.WorkingArea
...or some variant ...或某些变体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.