2016-08-06 21 views
1

SendInputを使用したマウス刺激はMainDisplayで完全に機能します。しかし、私がSendInputを拡張画面用に使用すると(例えば、私の場合、メインディスプレイの左側に2番目の画面が表示されますが、メインディスプレイの周りの拡張ディスプレイに関係なく問題は再現可能ですが、メインディスプレイの解像度は異なります): SendInputを拡張画面で使用すると、マウスの位置はXとYの両方の位置でオフセットされます(拡張画面のX(幅)とY(高さ)に基づいて、xが40〜80ポイント、Yが10〜メインディスプレイ幅/高さ)拡張画面SendInputが拡張画面で完全に機能しない

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool GetCursorPos(ref Win32Point pt); 

[DllImport("user32.dll")] 
internal static extern bool SetCursorPos(int X, int Y); 

[StructLayout(LayoutKind.Sequential)] 
internal struct Win32Point 
{ 
    public Int32 X; 
    public Int32 Y; 
}; 

internal enum SendInputEventType : int 
{ 
    InputMouse, 
    InputKeyboard 
} 

[DllImport("user32.dll", SetLastError = true)] 
    private static extern uint SendInput(uint nInputs, ref Input pInputs, int cbSize); 

public struct Input 
{ 
    public uint InputType; 
    public MouseInput MI; 
} 

public struct MouseInput 
{ 
    public int Dx; 
    public int Dy; 
    public uint MouseData; 
    public uint DwFlags; 
    public uint Time; 
    public IntPtr DwExtraInfo; 
} 

public enum MouseEventInfo 
{ 
    mouseEventfMove = 0x0001, 
    mouseEventfLeftdown = 0x0002, 
    mouseEventfLeftup = 0x0004, 
    mouseEventfRightdown = 0x0008, 
    mouseEventfRightup = 0x0010, 
    mouseEventfWheel = 0x0800, 
    mouseEventfAbsolute = 0x8000, 
    wheelDelta = 0x0078 
} 

static int CalculateAbsoluteCoordinateX(int x, System.Drawing.Rectangle currentBounds) 
    { 
     return ((currentBounds.X + x) * 65536)/(currentBounds.Width); 
    } 

    static int CalculateAbsoluteCoordinateY(int y, System.Drawing.Rectangle currentBounds) 
    { 
     return (((currentBounds.Y + y) * 65536)/currentBounds.Height); 
    } 

// for me screen at index 0 (screen no 1) is main display. Screen id 2 
//placed to the left of the main display as per resolution screen i.e.at 
//index 1 (Screen.AllScreens[1]) is extended display and Bound.X is a -ve value 
    public static int ScreenId = 2; 

    public static System.Drawing.Rectangle CurrentBounds 
    { 
     get 
     { 
      return SysForms.Screen.AllScreens[ScreenId - 1].Bounds; 
     } 
    } 

    public static void ClickLeftMouseButton(int x, int y) 
    { 
     Input mouseInput = new Input(); 
     mouseInput.InputType = SendInputEventType.InputMouse; 
     mouseInput.MI.Dx = CalculateAbsoluteCoordinateX(x, CurrentBounds); 
     mouseInput.MI.Dy = CalculateAbsoluteCoordinateY(y, CurrentBounds); 
     mouseInput.MI.MouseData = 0; 

     mouseInput.MI.DwFlags = MouseEventInfo.mouseEventfMove | MouseEventInfo.mouseEventfAbsolute; 
     SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); 

     mouseInput.MI.DwFlags = MouseEventInfo.mouseEventfLeftdown; 
     SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); 

     mouseInput.MI.DwFlags = MouseEventFlags.mouseEventfLeftup; 
     SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); 
    } 

//Below is code of the WPF MainWindow for testing. Two buttons with click  event. 
// For main display with screenid as 1 both setcursor position and sendinput 
//work perfectly, as I get the MousePosition, but when I apply this to 
//extended screen (currently with two screen, main display is screen 1 in my   
//case and screen 2 is extended screen, they put the mouse at two different positions. 
//I have my doubts the way I am using the extended screen Bounds.X, but 
//haven't will able to fix the issue 

    int x = 600; 
    int y = 300; 

    private void btnSend_Click(object sender, RoutedEventArgs e) 
    { 
     SetCursorPos(SysForms.Screen.AllScreens[ScreenId - 1].Bounds.X + x, SysForms.Screen.AllScreens[screenId - 1].Bounds.Y + y);    
    } 

    private void btnSend1_Click(object sender, RoutedEventArgs e) 
    { 
     ClickLeftMouseButton(x, y); 
    } 
+0

ドロップダウン、私は本当にあなたの質問に答えることはできませんが、私は両方の画面にまたがるウィンドウがコンテキストメニューでひどい時間を持っていること(2つの異なる解像度を持っている)私の仕事のコンピュータ上で気づきましたボックスなどが正しく整列され、正しくサイジングさえできます。 – leetibbett

答えて

0

上の違いが問題を発見理由としてどんな支援を事前に

おかげで異なっています。 SendInputを使用している間、絶対値でのx、yの変換は、Main/Primary画面に対して行わなければなりません。 このように変更:

static int CalculateAbsoluteCoordinateX(int x, System.Drawing.Rectangle currentBounds) 
{ 
    return ((currentBounds.X + x) * 65536)/(SystemParameters.PrimaryScreenWidth); 
} 

static int CalculateAbsoluteCoordinateY(int y, System.Drawing.Rectangle currentBounds) 
{ 
    return (((currentBounds.Y + y) * 65536)/SystemParameters.PrimaryScreenHeight); 
} 
関連する問題