2013-04-20 83 views
1

我在Visual Studio上編寫了一些代碼,以便從網頁獲取一些信息。 現在我需要打開一個HTML元素的上下文菜單,我一直在尋找類似的東西:C#webbrowser - 觸發器右擊

webbrowser.Navigate("javascript: document.getElementsByClassName('className')[1].rightclick();void(0);"); 

但不幸的是,我注意到,只有點擊()函數在Javascript中存在。 有什麼解決方法嗎?

謝謝大家!

+0

你想打開你的元素上下文菜單? – KF2 2013-04-20 11:03:07

+0

是的,這是我想要的! :) – brutus8890 2013-04-20 11:18:45

回答

2

您可以用流動的步驟做:

  1. 獲取web-browser control的窗體上的位置:

    Point controlLoc = this.PointToScreen(webBrowser1.Location); 
    
  2. 獲取位置的HtmlElement上的Web瀏覽器控制

    X= element.OffsetRectangle.Left; 
    
    Y =element.OffsetRectangle.Top; 
    
  3. 總計你的位置S:

    controlLoc.X = controlLoc.X + element.OffsetRectangle.Left; 
    
    controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top; 
    
  4. 將鼠標定位到新的位置:

    Cursor.Position = controlLoc; 
    
  5. 模擬鼠標右鍵點擊:

    MouseSimulator.ClickRightMouseButton(); 
    

完整代碼:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

      webBrowser1.DocumentText = "<button class=\"mybtn\" type=\"submit\"> Right click"; 
      webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
     } 

     void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("button")) 
      { 
       if (element.GetAttribute("ClassName") == "mybtn") 
       { 
        Point controlLoc = this.PointToScreen(webBrowser1.Location); 
        //Get Element Posation 
        controlLoc.X= controlLoc.X + element.OffsetRectangle.Left; 
        controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top; 
        Cursor.Position = controlLoc; 
        MouseSimulator.ClickRightMouseButton(); 
       } 
      } 
     } 
    } 

    public class MouseSimulator 
    { 
     [DllImport("user32.dll", SetLastError = true)] 
     static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); 

     [StructLayout(LayoutKind.Sequential)] 
     struct INPUT 
     { 
      public SendInputEventType type; 
      public MouseKeybdhardwareInputUnion mkhi; 
     } 
     [StructLayout(LayoutKind.Explicit)] 
     struct MouseKeybdhardwareInputUnion 
     { 
      [FieldOffset(0)] 
      public MouseInputData mi; 

      [FieldOffset(0)] 
      public KEYBDINPUT ki; 

      [FieldOffset(0)] 
      public HARDWAREINPUT hi; 
     } 
     [StructLayout(LayoutKind.Sequential)] 
     struct KEYBDINPUT 
     { 
      public ushort wVk; 
      public ushort wScan; 
      public uint dwFlags; 
      public uint time; 
      public IntPtr dwExtraInfo; 
     } 
     [StructLayout(LayoutKind.Sequential)] 
     struct HARDWAREINPUT 
     { 
      public int uMsg; 
      public short wParamL; 
      public short wParamH; 
     } 
     struct MouseInputData 
     { 
      public int dx; 
      public int dy; 
      public uint mouseData; 
      public MouseEventFlags dwFlags; 
      public uint time; 
      public IntPtr dwExtraInfo; 
     } 
     [Flags] 
     enum MouseEventFlags : uint 
     { 
      MOUSEEVENTF_MOVE = 0x0001, 
      MOUSEEVENTF_LEFTDOWN = 0x0002, 
      MOUSEEVENTF_LEFTUP = 0x0004, 
      MOUSEEVENTF_RIGHTDOWN = 0x0008, 
      MOUSEEVENTF_RIGHTUP = 0x0010, 
      MOUSEEVENTF_MIDDLEDOWN = 0x0020, 
      MOUSEEVENTF_MIDDLEUP = 0x0040, 
      MOUSEEVENTF_XDOWN = 0x0080, 
      MOUSEEVENTF_XUP = 0x0100, 
      MOUSEEVENTF_WHEEL = 0x0800, 
      MOUSEEVENTF_VIRTUALDESK = 0x4000, 
      MOUSEEVENTF_ABSOLUTE = 0x8000 
     } 
     enum SendInputEventType : int 
     { 
      InputMouse, 
      InputKeyboard, 
      InputHardware 
     } 

     public static void ClickRightMouseButton() 
     { 
      INPUT mouseDownInput = new INPUT(); 
      mouseDownInput.type = SendInputEventType.InputMouse; 
      mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN; 
      SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT())); 

      INPUT mouseUpInput = new INPUT(); 
      mouseUpInput.type = SendInputEventType.InputMouse; 
      mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP; 
      SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT())); 
     } 
    } 

