WebBrowser ContextMenu 运行菜单项的操作

Posted

技术标签:

【中文标题】WebBrowser ContextMenu 运行菜单项的操作【英文标题】:WebBrowser ContextMenu run action of menu item 【发布时间】:2014-06-09 11:31:46 【问题描述】:

我有一个带有 WebBrowser 控件的应用程序。 我通过单击按钮将页面加载到它。 然后我想从 Web 浏览器的上下文菜单中运行“转换为 Adob​​e PDF”操作,但是......当我尝试通过以下方式访问上下文菜单时:

foreach (MenuItem vMenuItem in WebBrowser.ContextMenu.MenuItems)

    if (vMenuItem.Text.Contains("onwert") && vMenuItem.Text.Contains("PDF"))
    
        vMenuItem.PerformClick();
    

IDE在线显示错误“Object reference not set to an instance of an object”

foreach (MenuItem vMenuItem in WebBrowser.ContextMenu.MenuItems)

我没有创建自己的上下文菜单,我希望显示默认的上下文菜单。 如何访问 WebBrowser 的上下文菜单并执行该操作?

【问题讨论】:

This artical may help. 【参考方案1】:

我和你有类似的问题。我在here 的帖子中提到了您的问题。 如果您仍然对此问题感兴趣,这是使用模拟点击的有效解决方案:

//Example of how to use the code:
    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()));

【讨论】:

嗨喵喵这对我很有帮助。谢谢你的帖子。是否有任何自定义方法可以捕获关键事件。 @madan 很高兴我能帮上忙……关键事件的模拟要容易得多。示例:SendKeys.Send("ESC");这显然会模拟转义键事件。 msdn.microsoft.com/en-us/library/… 感谢您的回复。我有这个问题,你有什么解决办法。 ***.com/questions/25884830/…

以上是关于WebBrowser ContextMenu 运行菜单项的操作的主要内容,如果未能解决你的问题,请参考以下文章

在运行时创建表单和 WebBrowser

在显示 contextMenu 时做一些事情

vb Webbrowser中发生脚本运行错误,但是用WebBrowser1.Silent=true后相当网页还是运行不正常,该怎么解决

允许 System.Windows.Forms.WebBrowser 运行 javascript

ContextMenu 中的 NavigationLink 不再在 iOS14 xcode12 beta3 中工作?

WebBrowser.DrawToBitmap() 还是其他方法?