1

Form1 の上部で、次のことを行いました。

private IntPtr ID;
private int counter = 0;

コンストラクターで私がした:

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

次に、timer2 ティック イベントで次のことを行いました。

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 to CenterScreen しかし、フォームは左上隅から移動し始めます 0,0

4

3 に答える 3

1

より良い解決策があります。次のようなprotected方法を使用してください。CenterToScreen()

this.CenterToScreen();
于 2013-09-29T10:25:52.370 に答える
0

StartPositionメインの WinFormのプロパティを次のように設定できますCenterScreen

 Form1.StartPosition = FormStartPosition.CenterScreen;

次に、この画面の中心に対して何らかの形で別の位置に表示したい場合は、TopおよびLeftプロパティを操作して、必要なピクセル数を加算または減算できます。

画面の境界を知る必要がある場合:

System.Windows.Forms.Screen.PrimaryScreen.Bounds

または、タスク バーを考慮して:

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

...またはいくつかのバリアント

于 2013-09-29T10:56:21.037 に答える
0

これが答えです。

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

それ以前は、counter-- は 0 に設定されていました。代わりに y も 0 でした。これは 0,0 です。これが場所です。現在、カウンターと y の開始位置は画面の中央です。

ありがとう。

于 2013-09-29T09:24:02.703 に答える