簡體   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