本文介绍了C#如何获得Windows监视器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让显示所有显示器和监视器名称的功能。
,而第2次我叫enumDisplayDevices,如果第一个参数是lpDisplayDevice.DeviceName有一个错误。我知道这是第一个参数监守如果它被设置为IntPtr.Zero代替,没有错误。



我无法找到如何做到这一点在C#中的另一个例子。

 公共静态无效show_display_devices()
{
DISPLAY_DEVICE lpDisplayDevice =新DISPLAY_DEVICE(0); // OUT
INT DEVNUM = 0;
,而(真)
{
BOOL成功= EnumDisplayDevices(IntPtr.Zero,DEVNUM,参考lpDisplayDevice,0);
如果
断点(成功!);

Console.WriteLine(\\\
devNum =+ DEVNUM);
Console.WriteLine(CB =+ lpDisplayDevice.cb);
Console.WriteLine(的DeviceID =+ lpDisplayDevice.DeviceID);
Console.WriteLine(DeviceKey =+ lpDisplayDevice.DeviceKey);
Console.WriteLine(设备名称=+ lpDisplayDevice.DeviceName.Trim());
Console.WriteLine(DeviceString =+ lpDisplayDevice.DeviceString.Trim());
//显示监视器名称:
DISPLAY_DEVICE monitor_name =新DISPLAY_DEVICE(0); // OUT

个;>>>>>>>>>>下一行有错误:其中;<<<<<<<<<<<<<<
EnumDisplayDevices(lpDisplayDevice.DeviceName,0,参考monitor_name,0); <<<<<<<<<<<<<<<<< lpDisplayDevice.DeviceName会导致错误<<<<<<
^^^^^^^^^^^^^^^^^^^^^^^^^^ - 导致错误<<<<<<

Console.WriteLine(监视器名称=+ lpDisplayDevice.DeviceString.Trim());

++ DEVNUM;
}
}


解决方案

的问题是,在申报的私人静态外部布尔EnumDisplayDevices()。我改变了lpDevice从IntPtr的字符串,它为我的作品



下面的源代码在输出窗口中的以下内容:



DEVNUM = 0
CB = 424
的DeviceID = PCI\VEN_10DE&放大器; DEV_0402&安培; SUBSYS_0440196E&安培; REV_A1
DeviceKey = \Registry\Machine\System\CurrentControlSet \Control\Video {B757E378-AF2E-4826-BA9E-927C5B81B139} \0000
设备名称= \.\DISPLAY1
DeviceString =的NVIDIA GeForce 8600 GT
线程'RunPowerStatusMonitor主题(0x2108)已与代码0(为0x0)退出。
监视器名称=通用即插即用监视器



DEVNUM = 1
CB = 424
的DeviceID = PCI\VEN_10DE&放大器; DEV_0402&安培; SUBSYS_0440196E&安培; REV_A1
DeviceKey = {\Registry\Machine\System\CurrentControlSet\Control\Video B757E378-AF2E-4826-BA9E-927C5B81B139} \0001
设备名称= \.\ DISPLAY2
DeviceString = NVIDIA的GeForce 8600 GT
监视器名称=通用即插即用监视器

 公共静态无效show_display_devices( )
{
DISPLAY_DEVICE lpDisplayDevice =新DISPLAY_DEVICE(0); // OUT
DISPLAY_DEVICE monitor_name =新DISPLAY_DEVICE(0); // OUT

INT DEVNUM = 0;
,而(EnumDisplayDevices(NULL,DEVNUM,参考lpDisplayDevice,0))
{

Console.WriteLine(\\\
devNum =+ DEVNUM);
Console.WriteLine(CB =+ lpDisplayDevice.cb);
Console.WriteLine(的DeviceID =+ lpDisplayDevice.DeviceID);
Console.WriteLine(DeviceKey =+ lpDisplayDevice.DeviceKey);
Console.WriteLine(设备名称=+ lpDisplayDevice.DeviceName.Trim());
Console.WriteLine(DeviceString =+ lpDisplayDevice.DeviceString.Trim());
//显示监视器名称:



EnumDisplayDevices(lpDisplayDevice.DeviceName,0,参考monitor_name,0);
Console.WriteLine(监视器名称=+ monitor_name.DeviceString.Trim());

++ DEVNUM;
}
}

函数[DllImport(User32.dll中)]
私人静态外部布尔EnumDisplayDevices(
串lpDevice,INT iDevNum,
REF DISPLAY_DEVICE lpDisplayDevice,诠释的dwFlags);




