How can I force display detection in Windows?

懵懂的女人 提交于 2019-12-06 20:58:17

问题


I often boot my Windows 7 PC with the attached KVM switch focused on another computer. When I switch to the booted PC, the display resolution is wrong (and the second attached monitor is not detected).

I can correct this by right-clicking the desktop, choosing Screen Resolution and clicking Detect. This makes Windows detect attached displays and adjust to the most optimal resolution.

I would like to write a small utility to do this automatically. Which Win32 API call or C# object should I use?


回答1:


You can try:

  1. You can use Spy++ to search for the windows that are open and take a look at their properties and messages.
  2. Use process to start "rundll32.exe shell32.dll,Control_RunDLL desk.cpl" or experiment with calling it directly to see if you can get a window handle, check below link for ideas.
  3. Use the code "send button click to external app" and modify it to search for a window with caption "Screen Resolution" and send a BN_CLICK to the childwindow with the caption "Detect".
  4. Since the computer is already on you might want to fire it up automatically on logon, for that use the task scheduler.



回答2:


This will get you half-way there:

Execute: control.exe desk.cpl,Settings,@Settings

That will bring up the Screen Resolution panel directly.

I might also suggest a scripting tool like http://en.wikipedia.org/wiki/Windows_Script_Host And write a utility that'll open the panel and click the button.

Barring that it's possible that the control panel directly calls into a windows .dll which you can load and invoke in code directly, but that would require some sleuthing to detect. (you can start by running the .cpl in a debugger and see what happens when you click the detect).




回答3:


Why not just use Do It Again and write a macro that resets the resolution for you by recording your mouse and keyboard actions?




回答4:


Not sure if this will work for you, but you can try something like this. At least it can get you started.

[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 714)]
public struct DISPLAY_DEVICE
{
    [FieldOffset(0)]
    public int cb;
    [FieldOffset(4)]
    public char DeviceName;
    [FieldOffset(68)]
    public char DeviceString;
    [FieldOffset(324)]
    public int StateFlags;
    [FieldOffset(328)]
    public char DeviceID;
    [FieldOffset(584)]
    public char DeviceKey;
}

[DllImport("User32.dll", SetLastError = true)]
static extern Boolean EnumDisplayDevices(
        string lpDevice,
        uint iDevNum,
        ref DISPLAY_DEVICE lpDisplayDevice,
        uint dwFlags
);

public void DetectDevices()
{
    var flag = true;
    for (uint i = 0; flag && i < 100; i++)
    {
        var device = new DISPLAY_DEVICE();
        device.cb = Marshal.SizeOf(device);
        flag = EnumDisplayDevices(null, i, ref device, 1);
    }
}


来源:https://stackoverflow.com/questions/3858844/how-can-i-force-display-detection-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!