加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:43:25 所属栏目:百科 来源:网络整理
导读:我想写一些有趣的代码在 Windows 7上翻转方向.请参阅我想要控制的选项的屏幕截图. 这是我的代码: class Program{ public const long WM_PAINT=0x0F; public const long WM_DISPLAYCHANGE=0x7E; [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
我想写一些有趣的代码在 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,但是,你可以使用这些结构.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读