70

フルスクリーンで実行する必要がある .net Windows アプリケーションがあります。ただし、アプリケーションが起動すると、メイン フォームの上にタスクバーが表示され、フォームをクリックするか、ALT-TAB を使用してフォームをアクティブにしたときにのみ消えます。フォームの現在のプロパティは次のとおりです。

  • WindowState=FormWindowState.Normal
  • TopMost=通常
  • Size=1024,768 (これは、実行するマシンの画面解像度です)
  • FormBorderStyle = なし

フォームの読み込み時に以下を追加しようとしましたが、うまくいきませんでした:

  • this.Focus(); (フォーカスを与えた後 this.Focus プロパティは常に false です)
  • this.BringToFront();
  • this.TopMost = true; (ただし、これは私のシナリオでは理想的ではありません)
  • this.Bounds = Screen.PrimaryScreen.Bounds;
  • this.Bounds = Screen.PrimaryScreen.Bounds;

.NET内でそれを行う方法はありますか、それともネイティブのWindowsメソッドを呼び出す必要がありますか?もしそうなら、コードスニペットは非常に高く評価されます.

どうもありがとう

4

9 に答える 9

65

私は非常に多くの解決策を試しましたが、そのうちのいくつかはWindows XPで機能し、すべてがWindows7では機能しませんでした。結局のところ私はそうするための簡単なメソッドを作成します。

private void GoFullscreen(bool fullscreen)
    {
        if (fullscreen)
        {
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
        }
        else
        {
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
        }
    }

コードの順序は重要であり、WindwosStateとFormBorderStyleの場所を変更すると機能しません。

この方法の利点の1つは、TOPMOSTをfalseのままにして、他のフォームがメインフォームに優先するようにすることです。

それは私の問題を完全に解決しました。

于 2012-01-15T09:37:36.900 に答える
16

これがフォームをフルスクリーンにする方法です。

private void button1_Click(object sender, EventArgs e)
{
    int minx, miny, maxx, maxy;
    inx = miny = int.MaxValue;
    maxx = maxy = int.MinValue;

    foreach (Screen screen in Screen.AllScreens)
    {
        var bounds = screen.Bounds;
        minx = Math.Min(minx, bounds.X);
        miny = Math.Min(miny, bounds.Y);
        maxx = Math.Max(maxx, bounds.Right);
        maxy = Math.Max(maxy, bounds.Bottom);
    }

    Form3 fs = new Form3();
    fs.Activate();
    Rectangle tempRect = new Rectangle(1, 0, maxx, maxy);
    this.DesktopBounds = tempRect;
}
于 2013-08-05T04:59:58.937 に答える
15

私の簡単な修正は、フォームのメソッドを呼び出すことが判明したためActivate()、使用する必要はありませんTopMost(これが私が目指していたものです)。

于 2010-02-16T11:00:10.003 に答える
3

FormBorderStyle プロパティを None に、WindowState を Maximized に設定するだけでできると思います。Visual Studio を使用している場合、これらは両方とも IDE で見つけることができるため、プログラムで行う必要はありません。これを行う前に、プログラムを閉じる/終了する何らかの方法を含めるようにしてください。これにより、右上隅にあるとても便利な X が削除されます。

編集:

代わりにこれを試してください。長年保管していた切り株です。誰の功績か思い出せませんが、うまくいきます。

/*
 * A function to put a System.Windows.Forms.Form in fullscreen mode
 * Author: Danny Battison
 * Contact: gabehabe@googlemail.com
 */

        // a struct containing important information about the state to restore to
        struct clientRect
        {
            public Point location;
            public int width;
            public int height;
        };
        // this should be in the scope your class
        clientRect restore;
                bool fullscreen = false;

        /// <summary>
        /// Makes the form either fullscreen, or restores it to it's original size/location
        /// </summary>
        void Fullscreen()
        {
            if (fullscreen == false)
            {
                this.restore.location = this.Location;
                this.restore.width = this.Width;
                this.restore.height = this.Height;
                this.TopMost = true;
                this.Location = new Point(0,0);
                this.FormBorderStyle = FormBorderStyle.None;
                this.Width = Screen.PrimaryScreen.Bounds.Width;
                this.Height = Screen.PrimaryScreen.Bounds.Height;
            }
            else
            {
                this.TopMost = false;
                this.Location = this.restore.location;
                this.Width = this.restore.width;
                this.Height = this.restore.height;
                                // these are the two variables you may wish to change, depending
                                // on the design of your form (WindowState and FormBorderStyle)
                this.WindowState = FormWindowState.Normal;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }
于 2010-02-16T10:16:21.063 に答える
2

私はそれがどのように機能するかについて説明していませんが、機能しており、カウボーイコーダーであることだけが必要です.

        System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
        this.MaximizedBounds = Screen.GetWorkingArea(this);
        this.WindowState = FormWindowState.Maximized;
于 2014-08-24T13:43:43.517 に答える