[StructLayout(LayoutKind.Sequential)]
公共结构DISPLAY_DEVICE
{
公众诠释CB;
[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 32)]
公共字符串设备名称;
[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串DeviceString;
公众诠释StateFlags;
[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串的DeviceID;
[的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串DeviceKey;

公共DISPLAY_DEVICE(INT标志)
{
CB = 0;
StateFlags =标志;
设备名称=新的字符串((CHAR)32,32);
DeviceString =新的字符串((char)的32,128);
的DeviceID =新的字符串((char)的32,128);
DeviceKey =新的字符串((char)的32,128);
CB = Marshal.SizeOf(本);
}
}


I'm trying to make a function that displays all display and monitor names.But the 2nd time I call enumDisplayDevices, there's an error if the first argument is lpDisplayDevice.DeviceName. I know it's the first argument becuase if it's set to "IntPtr.Zero" instead, there is no error.

I cannot find another example of how to do this in C#.

    public static void show_display_devices()
    {
        DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0);     // OUT
        int devNum = 0;
        while(true)
        {
            bool success = EnumDisplayDevices( IntPtr.Zero,  devNum,  ref lpDisplayDevice,  0);
            if( !success )
                break;

            Console.WriteLine("\ndevNum =" + devNum);
            Console.WriteLine("cb =" + lpDisplayDevice.cb);
            Console.WriteLine("DeviceID =" + lpDisplayDevice.DeviceID);
            Console.WriteLine("DeviceKey =" + lpDisplayDevice.DeviceKey);
            Console.WriteLine("DeviceName =" + lpDisplayDevice.DeviceName.Trim());
            Console.WriteLine("DeviceString =" + lpDisplayDevice.DeviceString.Trim());
            // Show monitor name:
            DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0);        // OUT

               >>>>>>>>>>>   NEXT LINE HAS ERROR:  <<<<<<<<<<<<<<<
            EnumDisplayDevices(lpDisplayDevice.DeviceName, 0, ref monitor_name, 0);  <<<<<<<<<<<<<<<<<  "lpDisplayDevice.DeviceName" causes error  <<<<<<
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^ - CAUSES ERROR <<<<<<

            Console.WriteLine("Monitor name =" + lpDisplayDevice.DeviceString.Trim());

            ++devNum;
        }
    }
解决方案

The problem was in the declaration "private static extern bool EnumDisplayDevices()". I changed lpDevice from IntPtr to string, and it works for me.

The source code below produces the following in the Output window:

devNum =0cb =424DeviceID =PCI\VEN_10DE&DEV_0402&SUBSYS_0440196E&REV_A1DeviceKey =\Registry\Machine\System\CurrentControlSet\Control\Video{B757E378-AF2E-4826-BA9E-927C5B81B139}\0000DeviceName =\.\DISPLAY1DeviceString =NVIDIA GeForce 8600 GTThe thread 'RunPowerStatusMonitor Thread' (0x2108) has exited with code 0 (0x0).Monitor name =Generic PnP Monitor

devNum =1cb =424DeviceID =PCI\VEN_10DE&DEV_0402&SUBSYS_0440196E&REV_A1DeviceKey =\Registry\Machine\System\CurrentControlSet\Control\Video{B757E378-AF2E-4826-BA9E-927C5B81B139}\0001DeviceName =\.\DISPLAY2DeviceString =NVIDIA GeForce 8600 GTMonitor name =Generic PnP Monitor

        public static void show_display_devices()
    {
        DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0);     // OUT
            DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0);        // OUT

        int devNum = 0;
        while (EnumDisplayDevices(null, devNum, ref lpDisplayDevice, 0))
        {

            Console.WriteLine("\ndevNum =" + devNum);
            Console.WriteLine("cb =" + lpDisplayDevice.cb);
            Console.WriteLine("DeviceID =" + lpDisplayDevice.DeviceID);
            Console.WriteLine("DeviceKey =" + lpDisplayDevice.DeviceKey);
            Console.WriteLine("DeviceName =" + lpDisplayDevice.DeviceName.Trim());
            Console.WriteLine("DeviceString =" + lpDisplayDevice.DeviceString.Trim());
            // Show monitor name:



            EnumDisplayDevices( lpDisplayDevice.DeviceName, 0, ref monitor_name, 0);
            Console.WriteLine("Monitor name =" + monitor_name.DeviceString.Trim());

            ++devNum;
        }
    }

    [DllImport("User32.dll")]
    private static extern bool EnumDisplayDevices(
        string lpDevice, int iDevNum,
        ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);




    [StructLayout(LayoutKind.Sequential)]
    public struct DISPLAY_DEVICE
    {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceString;
        public int StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceKey;

        public DISPLAY_DEVICE(int flags)
        {
            cb = 0;
            StateFlags = flags;
            DeviceName = new string((char)32, 32);
            DeviceString = new string((char)32, 128);
            DeviceID = new string((char)32, 128);
            DeviceKey = new string((char)32, 128);
            cb = Marshal.SizeOf(this);
        }
    }

这篇关于C#如何获得Windows监视器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:02