C# :: Code & APIs

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

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

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

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

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

아래 코드를 static class 에 넣고,

 

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public static class FullScreenAppsModule
{
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left, top, right, bottom;
    }

    [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")] private static extern IntPtr GetShellWindow();
    [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetClassName(IntPtr hWnd, StringBuilder sb, int max);

    public static bool IsForegroundFullScreen() => IsForegroundFullScreen(null);

    public static bool IsForegroundFullScreen(Screen targetScreen)
    {
        IntPtr hWnd = GetForegroundWindow();
        if (hWnd == IntPtr.Zero || hWnd == GetShellWindow()) return false;

        // 바탕화면 (Progman / WorkerW) 제외
        var sb = new StringBuilder(256);
        GetClassName(hWnd, sb, sb.Capacity);
        string cls = sb.ToString();
        if (cls == "Progman" || cls == "WorkerW") return false;

        if (!GetWindowRect(hWnd, out RECT r)) return false;

        // 지정한 화면이 없으면 Foreground 창이 있는 모니터 기준
        if (targetScreen == null) targetScreen = Screen.FromHandle(hWnd);
        var b = targetScreen.Bounds;

        // 최대화 창은 보이지 않는 테두리 때문에 화면보다 커서 구분되므로,
        // >= 로 완화하지 말고 네 변이 정확히 일치할 때만 전체 화면으로 판단
        return r.left == b.Left && r.top == b.Top &&
               r.right == b.Right && r.bottom == b.Bottom;
    }
}

 

IsForegroundFullScreen() 함수를 호출하셔서 사용하시면 됩니다.

 

if (FullScreenAppsModule.IsForegroundFullScreen())
{
    // 전체 화면 앱 실행 중: 팝업 표시를 보류
    return;
}
ShowNotificationForm();

 

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

Leave a Reply

Discover more from Dream big, Achieve more.

Subscribe now to keep reading and get access to the full archive.

Continue reading