2014-04-24 109 views
2

我有一個帶有WebBrowser控件的應用程序。 我點擊一個按鈕加載一個頁面。 然後我想從運行Web瀏覽器的上下文菜單「轉換爲Adobe PDF」行動,但...當我嘗試通過訪問上下文菜單:WebBrowser ContextMenu菜單項的運行操作

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

的IDE顯示錯誤「對象未設置爲上線的對象」的一個實例與

foreach (MenuItem vMenuItem in WebBrowser.ContextMenu.MenuItems) 

我沒有創建自己的上下文菜單,我想默認的上下文菜單顯示。 如何訪問WebBrowser的上下文菜單並執行該操作?

+0

[這ARTICAL可以幫助(http://www.dzone.com/links/how_to_customize_the_webbrowser_context_menu_in_c.html)。 – Amicable

回答

2

我遇到了類似的問題。我在我的文章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())); 
} 
+1

嗨喵,這可以幫助我很多。感謝您的帖子。有任何自定義方法捕捉關鍵事件。 – madan

+0

@madan很高興我能幫上忙......關鍵事件的模擬更容易。例如:SendKeys.Send(「{ESC}」); 這將顯然模擬逃生關鍵事件。 http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx – Msegling

+0

感謝您的回覆。我有這個問題,你有任何解決方案。 http://stackoverflow.com/questions/25884830/handling-key-events-on-webbrowser-control – madan