Form.SetDesktopLocation(Int32, Int32) 方法

定義

設定桌面座標中表單的位置。

C#
public void SetDesktopLocation(int x, int y);

參數

x
Int32

表單位置的 X 座標。

y
Int32

表單位置的 Y 座標。

範例

下列範例示範如何使用 SetDesktopLocationLoadActivate 成員。 若要執行此範例,請將下列程式碼貼到名為 的 Form1 表單中,其中包含名為 的 Button1 按鈕,以及名為 Label1Label2 的兩 Label 個控制項。

C#
static int x = 200;
static int y = 200;

private void Button1_Click(System.Object sender, 
    System.EventArgs e)
{
    // Create a new Form1 and set its Visible property to true.
    Form1 form2 = new Form1();
    form2.Visible = true;

    // Set the new form's desktop location so it  
    // appears below and to the right of the current form.
    form2.SetDesktopLocation(x, y);
    x += 30;
    y += 30;

    // Keep the current form active by calling the Activate
    // method.
    this.Activate();
    this.Button1.Enabled = false;
}

// Updates the label text to reflect the current values of x 
// and y, which was were incremented in the Button1 control's 
// click event.
private void Form1_Activated(object sender, System.EventArgs e)
{
    Label1.Text = "x: "+x+" y: "+y;
    Label2.Text = "Number of forms currently open: "+count;
}

static int count = 0;

private void Form1_Closed(object sender, System.EventArgs e)
{
    count -= 1;
}

private void Form1_Load(object sender, System.EventArgs e)
{
    count += 1;
}

備註

桌面座標是以螢幕的工作區域為基礎,其中會排除工作列。 您可以使用此方法將表單放置在桌面上。 因為桌面座標是以表單的工作區為基礎,所以您可以使用這個方法來確保您的表單在桌面上完全可見。 這個方法主要用於最上層表單;使用 LayoutMdi 方法,將多文檔介面放在 MDI) 子表單 (。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