C# webbrowser控件,更改右键单击选项

发布于 2024-10-31 04:32:13 字数 111 浏览 0 评论 0原文

我需要你的帮助来在我拥有的网络浏览器控件中创建右键单击选项。 如果我右键单击链接,可能会打开此选项: 在新选项卡中打开 在新窗口中打开 复制链接 请帮助我创建 C# 代码和解释,因为我是网络浏览器控制的新手。

i need your help to create a right click option in webbrowser control i have.
may be if i right click in a link will open this option :
open in new tab
open in new window
copy link
please help me create a the C# code and the explaination, because i am a newbie in webbrowser control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-11-07 04:32:13

使用 Visual Studio 创建您自己的上下文菜单非常容易。只需将一个上下文菜单项放到您的表单上,然后繁荣,您就快完成了。

添加所需的按钮,根据需要处理其单击/更改事件。

然后,在您的代码中,要么使用 webBrowser 控件上的属性将上下文菜单更改为您创建的菜单,要么在 Form_Load 上的代码中处理它。

 private void Form1Load(object sender, EventArgs e)
 {
    mainBrowser.ContextMenuStrip = browserCMenu;
 }

browserCMenu 将在设计器内部创建,但您也可以从代码创建:

this.browserCMenu = new System.Windows.Forms.ContextMenuStrip(this.components);

// This is the AddRange() command for items.  
// You basically create your controls first (buttons, checkboxes, etc), and then assign
// them here, so they are added to your control.
this.browserCMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    this.copylinkMenuItem,
    this.copyMenuitem}); 

this.browserCMenu.Name = "browserCMenu";
this.browserCMenu.Size = new System.Drawing.Size(128, 48);

请注意,上面是设计器生成的代码。由于您(很可能)将使用 Visual Studio 设计器来完成工作,因此您只需执行以下步骤:

  1. 将 ContextMenuStrip 项从工具箱拖到窗体中。
  2. 单击“在此处键入”为新按钮命名,或单击箭头添加不同类型的控件。
  3. 然后只需双击您添加的控件(按钮、列表等),即可处理上下文菜单项的单击事件/chagned 事件,并添加代码。
  4. 通过更改 Web 浏览器上下文菜单项(如上面的 Form1Load() 部分所示)将该上下文菜单指定为 Web 浏览器的上下文菜单。

您还可以查询目标文本类型,但我不会在这里详细介绍,您可以搜索 MSDN 来获取该信息,或者在 StackOverflow 上搜索,有几篇相关文章。但这将允许您右键单击不同类型的数据获得不同的上下文菜单(我个人认为它没有什么用处)。最好为所有使用相同上下文菜单项的浏览器控件提供一个标准上下文菜单,这样您的代码就不会因上下文菜单过多而陷入困境/过于复杂。

It's pretty easy to create your own context menu using Visual studio. Just drop a context menu item onto your form, and boom, your almost done.

Add the buttons you want, handle their click/changed events as necessary.

Then, in your code, either use the properties on the webBrowser control to change the context menu to the one you created, or handle it in code on Form_Load.

 private void Form1Load(object sender, EventArgs e)
 {
    mainBrowser.ContextMenuStrip = browserCMenu;
 }

Where browserCMenu would be created inside of the designer, but you could create from code as well:

this.browserCMenu = new System.Windows.Forms.ContextMenuStrip(this.components);

// This is the AddRange() command for items.  
// You basically create your controls first (buttons, checkboxes, etc), and then assign
// them here, so they are added to your control.
this.browserCMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    this.copylinkMenuItem,
    this.copyMenuitem}); 

this.browserCMenu.Name = "browserCMenu";
this.browserCMenu.Size = new System.Drawing.Size(128, 48);

Note that the above is designer generated code. As you'll be (most likely) using the Visual Studio designer to do your work you can just do the following steps:

  1. Drag a ContextMenuStrip item from the toolbox to your form.
  2. Click on 'Type Here' to name your new button, or click the arrow to add different type of controls.
  3. Then simply double click on the control you added (button, list, etc), to handle the click event/chagned event for the context menu item, and add your code.
  4. Assign that context menu, as the context menu for the web browser by changing the web browser context menu item (as shown in the Form1Load() section above).

You can also query what type of text is being targeted as well, but i wont go into that here, you can search MSDN for that information, or search here on StackOverflow, there are several articles on that. But that will allow you to obtain different context menus for different types of data being right clicked (i dont find it that useful personally). It's best just to present a standard context menu for all browser controls, that use the same context menu items, so that your code isn't bogged down/too complex with too many context menus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文