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

TestProject.zip
0.01MB

▶ POINT.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 포인트
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// X
        /// </summary>
        public int X;

        /// <summary>
        /// Y
        /// </summary>
        public int Y;

        #endregion
    }
}

 

728x90

 

▶ 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
    }
}

 

300x250

 

▶ MONITOR_INFO_EX.cs

using System.Runtime.InteropServices;

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

        #region Field

        /// <summary>
        /// 크기
        /// </summary>
        public uint Size;

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

        /// <summary>
        /// 작업 영역 사각형
        /// </summary>
        public RECTANGLE WorkingAreaRectangle;

        /// <summary>
        /// 플래그
        /// </summary>
        public uint Flag;

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

        #endregion
    }
}

 

▶ MonitorHelper.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 모니터 헬퍼
    /// </summary>
    public static class MonitorHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 포인트에서 모니터 구하기 - MonitorFromPoint(point, flag)

        /// <summary>
        /// 포인트에서 모니터 구하기
        /// </summary>
        /// <param name="point">포인트</param>
        /// <param name="flag">플래그</param>
        /// <returns>모니터 핸들</returns>
        [DllImport("user32", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr MonitorFromPoint(POINT point, uint flag);

        #endregion
        #region 모니터 정보 구하기 - GetMonitorInfo(monitorHandle, monitorInfo)

        /// <summary>
        /// 모니터 정보 구하기
        /// </summary>
        /// <param name="monitorHandle">모니터 핸들</param>
        /// <param name="monitorInfo">모니터 정보</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool GetMonitorInfo(IntPtr monitorHandle, ref MONITOR_INFO_EX monitorInfo);

        #endregion

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

        #region 모니터 정보 구하기 - GetMonitorInformation(monitorHandle)

        /// <summary>
        /// 모니터 정보 구하기
        /// </summary>
        /// <param name="monitorHandle">모니터 핸들</param>
        /// <returns>모니터 정보</returns>
        public static MONITOR_INFO_EX GetMonitorInformation(IntPtr monitorHandle)
        {
            MONITOR_INFO_EX monitorInfo = new MONITOR_INFO_EX
            {
                Size = (uint)Marshal.SizeOf(typeof(MONITOR_INFO_EX))
            };

            GetMonitorInfo(monitorHandle, ref monitorInfo);

            return monitorInfo;
        }

        #endregion
        #region 모니터 정보 구하기 - GetMonitorInformation(point)

        /// <summary>
        /// 모니터 정보 구하기
        /// </summary>
        /// <param name="point">포인터</param>
        /// <returns>모니터 정보</returns>
        public static MONITOR_INFO_EX GetMonitorInformation(POINT point)
        {
            IntPtr monitorHandle = MonitorFromPoint(point, 0);

            MONITOR_INFO_EX monitorInfo = GetMonitorInformation(monitorHandle);

            return monitorInfo;
        }

        #endregion
    }
}

 

▶ Program.cs

using System;

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

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            MONITOR_INFO_EX monitorInfo = MonitorHelper.GetMonitorInformation(new POINT { X = 0, Y = 0 });

            Console.WriteLine(monitorInfo.DeviceName);
        }

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

댓글을 달아 주세요