2012-08-29 28 views
0

您好我有一個C#應用程序和嵌入式瀏覽器,它的任務是找到一個鏈接,然後右鍵單擊它並單擊屬性!如何找到右鍵單擊屬性C#web瀏覽器的Contextmenu

鼠標以編程方式移動,所以我需要在右擊菜單中找到屬性!

你能幫我怎麼做嗎?

我試圖按'右'後右鍵單擊,但它沒有在一些計算機上工作!

所以我需要通過移動鼠標!

這裏是我找到一個鏈接,然後右鍵單擊代碼:

int x = getXoffset(link); 
int y = getYoffset(link); 
webBrowser1.Document.Window.ScrollTo(x, y); 
Linker.Win32.POINT p2 = new Linker.Win32.POINT(); 
webBrowser1.Focus(); 
p2.x = webBrowser1.Left + 10; 
p2.y = webBrowser1.Top + 5; 
Linker.Win32.ClientToScreen(this.Handle, ref p2); 
Linker.Win32.SetCursorPos(p2.x, p2.y); 
MouseOperations.GetCursorPosition(); 

MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown); 
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown); 
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp); 

爲達成右鍵點擊MENY性質的任何其他的想法表示歡迎

+0

或有什麼方法可以在右鍵單擊菜單中計數項目,然後選擇屬性的最後一個項目? –

+0

你到底想做什麼? – KF2

+0

正如我解釋我的應用程序找到一個鏈接,例如,當它加載www.example.com它找到鏈接命名:測試,然後右鍵點擊int,然後我想打開屬性 –

回答

0

使用此代碼:

[DllImport("user32.dll", SetLastError = true)] 
     static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); 
     public static void PressKey(Keys key, bool up) 
     { 
      const int KEYEVENTF_EXTENDEDKEY = 0x1; 
      const int KEYEVENTF_KEYUP = 0x2; 
      if (up) 
      { 
       keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); 
      } 
      else 
      { 
       keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      webBrowser1.Navigate("http://google.com");//Your link 
      webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
     } 

     void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      //Find your link and right click(automatically by your code) 
      webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown); 
     } 

     void Document_MouseDown(object sender, HtmlElementEventArgs e) 
     { 
      if (e.MouseButtonsPressed == MouseButtons.Right) 
      { 
       Thread.Sleep(1000); 
       PressKey(Keys.P, true); 
       PressKey(Keys.P, false); 
      } 
     } 
+0

tanx它幫了很多 –

相關問題