2017-03-08 23 views
0

コード内のいくつかのシナリオでマウスの左クリックをシミュレートする必要がありました(c#.net)。 このシミュレーションは実行する必要がありますが、ユーザーにとっては不可視でなければなりません。左クリックが発生するとカーソル位置を保存します。左クリックをした後、カーソル位置をセーブ値に設定する必要があります。 問題があります。ターゲット上でマウスの左クリックをシミュレートし、C#で前の位置にマウスを移動します。

   int save_cursur_x = Cursor.Position.X; 
       int save_cursur_y = Cursor.Position.Y; 
       int current_node_x = newGraphEditor.getCurrentNodeGlobalPosition_x(); 
       int current_node_y = newGraphEditor.getCurrentNodeGlobalPosition_y(); 
       LeftMouseClick(current_node_x, current_node_y); 
       Cursor.Position = new Point(save_cursur_x, save_cursur_y); 

最後の行(Cursor.Position =新しいポイント(save_cursur_x、save_cursur_y))が削除されると左クリックは、目標位置に正確に起こります。

  //This is a replacement for Cursor.Position in WinForms 
      [System.Runtime.InteropServices.DllImport("user32.dll")] 
      static extern bool SetCursorPos(int x, int y); 

      [System.Runtime.InteropServices.DllImport("user32.dll")] 
      public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 

      public const int MOUSEEVENTF_LEFTDOWN = 0x02; 
      public const int MOUSEEVENTF_LEFTUP = 0x04; 

      //This simulates a left mouse click 
      public static void LeftMouseClick(int xpos, int ypos) 
      { 
       SetCursorPos(xpos, ypos); 
       mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); 
       mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); 
      } 

更新: があり、私はその行を追加する場合でも、この方法

これはLeftMouseClickで左クリックは、未知の位置で起こる(または多分左クリックは発生しません)選択委譲ターゲット位置が設定されている場所。このデリゲートは、ターゲットをクリックすると正確に実行されます。私は問題がここにあると思う。ターゲットを左クリックした後、このデリゲートは実行されません。カーソルの位置を前の値に設定した後、委任が実行されます。今カーソルがターゲット上にないので、その目標位置が正しく設定されていません。(その位置が変更されている)

ItemEventHandler<IModelItem> selectionDelegate = 

        delegate (object source, ItemEventArgs<IModelItem> args) 
        { 
         var tag = ((INode)(args.Item)).Tag; 
         if (tag is HostInnerNodeTag) 
         { 
          currentNodeGlobalPosition_x = Cursor.Position.X; 
          currentNodeGlobalPosition_y = Cursor.Position.Y; 
         } 
        } 

答えて

0

をあなたが代わりのSendMessageを使用することができます。

[DllImport("user32.dll")] 
    public static extern int SendMessage(
         IntPtr hWnd,  // handle to destination window 
         uint Msg,  // message 
         IntPtr wParam, // first message parameter 
         IntPtr lParam // second message parameter 
        ); 

    public const uint WM_LBUTTONDOWN = 0x0201; 
    public const uint WM_LBUTTONUP = 0x0202;  

    //This simulates a left mouse click 
    public static void LeftMouseClick(IntPtr hwnd, uint xpos, uint ypos) 
    { 
     SendMessage(hwnd, WM_LBUTTONDOWN, new IntPtr(xpos), new IntPtr(ypos)); 
     SendMessage(hwnd, WM_LBUTTONUP, new IntPtr(xpos), new IntPtr(ypos)); 
    } 

    private void send() 
    {    
     LeftMouseClick(newGraphEditor.Handle, 10, 10); 
    } 
+0

スレッドに問題があると思います。間違った位置ではない。私はカーソルの移動と左クリックが矛盾していると思います。 –

+0

このコードを使用しようとしましたか?mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE、xpos、ypos、0,0); – user3811082

+0

tagINPUTとは何ですか? –

関連する問題