結果:

enter image description here

0

您可以按照以下步驟

步驟1

創建一個新的Windows窗體應用程序,並將一個TextBoxContextMenuStrip控制到表格。將TextBox的MultiLine屬性設置爲true,將ContextMenuStrip屬性設置爲contextMenuStrip1以覆蓋TextBox的默認ContextMenu(如下圖所示)。請參閱: enter image description here

步驟2

中的ContextMenuStrip增加6個菜單項中的TextBox控件的默認的ContextMenuStrip,撤銷,剪切,複製,粘貼,刪除,並與三個分離器「全選」撤銷後,刪除和「全選」菜單項。在ContextMenuStrip字體,前景色和背景色中添加三個菜單項,用於更改文本框的字體,文本顏色和背景色。 contextMenuStrip1應該像上面的第一張圖一樣。你可以參考我以前的文章來添加一個上下文菜單。

將這些菜單項的Name屬性設置爲與它們的Text相同。對於菜單項「撤消」,將其名稱設置爲「撤消」。

步驟3

添加以下代碼中contextMenuStrip1開幕事件禁用基於某些條件(見註釋)ToolStripMenuItems。請參閱:

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 
{ 
    // Disable Undo if CanUndo property returns false 
    if (textBox1.CanUndo) 
    { 
     contextMenuStrip1.Items["Undo"].Enabled = true; 
    } 
    else 
    { 
     contextMenuStrip1.Items["Undo"].Enabled = false; 
    } 


// Disable Cut, Copy and Delete if any text is not selected in TextBox 
if (textBox1.SelectedText.Length == 0) 
{ 
    contextMenuStrip1.Items["Cut"].Enabled = false; 
    contextMenuStrip1.Items["Copy"].Enabled = false; 
    contextMenuStrip1.Items["Delete"].Enabled = false; 
} 
else 
{ 
    contextMenuStrip1.Items["Cut"].Enabled = true; 
    contextMenuStrip1.Items["Copy"].Enabled = true; 
    contextMenuStrip1.Items["Delete"].Enabled = true; 
} 

// Disable Paste if Clipboard does not contains text 
if (Clipboard.ContainsText()) 
{ 
    contextMenuStrip1.Items["Paste"].Enabled = true; 
} 
else 
{ 
    contextMenuStrip1.Items["Paste"].Enabled = false; 
} 

// Disable Select All if TextBox is blank 
if (textBox1.Text.Length == 0) 
{ 
    contextMenuStrip1.Items["SelectAll"].Enabled = false; 
} 
else 
{ 
    contextMenuStrip1.Items["SelectAll"].Enabled = true; 
} 

}

步驟4

添加以下代碼ToolStripMenuItems的單擊事件:

private void Undo_Click(object sender, EventArgs e) 
{ 
    textBox1.Undo(); 
} 

private void Cut_Click(object sender, EventArgs e) 
{ 
    textBox1.Cut(); 
} 

private void Copy_Click(object sender, EventArgs e) 
{ 
    textBox1.Copy(); 
} 

private void Paste_Click(object sender, EventArgs e) 
{ 
    textBox1.Paste(); 
} 

private void Delete_Click(object sender, EventArgs e) 
{ 
    int SelectionIndex = textBox1.SelectionStart; 
    int SelectionCount = textBox1.SelectionLength; 
    textBox1.Text = textBox1.Text.Remove(SelectionIndex, SelectionCount); 
    textBox1.SelectionStart = SelectionIndex; 
} 

private void SelectAll_Click(object sender, EventArgs e) 
{ 
    textBox1.SelectAll(); 
} 

private void Font_Click(object sender, EventArgs e) 
{ 
    FontDialog fontDialog = new FontDialog(); 
    if (fontDialog.ShowDialog() == DialogResult.OK) 
    { 
     textBox1.Font = fontDialog.Font; 
    } 
} 

private void Forecolor_Click(object sender, EventArgs e) 
{ 
    ColorDialog colorDialog = new ColorDialog(); 
    if (colorDialog.ShowDialog() == DialogResult.OK) 
    { 
     textBox1.ForeColor = colorDialog.Color; 
    } 
} 

private void Backcolor_Click(object sender, EventArgs e) 
{ 
    ColorDialog colorDialog = new ColorDialog(); 
    if (colorDialog.ShowDialog() == DialogResult.OK) 
    { 
     textBox1.BackColor = colorDialog.Color; 
    } 
}