2012-10-13 5 views
4

저는 Teamviewer를 재미있는 소프트웨어로 만들려고합니다. 한 사람이 다른 사람의 화면을보고 클릭하면됩니다. 어쨌든 대부분의 소켓 작업은 끝났지 만 마우스 클릭이 올바르게 작동하는 방법을 모르겠습니다. 여기에 프로그래밍 방식으로 마우스를 이동 온라인으로 볼 수있는 코드 :C#으로 움직이는 마우스 (좌표 단위)

public static class VirtualMouse 
{ 
    // import the necessary API function so .NET can 
    // marshall parameters appropriately 
    [DllImport("user32.dll")] 
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); 

    // constants for the mouse_input() API function 
    private const int MOUSEEVENTF_MOVE = 0x0001; 
    private const int MOUSEEVENTF_LEFTDOWN = 0x0002; 
    private const int MOUSEEVENTF_LEFTUP = 0x0004; 
    private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; 
    private const int MOUSEEVENTF_RIGHTUP = 0x0010; 
    private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; 
    private const int MOUSEEVENTF_MIDDLEUP = 0x0040; 
    private const int MOUSEEVENTF_ABSOLUTE = 0x8000; 


    // simulates movement of the mouse. parameters specify changes 
    // in relative position. positive values indicate movement 
    // right or down 
    public static void Move(int xDelta, int yDelta) 
    { 
     mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0); 
    } 


    // simulates movement of the mouse. parameters specify an 
    // absolute location, with the top left corner being the 
    // origin 
    public static void MoveTo(int x, int y) 
    { 
     mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0); 
    } 


    // simulates a click-and-release action of the left mouse 
    // button at its current position 
    public static void LeftClick() 
    { 
     mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
     mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
    } 
} 

가 지금은 moveTo 메소드를 사용하여 마우스를 이동할는하지만, 움직임이 미친 높은 번호가 필요합니다. 화면상의 위 치로 픽셀 단위로 이동하는 좌표를 일치시킬 수 있습니까? 죄송합니다 이것은 명백한 질문처럼 보이지만 거의 한 시간 동안 봤는데 봤는데 어떤 단위가 마우스 x 및 y 위치에 사용되고 있는지에 대한 논의를 찾을 수 없으므로 어떤 종류의 수식을 사용하여 한 패널의 클릭 수와 사용자의 클릭 수를 비교합니다. Microsoft's documentation 가입일

+3

['Cursor.Position'] (http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx)는 유망 해 보입니다. – chris

답변

5

는 : MOUSEEVENTF_ABSOLUTE 값이 지정된

경우 DX 및 DY은 0과 65,535 사이의 정규화 절대 좌표를 포함한다. 이벤트 절차는 이러한 좌표를 디스플레이 표면에 매핑합니다. 좌표 (0,0)은 디스플레이 표면의 왼쪽 위 모서리에 매핑되며 (65535,65535)는 오른쪽 아래 모서리에 매핑됩니다.

이처럼 원하는 값으로 픽셀 단위로 입력을 변환하는 것을 사용할 수 있습니다 명심하십시오

var inputXinPixels = 200; 
var inputYinPixels = 200; 
var screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; 
var outputX = inputXinPixels * 65535/screenBounds.Width; 
var outputY = inputYinPixels * 65535/screenBounds.Height; 
MoveTo(outputX, outputY); 

이 복수의 모니터에 대한 정확하지 않을 수있다. 또한 문서에 다음과 같이 표시되어 있습니다.

이 기능이 대체되었습니다. 대신 SendInput을 사용하십시오.

부록 : 위의 공식은 최선을하지 않을 수 있습니다 pointed by J3soon으로. research done for AutoHokey을 바탕으로 내부에 다음 코드는 잘 작동 :

var outputX = (inputXinPixels * 65536/screenBounds.Width) + 1; 
var outputY = (inputYinPixels * 65536/screenBounds.Height) + 1; 

는 참조 용으로 AutoHotkey source code를 참조하십시오.


내가 귀하의 위치에 있었다면 Cursor.Position을 사용합니다. 다음 코드는 예상대로 작동합니다

System.Windows.Forms.Cursor.Position = new System.Drawing.Point(200, 200); 

예, 그것은 좌표 (200, 200) 화면의 픽셀 [LinqPad에서 테스트]에 마우스 포인터를 배치합니다.

부록 : 적어도 System.Windows.Forms.Cursor.Position은 내부적으로 어떤 용도로 사용되는지 살펴 보았습니다. SetCursorPos에 대한 호출입니다. 이상한 좌표 변환이 필요하지 않습니다.

+0

'InputXinPixels * 65536/SCREEN_WIDTH + 1'이 나의 경우에 더 잘 작동하며 [AutoHotKey] (https://github.com/Lexikos/AutoHotkey_L/blob/58842fb2956fe082bc11476316d4590b0e2ee8a8/source/keyboard_mouse.cpp#L2545)에서도 사용됩니다. – J3soon

관련 문제