2013-10-22 13 views
0

我在此處找到了此代碼,但是當我嘗試將其實施到我的代碼中時,它不起作用。我不是專業的編碼員,因此請你們幫忙。 它是一個參考問題?還是有其他方法讓光標點擊?無法獲取鼠標座標,但所有代碼都來自並且已經爲其他人工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace MouseWPF 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

     } 
    } 
    public class form1 : Grid 
    { 
     [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
     public static extern void mouse_event(long dwflags, long dx, long dy, long cButtons, long dwExtraInfo); 
     private const int MOUSEEVENTF_LEFTDOWN = 0x02; 
     private const int MOUSEEVENTF_LEFTUP = 0x04; 
     private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 
     private const int MOUSEEVENTF_RIGHTUP= 0x10; 
     public static Point Position { get; set; } 
     public form1() 
     { 

     } 
     public void DoMouseClick() 
     { 
//This is where the error is, System.Windows.Input.Cursor' does not contain a definition for 'Position' // 
      int X = Cursor.Position.X; 
      int Y = Cursor.Position.Y; 
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 

     } 
    } 
} 
+1

究竟發生了什麼?它會給你0,0嗎?它會產生錯誤嗎? –

+1

「它不起作用」 - 這不夠精確。請完全描述*發生了什麼。與插入代碼之前相比,什麼都沒有?錯誤消息(哪一個?)?一個例外(哪一個?)?還有什麼不是預期的(什麼?)? –

+0

錯誤'System.Windows.Input.Cursor'不包含'Position'的定義,並且沒有找到接受'System.Windows.Input.Cursor'類型的第一個參數的擴展方法'Position'缺少使用指令或程序集引用?) –

回答

0

我在開發winforms遊戲編輯器時遇到了同樣的問題。我通過在想要獲取鼠標座標的窗體上創建一個onMouseMove事件來修復它。如果您不知道如何製作活動,請轉到表單編輯器,點擊表單,然後點擊屬性框中的雷電。繼續,並將它的名字寫在鼠標上,然後這裏有一些代碼來演示如何獲得正確的座標。

private void onMove(object sender, MouseEventArgs e) 
    { 
     label1.Text = "X:" + e.X + "Y:" + e.Y; 
    } 
+0

由於某些原因,Cursor屬性給出了與屏幕實際大小相關的鼠標座標,而不是應用程序中的座標。 –

+0

這就是我想要的^^ –