■ 특정 화면에서 전경 윈도우의 전체 화면 모드 여부를 구하는 방법을 보여준다.
▶ RECT.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 사각형 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct RECT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 왼쪽 /// </summary> public int Left; /// <summary> /// 위쪽 /// </summary> public int Top; /// <summary> /// 오른쪽 /// </summary> public int Right; /// <summary> /// 아래쪽 /// </summary> public int Bottom; #endregion } } |
▶ WindowHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 윈도우 헬퍼 /// </summary> public static class WindowHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 사각형 구하기 - GetWindowRect(windowHandle, rectangle) /// <summary> /// 윈도우 사각형 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="rectangle">사각형</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool GetWindowRect(HandleRef windowHandle, [In, Out] ref RECT rectangle); #endregion #region 전경 윈도우 구하기 - GetForegroundWindow() /// <summary> /// 전경 윈도우 구하기 /// </summary> /// <returns>전경 윈도우 핸들</returns> [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 전경 전체 화면 여부 구하기 - IsForegroundFullScreen(screen) /// <summary> /// 전경 전체 화면 여부 구하기 /// </summary> /// <param name="screen">화면</param> /// <returns>전경 전체 화면 여부</returns> public static bool IsForegroundFullScreen(Screen screen) { if(screen == null) { screen = Screen.PrimaryScreen; } RECT rectangle = new RECT(); GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rectangle); return new Rectangle ( rectangle.Left, rectangle.Top, rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top ).Contains(screen.Bounds); } #endregion #region 전경 전체 화면 여부 구하기 - IsForegroundFullScreen() /// <summary> /// 전경 전체 화면 여부 구하기 /// </summary> /// <returns>전경 전체 화면 여부</returns> public static bool IsForegroundFullScreen() { return IsForegroundFullScreen(null); } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using System; using System.Threading; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.testButton.Click += testButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 테스트 버튼 클릭시 처리하기 - testButton_Click(sender, e) /// <summary> /// 테스트 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton_Click(object sender, EventArgs e) { Thread.Sleep(5000); bool result = WindowHelper.IsForegroundFullScreen(Screen.AllScreens[0]); MessageBox.Show($"첫번째 모니터에서 전경 윈도우의 전체 화면 모드 여부 : {result}"); } #endregion } } |