ContextMenuHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Windows.Forms;
  2. using CefSharp;
  3. using CefSharp.WinForms;
  4. namespace prBrowser
  5. {
  6. internal class ContextMenuHandler : IContextMenuHandler
  7. {
  8. private const int ShowDevTools = 26501;
  9. private const int CloseDevTools = 26502;
  10. private const int SaveImageAs = 26503;
  11. private const int SaveAsPdf = 26504;
  12. private const int SaveLinkAs = 26505;
  13. private const int CopyLinkAddress = 26506;
  14. private const int OpenLinkInNewTab = 26507;
  15. private const int CloseTab = 40007;
  16. private const int RefreshTab = 40008;
  17. private const int DeleteCache = 62535;
  18. private readonly MainForm myForm;
  19. private string lastSelText = "";
  20. public ContextMenuHandler(MainForm form)
  21. {
  22. myForm = form;
  23. }
  24. public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
  25. {
  26. model.Clear();
  27. lastSelText = parameters.SelectionText;
  28. if (parameters.SelectionText.CheckIfValid())
  29. {
  30. model.AddItem(CefMenuCommand.Copy, "复制");
  31. model.AddItem(CefMenuCommand.Paste, "粘贴");
  32. model.AddSeparator();
  33. }
  34. else
  35. {
  36. model.AddItem(CefMenuCommand.Paste, "粘贴");
  37. model.AddSeparator();
  38. }
  39. if (parameters.LinkUrl != "")
  40. {
  41. model.AddItem((CefMenuCommand)26507, "在新标签页中打开连接");
  42. model.AddItem((CefMenuCommand)26506, "复制连接");
  43. model.AddSeparator();
  44. }
  45. if (parameters.HasImageContents && parameters.SourceUrl.CheckIfValid())
  46. {
  47. model.AddItem((CefMenuCommand)26503, "图片另存为");
  48. }
  49. if (parameters.SelectionText != null)
  50. {
  51. }
  52. model.AddItem((CefMenuCommand)26501, "开发者工具");
  53. model.AddItem(CefMenuCommand.ViewSource, "查看源代码");
  54. model.AddItem((CefMenuCommand)62535, "强力清除缓存(会关闭浏览器)");
  55. model.AddSeparator();
  56. model.AddItem((CefMenuCommand)40008, "刷新标签页");
  57. model.AddItem((CefMenuCommand)40007, "关闭标签页");
  58. }
  59. public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
  60. {
  61. if (commandId == (CefMenuCommand)26501)
  62. {
  63. browser.ShowDevTools();
  64. }
  65. if (commandId == (CefMenuCommand)26502)
  66. {
  67. browser.CloseDevTools();
  68. }
  69. if (commandId == (CefMenuCommand)26503)
  70. {
  71. browser.GetHost().StartDownload(parameters.SourceUrl);
  72. }
  73. if (commandId == (CefMenuCommand)26505)
  74. {
  75. browser.GetHost().StartDownload(parameters.LinkUrl);
  76. }
  77. if (commandId == (CefMenuCommand)26507)
  78. {
  79. ChromiumWebBrowser newBrowser = myForm.AddNewBrowserTab(parameters.LinkUrl, focusNewTab: false, browser.MainFrame.Url);
  80. }
  81. if (commandId == (CefMenuCommand)26506)
  82. {
  83. Clipboard.SetText(parameters.LinkUrl);
  84. }
  85. if (commandId == (CefMenuCommand)40007)
  86. {
  87. myForm.InvokeOnParent(delegate
  88. {
  89. myForm.CloseActiveTab();
  90. });
  91. }
  92. if (commandId == (CefMenuCommand)40008)
  93. {
  94. myForm.InvokeOnParent(delegate
  95. {
  96. myForm.RefreshActiveTab();
  97. });
  98. }
  99. if (commandId == (CefMenuCommand)62535)
  100. {
  101. Application.Exit();
  102. }
  103. return false;
  104. }
  105. public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
  106. {
  107. }
  108. public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
  109. {
  110. return false;
  111. }
  112. }
  113. }