示例#1
0
        private Int32 GetCurrentDisplayOrientation()
        {
            NativeMethods.DEVMODE devmode = new NativeMethods.DEVMODE
            {
                dmDeviceName = new String(new char[32]),
                dmFormName   = new String(new char[32])
            };
            devmode.dmSize = (short)Marshal.SizeOf(devmode);

            if (0 != NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref devmode))
            {
                return(devmode.dmDisplayOrientation);
            }

            return(DMDO_UNKNOWN);
        }
示例#2
0
        private void UpdateDisplayOrientation()
        {
            if ((this.currentOrientation == this.desiredOrientation) ||
                (this.desiredOrientation == DMDO_UNKNOWN) ||
                (this.currentOrientation == DMDO_UNKNOWN))
            {
                // Do not attempt a change if either state is unknown or if we're already in the desired state
                return;
            }

            NativeMethods.DEVMODE devmode = new NativeMethods.DEVMODE
            {
                dmDeviceName = new String(new char[32]),
                dmFormName   = new String(new char[32])
            };
            devmode.dmSize = (short)Marshal.SizeOf(devmode);

            if (0 != NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref devmode))
            {
                if (((currentOrientation == NativeMethods.DMDO_DEFAULT) && (desiredOrientation != NativeMethods.DMDO_180)) ||
                    ((currentOrientation == NativeMethods.DMDO_180) && (desiredOrientation != NativeMethods.DMDO_DEFAULT)) ||
                    ((currentOrientation == NativeMethods.DMDO_90) && (desiredOrientation != NativeMethods.DMDO_270)) ||
                    ((currentOrientation == NativeMethods.DMDO_270) && (desiredOrientation != NativeMethods.DMDO_90)))
                {
                    // When rotating 90 degrees we need to swap the height/width
                    int temp = devmode.dmPelsHeight;
                    devmode.dmPelsHeight = devmode.dmPelsWidth;
                    devmode.dmPelsWidth  = temp;
                }

                devmode.dmDisplayOrientation = this.desiredOrientation;

                if (0 != NativeMethods.ChangeDisplaySettings(ref devmode, 0))
                {
                    MessageBox.Show("Rotating display failed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }