web-dev-qa-db-ja.com

タスクバーの上にフルスクリーンでWindowsフォームを表示する方法は?

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

  • WindowState = FormWindowState.Normal
  • TopMost = Normal
  • 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メソッドを呼び出す必要がありますか?その場合は、コードスニペットが非常に高く評価されます。

どうもありがとう

61

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

12

つかいます:

FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;

そして、フォームがタスクバーの上に配置されます。

95
TcKs

私は非常に多くのソリューションを試しましたが、それらのいくつかはWindows XPで動作しますが、それらはすべてWindows 7では動作しませんでした。

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のままにしておくことです。これにより、他のフォームがメインフォームを通過できるようになります。

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

47
mammadalius

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

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;
}
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
5
MEO

テスト済みのシンプルなソリューション

私はSOおよび他のいくつかのサイトでこの質問の答えを探していましたが、答えを出した人は私にとって非常に複雑であり、他のいくつかの答えは単に正しく動作しませんテストこのパズルを解きました。

注:Windows 8を使用していますが、タスクバーは自動非表示モードではありません。

変更を行う前にWindowStateをNormalに設定すると、カバーされていないタスクバーでエラーが停止することを発見しました。

コード

2つのメソッドを持つこのクラスを作成しました。1つ目は「フルスクリーンモード」に入り、2つ目は「フルスクリーンモード」を終了します。したがって、このクラスのオブジェクトを作成し、フルスクリーンに設定するフォームを引数としてEnterFullScreenModeメソッドまたはLeaveFullScreenModeメソッドに渡すだけです。

class FullScreen
{
    public void EnterFullScreenMode(Form targetForm)
    {
        targetForm.WindowState = FormWindowState.Normal;
        targetForm.FormBorderStyle = FormBorderStyle.None;
        targetForm.WindowState = FormWindowState.Maximized;
    }

    public void LeaveFullScreenMode(Form targetForm)
    {
        targetForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
        targetForm.WindowState = FormWindowState.Normal;
    }
}

使用例

    private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
    {
        FullScreen fullScreen = new FullScreen();

        if (fullScreenMode == FullScreenMode.No)  // FullScreenMode is an enum
        {
            fullScreen.EnterFullScreenMode(this);
            fullScreenMode = FullScreenMode.Yes;
        }
        else
        {
            fullScreen.LeaveFullScreenMode(this);
            fullScreenMode = FullScreenMode.No;
        }
    }

私はこの同じ答えを別の質問に載せましたが、これが重複しているかどうかはわかりません。 (他の質問へのリンク: WinFormsアプリをフルスクリーンにする方法

4
Zignd

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

編集:

代わりにこれを試してください。これは私が長い間保管してきたスニペットです。誰がそれを信用したかさえ思い出せませんが、うまくいきます。

/*
 * A function to put a System.Windows.Forms.Form in fullscreen mode
 * Author: Danny Battison
 * Contact: [email protected]
 */

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

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

        System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
        this.MaximizedBounds = Screen.GetWorkingArea(this);
        this.WindowState = FormWindowState.Maximized;
1
Antonio Lopez