问题
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:
- You can use Spy++ to search for the windows that are open and take a look at their properties and messages.
- 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.
- 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".
- 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