[c#] WinForms 앱을 전체 화면으로 만드는 방법

전체 화면으로 만들려고하는 WinForms 앱이 있습니다 (VS가 전체 화면 모드에서 수행하는 것과 비슷 함).

현재 내가 설정하고 FormBorderStyleNoneWindowStateMaximized있는 것은 나에게 조금 더 많은 공간을 제공하지만, 그것을 볼 경우 작업 표시 줄을 통해 적용되지 않습니다.

그 공간도 사용하려면 어떻게해야합니까?

보너스 포인트의 경우 MenuStrip해당 공간을 포기하기 위해 자동 숨김을 만들 수있는 방법이 있습니까?



답변

기본 질문에 다음이 트릭을 수행합니다 (작업 표시 줄 숨기기)

private void Form1_Load(object sender, EventArgs e)
{
    this.TopMost = true;
    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
}

그러나 흥미롭게도 마지막 두 줄을 바꾸면 작업 표시 줄이 계속 표시됩니다. 이러한 작업의 순서는 속성 창에서 제어하기 어려울 것이라고 생각합니다.


답변

테스트되고 간단한 솔루션

나는 SO 및 다른 사이트 에서이 질문에 대한 답을 찾고 있었지만 하나는 대답이 매우 복잡했고 다른 일부는 단순히 올바르게 작동하지 않으므로 많은 코드 테스트 후에이 퍼즐을 해결했습니다.

참고 : Windows 8을 사용하고 있습니다. 중이고 작업 표시 줄이 자동 숨기기 모드가 아닙니다.

수정을 수행하기 전에 WindowState를 Normal로 설정하면 적용되지 않는 작업 표시 줄에서 오류가 중지된다는 것을 발견했습니다.

코드

첫 번째는 “전체 화면 모드”로 들어가고 두 번째는 “전체 화면 모드”에서 나가는 두 가지 메서드가있는이 클래스를 만들었습니다. 따라서이 클래스의 개체를 만들고 전체 화면을 설정하려는 Form을 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;
        }
    }

나는이 질문의 중복인지 아닌지 확실하지 않은 다른 질문 에이 동일한 답변을 배치했습니다. (다른 질문에 대한 링크 : 작업 표시 줄 상단에 Windows Form을 전체 화면으로 표시하는 방법은 무엇입니까? )


답변

그리고 menustrip-question의 경우 set

MenuStrip1.Parent = Nothing

전체 화면 모드에서는 사라집니다.

전체 화면 모드를 종료 할 때 menustrip1.parent 다시 양식으로 하면 메뉴 스트립이 다시 정상이됩니다.


답변

다음 코드를 사용하여 시스템 화면에 맞출 수 있으며 작업 표시 줄이 표시됩니다.

    private void Form1_Load(object sender, EventArgs e)
    {
        // hide max,min and close button at top right of Window
        this.FormBorderStyle = FormBorderStyle.None;
        // fill the screen
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }

사용할 필요가 없습니다 :

    this.TopMost = true;

해당 라인 alt+tab은 다른 응용 프로그램으로 전환하는 데 방해가 됩니다. ( “TopMost”는 “TopMost”로 표시되지 않는 한, 창이 다른 창 위에 유지됨을 의미합니다.)


답변

최근에 Mediaplayer 응용 프로그램을 만들고 API 호출을 사용하여 프로그램이 전체 화면으로 실행될 때 작업 표시 줄이 숨겨 졌는지 확인한 다음 프로그램이 전체 화면이 아니거나 포커스가 없거나 종료되었을 때 작업 표시 줄을 복원했습니다.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

Sub HideTrayBar()
    Try


        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(tWnd, 0)
        ShowWindow(bWnd, 0)
    Catch ex As Exception
        'Error hiding the taskbar, do what you want here..
    End Try
End Sub
Sub ShowTraybar()
    Try
        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(bWnd, 1)
        ShowWindow(tWnd, 1)
    Catch ex As Exception
    'Error showing the taskbar, do what you want here..
               End Try


End Sub


답변

창을 최상위로 설정해야합니다.


답변

.NET 2.0에서 작동할지 모르겠지만 .NET 4.5.2에서 작동했습니다. 다음은 코드입니다.

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Your_Form_Name : Form
{
    public Your_Form_Name()
    {
        InitializeComponent();
    }

    // CODE STARTS HERE

    private System.Drawing.Size oldsize = new System.Drawing.Size(300, 300);
    private System.Drawing.Point oldlocation = new System.Drawing.Point(0, 0);
    private System.Windows.Forms.FormWindowState oldstate = System.Windows.Forms.FormWindowState.Normal;
    private System.Windows.Forms.FormBorderStyle oldstyle = System.Windows.Forms.FormBorderStyle.Sizable;
    private bool fullscreen = false;
    /// <summary>
    /// Goes to fullscreen or the old state.
    /// </summary>
    private void UpgradeFullscreen()
    {
        if (!fullscreen)
        {
            oldsize = this.Size;
            oldstate = this.WindowState;
            oldstyle = this.FormBorderStyle;
            oldlocation = this.Location;
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            fullscreen = true;
        }
        else
        {
            this.Location = oldlocation;
            this.WindowState = oldstate;
            this.FormBorderStyle = oldstyle;
            this.Size = oldsize;
            fullscreen = false;
        }
    }

    // CODE ENDS HERE
}

용법:

UpgradeFullscreen(); // Goes to fullscreen
UpgradeFullscreen(); // Goes back to normal state
// You don't need arguments.

주의 사항 : Form의 클래스 (예 🙂 안에 배치해야 partial class Form1 : Form { /* Code goes here */ }합니다. 그렇지 않으면 어떤 양식에도 배치하지 않으면 코드 this가 예외를 생성 하기 때문에 작동하지 않습니다 .