微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在Windows 7中设置显示器方向?

我想写一些有趣的代码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);
            ...
        }
        catch
        {
        }
    }
}

哪个运行,但不会产生任何影响.

参考代码http://justlikeamagic.com/2009/05/21/changing-display-settings-programmatically/
http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_chngingdisplay

解决方法

我已经开始了.

请看看:MultiMonitorHelper

它包含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,(uint) numModeInfoArrayElements,SdcFlags.Apply | SdcFlags.UseSupplieddisplayConfig);
            }
         }

它现在是原始的,这意味着现在没有“C#style”API,但是,你可以使用这些结构.

原文地址:https://www.jb51.cc/csharp/99285.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