Получить название монитора через WinApi?

В общем хочу узнать модель монитора. Через WMI это не делается. Впервые столкнулся с WinApi и замер. Больше от недопонимания. тыцвот здесь нашел код, но не совсем понятно как пихнуть его в проект). Не могли бы вы подсказать как заюзать этот класс?
  • Вопрос задан
  • 2745 просмотров
Пригласить эксперта
Ответы на вопрос 2
aush
@aush
А куда вы ваши классы пихаете? Обычные классы, обычные структуры, обычные функции (разве что вместо того, чтобы их реализовывать, просто указывают, что их надо экспортировать из другой длл). Добавьте новый файлик в проект и вставьте в него этот код.
Ответ написан
cjey
@cjey
Вот пример вызовов WinAPI для получения информации(разрешение, область в виртуальном мониторе) для всех мониторов(в многомониторной конфигурации).

Запускать MonitorInfo.FindMonitors();

using System;
using System.Runtime.InteropServices;

namespace MonitorInfoWinAPI
{
    public class MonitorInfo
    {
        private static IntPtr hDesktop;

        private static bool EnumerateMonitors(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData)
        {
            MonitorInfo.hDesktop = hDesktop;
            MonitorInfoEx info = new MonitorInfoEx();
            GetMonitorInfo(hDesktop, info);

            Console.WriteLine("Monitor {0}", new string(info.szDevice));
            Console.WriteLine("  resolution                   {0}x{1}", info.rcMonitor.right, info.rcMonitor.bottom);
            Console.WriteLine("  virtual monitor left  top    {0}x{1}", info.rcWork.left, info.rcMonitor.top);
            Console.WriteLine("  virtual monitor right bottom {0}x{1}", info.rcWork.right, info.rcMonitor.bottom);

            Console.WriteLine();

            return true;
        }

        public static void FindMonitors()
        {
            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumerateMonitors, IntPtr.Zero);
            return;
        }


        public delegate bool EnumMonitorsDelegate(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData);

        #region DLLImport
        [DllImport("user32.dll")]
        public static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

        [DllImport("user32.dll")]
        public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetMonitorInfo(IntPtr hmonitor, [In, Out]MonitorInfoEx info);

        #endregion


        #region Structs

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
        public class MonitorInfoEx
        {
            public int cbSize = Marshal.SizeOf(typeof(MonitorInfoEx));
            public Rect rcMonitor = new Rect();
            public Rect rcWork = new Rect();
            public int dwFlags = 0;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public char[] szDevice = new char[32];
        }

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

        [StructLayout(LayoutKind.Sequential)]
        public struct Point
        {
            public int X;
            public int Y;
        }

        #endregion
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы