2012-06-18 3 views
7

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, 0); 
      ... 
     } 
     catch 
     { 
     } 
    } 
} 

: 여기

Monitor Orientation



는 내가 가지고있는 코드입니다.

참조 코드 : http://justlikeamagic.com/2009/05/21/changing-display-settings-programmatically/ 및 Windows 7에

+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

http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_chngingdisplay, ChangeDisplaySetting 알려진 호환성 문제가 있습니다. 해결 방법은 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

관련 문제