简体   繁体   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?

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.

相关问题 当我/拖动/移动新窗体后面的form1使其同时与form1一起移动时,我该如何做? - How can i make that when i /drag/move form1 around the new form behind it will move with form1 on the same time? 我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心? - How can i do that on mouse enter event the new form/usercontrol will move to the center of Form1? 如何在类中创建 form1 的实例并在 form1 中创建类的实例? - How can I make instance of form1 in a class and make instance of the class in form1? 我如何从Form2调用参数为Form1的方法并在Form1的图表上绘图 - How can i call a method with parameter of form1 from form2 and plot on chart on form1 如何在Form1上创建一个标签,从form2中说出“Hello”? - How can I make a label on Form1 say “Hello” from form2? 如何在Form2中使用Form1中的变量? - How can I use variables from Form1 in Form2? 如何将 Timer 从 Form1 导入 Form2? - How can I import a Timer from Form1 to Form2? 如何从对话框中更改 Form1 中的某些内容? - How can I Change something in a Form1 from a DialogBox? 如何将表单设置在屏幕中央? - How can i set the Form to be in the Center of the Screen? 如何在不创建 form1 的新实例的情况下在 form2 中调用 form1 的方法 - How can I call method of form1 in form2 without create new instance of form1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM