C# :: Code & APIs

[C#] 다른 프로그램이 전체 화면 [Full-Screen] 으로 실행 중인지 여부 확인하기.

안녕하세요! HappyBono 인사드립니다.

대부분의 앱들에서 제공하는 팝업 알림 기능에 단점이 존재한다면, 게임이나 영상물 등의 미디어 컨텐츠를 즐기거나, Microsoft PowerPoint (파워포인트) 앱에서 제공하는 슬라이드 쇼 기능으로 발표 중인 경우, 화면에 팝업 알림이 표시되면서 흐름이 끊기는 경우가 빈번합니다.

특히, 그래픽 카드를 이용한 게임 중에는 팝업 창을 닫거나, 제거하려고 [닫기 (X)] 버튼을 누르면, 창의 포커스가 바탕화면에 위치한 관계로, 즐기던 게임은 최소화되고 바탕 화면으로 전환됩니다. 이로 인하여, 사용자 경험에는 좋지 않은 영향을 끼칠 수 있는데요,

아래와 같이, 다른 프로그램이 전체 화면 [Full-Screen] 으로 실행 중인지 여부를 확인할 수 있는 방법을 구현해보았고, 이를 통해 팝업 창을 표시하기 전, 실행 환경을 감지하여 팝업 창 표시 여부에 대한 조건을 지정해줄 수 있습니다.

아래 코드를 모듈에 넣고,  IsForegroundFullScreen() 함수를 호출하셔서 사용하시면 됩니다.

 

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll", SetLastError = true)]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    public static bool IsForegroundFullScreen()
    {
        return IsForegroundFullScreen(null);
    }


    public static bool IsForegroundFullScreen(System.Windows.Forms.Screen screen)
    {

        if (screen == null)
        {
            screen = System.Windows.Forms.Screen.PrimaryScreen;
        }
        RECT rect = new RECT();
        IntPtr hWnd = (IntPtr)GetForegroundWindow();


        GetWindowRect(new HandleRef(null, hWnd), ref rect);

        /* in case you want the process name:
        uint procId = 0;
        GetWindowThreadProcessId(hWnd, out procId);
        var proc = System.Diagnostics.Process.GetProcessById((int)procId);
        Console.WriteLine(proc.ProcessName);
        */


        if (screen.Bounds.Width == (rect.right - rect.left) && screen.Bounds.Height == (rect.bottom - rect.top))
        {
            Console.WriteLine("Fullscreen!")
            return true;
        }
        else {
            Console.WriteLine("Nope, :-(");
            return false;
        }
    }

 

여러분들께 도움이 되셨으면 좋겠습니다.
고맙습니다.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: