我正在Visual Studio上编写一些代码,以便从网页中获取一些信息。 现在我需要打开html元素的上下文菜单,我正在寻找类似的东西:
webbrowser.Navigate("javascript: document.getElementsByClassName('className')[1].rightclick();void(0);");
但不幸的是,我注意到Javascript中只存在click()函数。 有没有解决方法?
谢谢大家!
答案 0 :(得分:2)
您可以通过流程步骤执行此操作:
获取表单上web-browser control
的位置:
Point controlLoc = this.PointToScreen(webBrowser1.Location);
在网络浏览器控件上获取HtmlElement
的位置
X= element.OffsetRectangle.Left;
Y =element.OffsetRectangle.Top;
总结你的职位:
controlLoc.X = controlLoc.X + element.OffsetRectangle.Left;
controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top;
将鼠标位置设置为新位置:
Cursor.Position = controlLoc;
模拟鼠标右键单击:
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()));
}
}
<强>结果:强>
答案 1 :(得分:0)
您可以按照以下步骤操作
第1步
创建一个新的Windows窗体应用程序,并将TextBox
和ContextMenuStrip
控件拖到窗体上。将TextBox的MultiLine属性设置为true,将ContextMenuStrip属性设置为contextMenuStrip1以覆盖TextBox的默认ContextMenu(如下图所示)。看到:
第2步
在ContextMenuStrip中添加六个菜单项,如TextBox控件的默认ContextMenuStrip,撤消,剪切,复制,粘贴,删除和“全选”,在撤消,删除和“全选”菜单项后有三个分隔符。在ContextMenuStrip字体,Forecolor和Backcolor中添加三个菜单项,用于更改TextBox的字体,文本颜色和背景颜色。 contextMenuStrip1应该如上图所示。您可以参考我之前的文章添加上下文菜单。
将这些菜单项的Name属性设置为与Text相同。与菜单项“撤消”类似,将其名称设置为“撤消”。
第3步
在contextMenuStrip1的Opening事件中添加以下代码,以根据特定条件禁用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;
}
}