我想编写一些有趣的代码来在 Windows 7 上颠倒方向。请参阅我要控制的选项的屏幕截图。
这是我的代码:
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
{
}
}
}
它运行,但不会产生任何影响。
参考代码:http: //justlikeamagic.com/2009/05/21/changeing-display-settings-programmatically/ 和http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_chngingdisplay