[英]How to add a context menu in the webbrowser?
我知道已經有一個與此相關的主題,但是我需要更多信息。 目前,我有從另一個線程獲得的這段代碼,
public void AttachContextMenu()
{
try
{
if (webTab1.IsScriptEnabled)
{
webTab1.InvokeScript("execScript", "function FindParentLink(item) \r\n{\r\n\tif (!item.parentNode)\r\n\t\treturn null;\r\n\tif (item.tagName.toLowerCase() == 'a') \r\n\t{\r\n\t\treturn item;\r\n\t} \r\n\telse \r\n\t{\r\n\t\treturn FindParentLink(item.parentNode);\r\n\t}\r\n}\r\n\r\nfunction FindParentImage(item) \r\n{\r\n\tif (!item.parentNode)\r\n\t\treturn null;\r\n\tif (item.tagName.toLowerCase() == 'img') \r\n\t{\r\n\t\treturn item;\r\n\t} \r\n\telse \r\n\t{\r\n\t\treturn FindParentImage(item.parentNode);\r\n\t}\r\n}\r\n\r\nfunction HandleContextMenu() \r\n{\r\n\tvar linkItem = FindParentLink(event.srcElement);\r\n var imageItem = FindParentImage(event.srcElement);\r\n var notifyOutput = '';\r\n if (linkItem != null) if (linkItem.href != null) notifyOutput += linkItem.href;\r\n if (imageItem != null) if (imageItem.src != null) notifyOutput += imageItem.src;\r\n if (notifyOutput != '')\r\n window.external.notify(notifyOutput);\r\n else\r\n\t\twindow.external.notify('NOTLINKIMG');\r\n}");
webTab1.InvokeScript("execScript", "document.oncontextmenu = HandleContextMenu;");
}
}
catch
{
}
}
該線程說,當瀏覽器完成加載后,您可以調用此方法,盡管它似乎不起作用。
關於如何使它工作的任何想法?
這僅適用於Windows Phone7.X。 不是8。
AddHandler wb.Loaded, Sub(sendr As Object, ev As EventArgs)
wb.IsScriptEnabled = True
AttachContextMenu()
End Sub
AddHandler wb.ScriptNotify, Sub(sender As Object, e As Microsoft.Phone.Controls.NotifyEventArgs)
Diagnostics.Debug.WriteLine(e.Value.ToString)
End Sub
如果您知道如何在WP8 WebBrowser控件中打開上下文菜單,請告知我們。 在WP8上,我將onmousedown事件,計時器和onmouseout事件與WP7中的findparentlink函數結合使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.