2012-06-18 9 views
7

Quiero escribir un código divertido para darle la vuelta a la orientación en Windows 7. Ver la captura de pantalla de la opción que deseo controlar.¿Cómo configuro la orientación del monitor en Windows 7?


Monitor Orientation


Aquí está el código que tengo:

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 
     { 
     } 
    } 
} 

que corre, pero no produce efectos.

Código de referencia: http://justlikeamagic.com/2009/05/21/changing-display-settings-programmatically/ y http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_chngingdisplay

+4

"* escribe un código divertido *" - Huelo una broma práctica. :) – qJake

Respuesta

1

He comenzado algo.

favor, eche un vistazo: MultiMonitorHelper

Incluye estructuras necesarias para Win7, por lo que se podría llamar SetDisplayConfig y otras funciones.

Un ejemplo real, como girar la pantalla 90 grados:

 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); 
      } 
     } 

es crudo en este momento, es decir, no hay ninguna API "C# del estilo de" en este momento, pero, no obstante, puede utilizar esas estructuras.

+0

Gracias por compartir. Voy a echar un vistazo más de cerca pronto. – ja72

3

En Windows 7, ChangeDisplaySetting tiene un problema de compatibilidad conocido. La solución consiste en llamar a la función WDK: SetDisplayConfig.

http://social.msdn.microsoft.com/Forums/en/windowsuidevelopment/thread/5bc2396d-1e0e-44fb-b73b-95f8dfc45684

+0

Puede que esté en el camino correcto, pero ¿puede agregar algún código de ejemplo sobre cómo acceder y llamar a 'SetDisplayConfig'? – ja72

+0

http://social.msdn.microsoft.com/Forums/en-US/wdk/thread/099741a3-cc7b-45f3-bcb1-1c6dd368a35c –

+0

Robert, es posible que desee editar su respuesta para incluir ese enlace. – GeorgePotter

Cuestiones relacionadas