C# 如何在Windows 7中设置显示器方向?
我想写一些有趣的代码来翻转Windows7上的方向。请参阅我要控制的选项的屏幕截图C# 如何在Windows 7中设置显示器方向?,c#,screen-orientation,C#,Screen Orientation,我想写一些有趣的代码来翻转Windows7上的方向。请参阅我要控制的选项的屏幕截图 以下是我的代码: class Program { public const long WM_PAINT=0x0F; public const long WM_DISPLAYCHANGE=0x7E; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct DEVMODE // take
以下是我的代码:
class Program
{
public const long WM_PAINT=0x0F;
public const long WM_DISPLAYCHANGE=0x7E;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct DEVMODE // taken from Win API
{
...
public System.Windows.Forms.ScreenOrientation dmDisplayOrientation;
}
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public static extern int ChangeDisplaySettings(ref DEVMODE lpDevMode, int dwFlags);
[DllImport("User32.Dll")]
public static extern long PostMessage(IntPtr hWnd, long wMsg, long wParam, long lParam);
static void Main(string[] args)
{
ScreenOrientation ori=ScreenOrientation.Angle0;
DEVMODE mode=new DEVMODE()
{
dmSize=(short)Marshal.SizeOf(typeof(DEVMODE)),
dmDriverExtra=0,
dmDeviceName=new string(new char[32]),
dmFormName=new string(new char[32]),
};
try
{
EnumDisplaySettings(null, -1, ref mode);
if((mode.dmFields&0x80)>0)
{
ori=mode.dmDisplayOrientation;
}
mode.dmDisplayOrientation=ScreenOrientation.Angle270;
int temp=mode.dmPelsWidth;
mode.dmPelsWidth=mode.dmPelsHeight;
mode.dmPelsHeight=temp;
int ret=ChangeDisplaySettings(ref mode, 0);
PostMessage(Process.GetCurrentProcess().Handle, WM_DISPLAYCHANGE, 0, 0);
...
}
catch
{
}
}
}
运行,但不产生任何影响
参考代码:
在Windows 7上,
ChangeDisplaySetting
存在已知的兼容性问题。解决方法是调用WDK函数:SetDisplayConfig
我已经开始做一些事情了 请看一看: 它包括Win7必需的结构,以便您可以调用SetDisplayConfig和其他函数 一个实际示例,如何将监视器旋转90度:
int numPathArrayElements;
int numModeInfoArrayElements;
const QueryDisplayFlags pathType = QueryDisplayFlags.OnlyActivePaths;
// query active paths from the current computer.
// note that 0 is equal to SUCCESS!
// TODO; HARDCODE MAGIC VALUES AWAY.
if (CCDWrapper.GetDisplayConfigBufferSizes(pathType, out numPathArrayElements,
out numModeInfoArrayElements) == 0)
{
var pathInfoArray = new DisplayConfigPathInfo[numPathArrayElements];
var modeInfoArray = new DisplayConfigModeInfo[numModeInfoArrayElements];
// TODO; FALLBACK MECHANISM THAT HANDLES DIFFERENT VALUES FOR ZERO.
if (CCDWrapper.QueryDisplayConfig(
pathType,
ref numPathArrayElements, pathInfoArray,
ref numModeInfoArrayElements,
modeInfoArray, IntPtr.Zero) == 0)
{
pathInfoArray[0].targetInfo.rotation = DisplayConfigRotation.Rotate90;
CCDWrapper.SetDisplayConfig((uint) numPathArrayElements, pathInfoArray, (uint) numModeInfoArrayElements,
modeInfoArray, SdcFlags.Apply | SdcFlags.UseSuppliedDisplayConfig);
}
}
它现在是原始的,这意味着现在没有“C风格”的API,但仍然可以使用这些结构。“编写一些有趣的代码”-我闻到了一个恶作剧的味道。:)您可能选择了正确的路径,但能否添加一些有关如何访问和调用
SetDisplayConfig
的示例代码?Robert,您可能希望编辑答案以包含该链接。上面的链接是相同的问题,但不是答案。搜索pinvoke.net
中的SetDisplayConfig
没有任何结果。感谢分享。我很快会仔细看看。