첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ RECTANGLE.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 사각형
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct RECTANGLE
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// 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
    }
}

 

728x90

 

▶ MONITOR_INFORMATION.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 모니터 정보
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
    public class MONITOR_INFORMATION
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 크기
        /// </summary>
        public int Size = Marshal.SizeOf(typeof(MONITOR_INFORMATION));

        /// <summary>
        /// 디스플레이 사각형
        /// </summary>
        public RECTANGLE DisplayRectangle = new RECTANGLE();

        /// <summary>
        /// 작업 영역 사각형
        /// </summary>
        public RECTANGLE WorkingAreaRectangle = new RECTANGLE();

        /// <summary>
        /// 플래그
        /// </summary>
        public int Flag = 0;

        /// <summary>
        /// 장치명
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public char[] DeviceName = new char[32];

        #endregion
    }
}

 

300x250

 

▶ Monitor.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace TestProject
{
    /// <summary>
    /// 모니터
    /// </summary>
    public class Monitor
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Class
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 모니터 열거 콜백 - MonitorEnumCallback

        /// <summary>
        /// 모니터 열거 콜백
        /// </summary>
        private class MonitorEnumCallback
        {
            ////////////////////////////////////////////////////////////////////////////////////////// Property
            //////////////////////////////////////////////////////////////////////////////// Public

            #region 모니터 리스트 - MonitorList

            /// <summary>
            /// 모니터 리스트
            /// </summary>
            public List<Monitor> MonitorList { get; private set; }

            #endregion

            ////////////////////////////////////////////////////////////////////////////////////////// Constructor
            //////////////////////////////////////////////////////////////////////////////// Public

            #region 생성자 - MonitorEnumCallback()

            /// <summary>
            /// 생성자
            /// </summary>
            public MonitorEnumCallback()
            {
                MonitorList = new List<Monitor>();
            }

            #endregion

            ////////////////////////////////////////////////////////////////////////////////////////// Method
            //////////////////////////////////////////////////////////////////////////////// Public

            #region 콜백 처리하기 - Callback(monitorHandle, deviceContextHandle, monitorRectangleHandle, dataHandle)

            /// <summary>
            /// 콜백 처리하기
            /// </summary>
            /// <param name="monitorHandle">모니터 핸들</param>
            /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param>
            /// <param name="monitorRectangleHandle">모니터 사각형 핸들</param>
            /// <param name="dataHandle">데이터 핸들</param>
            /// <returns>처리 결과</returns>
            public bool Callback(IntPtr monitorHandle, IntPtr deviceContextHandle, IntPtr monitorRectangleHandle, IntPtr dataHandle)
            {
                MonitorList.Add(new Monitor(monitorHandle, deviceContextHandle));

                return true;
            }

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 모니터 열거 프로시저 대리자 - MonitorEnumProc(monitorHandle, deviceContextHandle, monitorRectangleHandle, dataHandle)

        /// <summary>
        /// 모니터 열거 프로시저 대리자
        /// </summary>
        /// <param name="monitorHandle">모니터 핸들</param>
        /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param>
        /// <param name="monitorRectangleHandle">모니터 사각형 핸들</param>
        /// <param name="dataHandle">데이터 핸들</param>
        /// <returns>처리 결과</returns>
        private delegate bool MonitorEnumProc(IntPtr monitorHandle, IntPtr deviceContextHandle, IntPtr monitorRectangleHandle, IntPtr dataHandle);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 모니터 정보 구하기 - GetMonitorInfo(monitorHandleRef, monitorInfo)
 
        /// <summary>
        /// 모니터 정보 구하기
        /// </summary>
        /// <param name="monitorHandleRef">모니터 참조 핸들</param>
        /// <param name="monitorInfo">모니터 정보</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32", CharSet = CharSet.Auto)]
        [ResourceExposure(ResourceScope.None)]
        private static extern bool GetMonitorInfo(HandleRef monitorHandleRef, [In, Out]MONITOR_INFORMATION monitorInfo);

        #endregion
        #region 디스플레이 모니터 열거하기 - EnumDisplayMonitors(deviceContextHandle, clippingRectangleHandle, monitorEnumProc, dataHandle)

        /// <summary>
        /// 디스플레이 모니터 열거하기
        /// </summary>
        /// <param name="deviceContextHandle">디바이스 컨텍스 핸들</param>
        /// <param name="clippingRectangleHandle">클리핑 사각형 핸들</param>
        /// <param name="monitorEnumProc">모니터 열거 프로시저</param>
        /// <param name="dataHandle">데이터 핸들</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32", ExactSpelling = true)]
        [ResourceExposure(ResourceScope.None)]
        private static extern bool EnumDisplayMonitors
        (
            HandleRef       deviceContextHandle,
            IntPtr          clippingRectangleHandle,
            MonitorEnumProc monitorEnumProc,
            IntPtr          dataHandle
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// NULL 참조 핸들
        /// </summary>
        public static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 주 장치를 위한 모니터 정보
        /// </summary>
        private const int MONITOR_INFORMATION_FOR_PRIMARY = 0x00000001;

        #endregion
 
        ////////////////////////////////////////////////////////////////////////////////////////// Property
        //////////////////////////////////////////////////////////////////////////////// Public
 
        #region 경계 사각형 - BoundRectangle

        /// <summary>
        /// 경계 사각형
        /// </summary>
        public System.Windows.Rect BoundRectangle { get; private set; }

        #endregion
        #region 작업 영역 사각형 - WorkingAreaRectangle

        /// <summary>
        /// 작업 영역 사각형
        /// </summary>
        public System.Windows.Rect WorkingAreaRectangle { get; private set; }

        #endregion
        #region 장치명 - DeviceName

        /// <summary>
        /// 장치명
        /// </summary>
        public string DeviceName { get; private set; }

        #endregion
        #region 주 장치 여부 - IsPrimary

        /// <summary>
        /// 주 장치 여부
        /// </summary>
        public bool IsPrimary { get; private set; }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Constructor
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 생성자 - Monitor(monitorHandle, deviceContextHandle)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="monitorHandle">모니터 핸들</param>
        /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param>
        private Monitor(IntPtr monitorHandle, IntPtr deviceContextHandle)
        {
            MONITOR_INFORMATION monitorInformation = new MONITOR_INFORMATION();

            GetMonitorInfo(new HandleRef(null, monitorHandle), monitorInformation);

            BoundRectangle = new System.Windows.Rect
            (
                monitorInformation.DisplayRectangle.Left,
                monitorInformation.DisplayRectangle.Top,
                monitorInformation.DisplayRectangle.Right  - monitorInformation.DisplayRectangle.Left,
                monitorInformation.DisplayRectangle.Bottom - monitorInformation.DisplayRectangle.Top
            );

            WorkingAreaRectangle = new System.Windows.Rect
            (
                monitorInformation.WorkingAreaRectangle.Left,
                monitorInformation.WorkingAreaRectangle.Top,
                monitorInformation.WorkingAreaRectangle.Right  - monitorInformation.WorkingAreaRectangle.Left,
                monitorInformation.WorkingAreaRectangle.Bottom - monitorInformation.WorkingAreaRectangle.Top
            );

            IsPrimary  = ((monitorInformation.Flag & MONITOR_INFORMATION_FOR_PRIMARY) != 0);
            DeviceName = new string(monitorInformation.DeviceName).TrimEnd((char)0);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Method
        //////////////////////////////////////////////////////////////////////////////// Static
        ////////////////////////////////////////////////////////////////////// Public

        #region 모든 모니터 열거 가능형 - AllMonitorEnumerable

        /// <summary>
        /// 모든 모니터 열거 가능형
        /// </summary>
        public static IEnumerable<Monitor> AllMonitorEnumerable
        {
            get
            {
                MonitorEnumCallback callback = new MonitorEnumCallback();

                MonitorEnumProc monitorEnumProc = new MonitorEnumProc(callback.Callback);

                EnumDisplayMonitors(NullHandleRef, IntPtr.Zero, monitorEnumProc, IntPtr.Zero);

                return callback.MonitorList.Cast<Monitor>();
            }
        }

        #endregion
    }
}

 

▶ Program.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            foreach(Monitor monitor in Monitor.AllMonitorEnumerable)
            {
                Console.WriteLine(monitor.DeviceName);
            }
        }

        #endregion
    }
}

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요