2012-06-18 19 views
7

Windows 7で向きを反転させるための楽しいコードを書きたいと思います。制御したいオプションのスクリーンショットを参照してください。Windows 7でモニタの向きを設定するにはどうすればよいですか?


Monitor Orientation


ここで私が持っているコードです:

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

動作しますが、任意の影響生成しません。

リファレンスコード:Windows 7の、ChangeDisplaySettinghttp://justlikeamagic.com/2009/05/21/changing-display-settings-programmatically/http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_chngingdisplay

+4

"*楽しいコードを書く*" - 私は実践的な冗談を嗅ぐ。 :) – qJake

答えて

1

私が何かを始めています。

ご覧下さい:MultiMonitorHelper

あなたはSetDisplayConfigや他の関数を呼び出すことができるようにそれは、Win7のために必要な構造を含みます。モニターを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, pathInfoArray, (uint) numModeInfoArrayElements, 
              modeInfoArray, SdcFlags.Apply | SdcFlags.UseSuppliedDisplayConfig); 
      } 
     } 

それは、「C#のスタイル」APIが今存在しないことを意味、今の生だが、どれも少なく、あなたはそれらの構造を使用することができます。

+0

シェアをありがとう。私は近いうちにもっと近く見ていきます。 – ja72

3

は、既知の互換性の問題があります。回避策は、WDK関数を呼び出すことです:SetDisplayConfig

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

+0

あなたは正しいパスにいるかもしれませんが、 'SetDisplayConfig'にアクセスして呼び出す方法のサンプルコードを追加できますか? – ja72

+0

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

+0

Robert、そのリンクを含めるように回答を編集することができます。 – GeorgePotter

関連する問題