2011-12-19 148 views
4

所以我試圖模擬鼠標左鍵單擊和左鼠標釋放做一些自動拖放。試圖模擬鼠標點擊/拖動

它目前在C#Winforms(是,winforms:|),並且有點像鵝。

基本上,一旦點擊被髮送,我希望它根據Kinect輸入更新光標位置。 Kinect的一面是好的,但我不知道如何找到按鈕是否仍然按下。

這裏是我正在使用的代碼+一些psuedocode來幫助更好地解釋我自己(做的同時)。

class MouseImpersonator 
{ 
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); 

    private const int leftDown = 0x02; 
    private const int leftUp = 0x04; 

    public static void Grab(int xPos, int yPos) 
    { 
     Cursor.Position = new Point(xPos + 25, yPos + 25); 
     mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0); 

     //do 
     //{ 
     //Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY); 
     //} while (the left mouse button is still clicked); 
    } 

    public static void Release(int xPos, int yPos) 
    { 
     Cursor.Position = new Point(xPos + 25, yPos + 25); 
     mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0); 
    } 
} 

我已經有谷歌的追捕而無法找到我所需要的只是一個WPF相當於什麼:http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx

我有點出我的深度,但任何幫助非常感謝。

盧卡斯。

    -
+0

對於名稱空間是WPF的一部分的信息。當您輸入發佈版時,是否嘗試將布爾變量設置爲true時輸入Grab和False? – 2011-12-19 13:10:45

+0

偷偷摸摸的WPF,感謝Mark,已經調整了這個問題。我想過布爾方法,但認爲可能會有更優雅的東西。如果我/任何人都不能想到另一種方法,它會工作,只是試圖避免總是採取簡單的方式:) – Lucas 2011-12-19 13:21:50

+0

聲明是錯誤的,最後一個參數是IntPtr。通過IntPtr.Zero。 – 2011-12-19 15:31:41

回答

2

最簡單的答案是事實上使用布爾,只是檢查看看發生了什麼。

我在一個新的線程上啓動它,所以它沒有打破所有其他事情。

理想情況下,你會稍微收拾一點。

public static void Grab(int xPos, int yPos) 
    { 
     _dragging = true; 

     Cursor.Position = new Point(xPos, yPos + offSet); 
     mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0); 

     var t = new Thread(CheckMouseStatus); 
     t.Start(); 
    } 
    public static void Release(int xPos, int yPos) 
    { 
     _dragging = false; 
     Cursor.Position = new Point(xPos, yPos + offSet); 
     mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0); 
    } 

    private static void CheckMouseStatus() 
    { 
     do 
     { 
      Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet); 
     } 
     while (_dragging); 
    } 
0

如果左邊鼠標按鍵時下面的代碼應返回true,否則爲false時,控制被System.Windows.Forms.Control的:

Control.MouseButtons.HasFlag(MouseButtons.Left) 

附:這個文件可以找到on MSDN here