C# 调用Webbrowser上下文菜单
我正在做一个项目,一个网络机器人,它利用了网络浏览器控件 我的目标是以编程方式打开加载的WebBrowser中所需元素的WebBrowser上下文菜单,并从上下文菜单中选择一个选项 示例: 在WebBrowser控件中导航到Google。 打开上下文菜单。选择“显示图片” 到目前为止,这是我找到的最接近的代码: 到目前为止,这段代码在第一行返回一个错误,这里有解决方案吗C# 调用Webbrowser上下文菜单,c#,webbrowser-control,C#,Webbrowser Control,我正在做一个项目,一个网络机器人,它利用了网络浏览器控件 我的目标是以编程方式打开加载的WebBrowser中所需元素的WebBrowser上下文菜单,并从上下文菜单中选择一个选项 示例: 在WebBrowser控件中导航到Google。 打开上下文菜单。选择“显示图片” 到目前为止,这是我找到的最接近的代码: 到目前为止,这段代码在第一行返回一个错误,这里有解决方案吗 工作备选方案: 到目前为止,我已经通过模拟点击实现了这一点,问题是当窗口被隐藏时,会出现明显的错误。我也不希望光标在屏幕
工作备选方案: 到目前为止,我已经通过模拟点击实现了这一点,问题是当窗口被隐藏时,会出现明显的错误。我也不希望光标在屏幕上跳跃。如果有一种方法可以模拟隐藏窗口上的点击,那么这可能是一个解决方案。 下面是我当前模拟点击的代码:(虽然我不想使用模拟点击,但这段代码可以工作) 如果有办法通过调用WebBrowsers ContextMenu完成此过程
凹凸。这取决于您的具体要求。如果您想操作特定的HTML元素,可以通过DOM访问它。依我看,你可以做WebBrowser能做的一切 如果要使用上下文菜单(避免编写额外代码或假装用户操作),可以在所需位置发送鼠标单击,然后临时阻止上下文菜单弹出窗口。这是我在Delphi中的示例代码,供您参考
function TTrident.ShowContextMenu(const dwID: DWORD; const ppt: PPOINT; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
if FDontShowContextMenuThisTime then
begin
FDontShowContextMenuThisTime := False;
Exit(S_OK);
end
else
Exit(S_FALSE);
end;
ShowContextMenu
是界面IDocHostUIHandler
的一种方法。换句话说,您必须通过实现至少IDocHostUIHandler
来扩展WebBrowser控件
Point controlLoc = this.PointToScreen(webbrowser1.Location);
controlLoc.X = controlLoc.X + webbrowser1.Document.GetElementById("sbvdcapimg").OffsetRectangle.Left+65;
controlLoc.Y = controlLoc.Y + webbrowser1.Document.GetElementById("sbvdcapimg").OffsetRectangle.Top+50;
Cursor.Position = controlLoc;
MouseSimulator.ClickRightMouseButton();
controlLoc.X = controlLoc.X + (webbrowser1.Document.GetElementById("sbvdcapimg").OffsetRectangle.Left + 95);
controlLoc.Y = controlLoc.Y + (webbrowser1.Document.GetElementById("sbvdcapimg").OffsetRectangle.Top + 45);
Cursor.Position = controlLoc;
MouseSimulator.ClickLeftMouseButton();
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()));
}
public static void ClickLeftMouseButton()
{
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = SendInputEventType.InputMouse;
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
}
function TTrident.ShowContextMenu(const dwID: DWORD; const ppt: PPOINT; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
if FDontShowContextMenuThisTime then
begin
FDontShowContextMenuThisTime := False;
Exit(S_OK);
end
else
Exit(S_FALSE);
end;