2

I have an application with a WebBrowser Control. I load a page to it with a button click. Then I want to run a "Convert to Adobe PDF" action from the context menu of the web browser but... when I try to access the context menu by:

foreach (MenuItem vMenuItem in WebBrowser.ContextMenu.MenuItems)
{
    if (vMenuItem.Text.Contains("onwert") && vMenuItem.Text.Contains("PDF"))
    {
        vMenuItem.PerformClick();
    }
}

The IDE displays an error "Object reference not set to an instance of an object" on line with

foreach (MenuItem vMenuItem in WebBrowser.ContextMenu.MenuItems)

I didn't create my own context menu, I want the default context menu to show. How can I access the WebBrowser's context menu and perform that action?

Harshana Narangoda
  • 775
  • 1
  • 8
  • 23
Jacek Rz.
  • 61
  • 5
  • [This artical may help](http://www.dzone.com/links/how_to_customize_the_webbrowser_context_menu_in_c.html). – Amicable Apr 24 '14 at 09:33

1 Answers1

2

i'm having a similar problem to you. I have made a reference to your problem in my post here. If you are still interested in this problem this is working solution that makes use of simulating clicks:

//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()));
}
Community
  • 1
  • 1
Msegling
  • 365
  • 3
  • 12
  • 1
    Hi Mew this helps me a lot.thanks for your post .is there any custom methods which captures key events. – madan Sep 29 '14 at 07:04
  • @madan Glad i could help... Simulation of key events are a hell of a lot easier. Example: SendKeys.Send("{ESC}"); This will obviously simulate the escape key event. http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx – Msegling Sep 30 '14 at 15:58
  • Thanks for your reply. I have this question have you any solution for it. http://stackoverflow.com/questions/25884830/handling-key-events-on-webbrowser-control – madan Oct 01 '14 at 04:49