本文介绍了如何设置显示器的方向在Windows 7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一些有趣code翻转的方向上我想控制的选项的Windows 7见截图倒挂。
I want to write some fun code to flip the orientation upside down on Windows 7. See screen shot of the option I want to control.
下面是code我有:
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
{
}
}
}
它运行,但不产生任何影响。
which runs, but does not produce any affects.
参考code:
和的
推荐答案
我已经开始的东西。
请看看:
它包括Win7的必要的结构,这样就可以调用SetDisplayConfig等功能。
It includes necessary structures for Win7, so that you could call SetDisplayConfig and other functions.
这是实际的例子,如何旋转显示器90度:
An actual example, how to rotate monitor 90 degrees:
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的权利,但没有少,你可以使用这些结构。
it's raw right now, meaning there is no "C# style" API right now, but none the less, you can use those structures.
这篇关于如何设置显示器的方向在Windows 7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!