[help] WebBrowser ContextMenu

01/10/2012 21:17 PraDevil[ELITE]#1
i create a program which open the folder directory of my own computer and url that load it to my webbrowser control, and i'll disable the context menu from appear on the webbrowser while right clicking, yes it does it..but its only done when its was a website url and not a my computer directory..how i can disable completely this right click features on my webbrowser even on my whole project? in detail i made a program like this..

- i put this on my form, which it was buton1, button2, Exit button and the webbrowser control
- when button1 was pressed it will show my "C:\Program Files\" directory on the webbrowser
- when buton2 was pressed it will load a "www.google.com" url on my webbrowser
- i disable the context menu by set it to false on my webbrowser properties at "IsWebBrowserContextMenuEnable"

so i can't right click on the webbrowser anymore, but this only done when i pressed button2 and not button1 which is my "C:\Program Files\" directory.

so my question is how i can disable completely this right click features on my webbrowser even on my whole project? hope someone can answer my question :)
01/13/2012 00:32 RebeccaBlack#2
I found this:

Code:
using System;
using System.Windows.Forms;

namespace WindowsApplication1 {
  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
      this.FormClosed += new FormClosedEventHandler(this.Form1_FormClosed);
    }
    private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
      Application.RemoveMessageFilter(this);
    }
    public bool PreFilterMessage(ref Message m) {
      // Filter out WM_NCRBUTTONDOWN/UP/DBLCLK
      if (m.Msg == 0xA4 || m.Msg == 0xA5 || m.Msg == 0xA6) return true;
      // Filter out WM_RBUTTONDOWN/UP/DBLCLK
      if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true;
      return false;
    }
  }
}
01/15/2012 06:11 PraDevil[ELITE]#3
sorry rebecca, im was using vb not c#
it would be great if you can post for vb too.
01/15/2012 11:22 Jay Niize#4
You can convert it from C# to Vb.net. (:

Code:
Imports System
Imports System.Windows.Forms

Namespace WindowsApplication1
     
     Public Class Form1
         Inherits Form
         Implements IMessageFilter
         
         Public Sub New()
             MyBase.New
             InitializeComponent
             Application.AddMessageFilter(Me)
             AddHandler FormClosed, AddressOf Me.Form1_FormClosed
         End Sub
         
         Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
             Application.RemoveMessageFilter(Me)
         End Sub
         
         Public Function PreFilterMessage(ByRef m As Message) As Boolean
             ' Filter out WM_NCRBUTTONDOWN/UP/DBLCLK
             If ((m.Msg = 164)  _
                         OrElse ((m.Msg = 165)  _
                         OrElse (m.Msg = 166))) Then
                 Return true
             End If
             ' Filter out WM_RBUTTONDOWN/UP/DBLCLK
             If ((m.Msg = 516)  _
                         OrElse ((m.Msg = 517)  _
                         OrElse (m.Msg = 518))) Then
                 Return true
             End If
             Return false
         End Function
     End Class
 End Namespace
I've not tested it.
01/15/2012 15:09 PraDevil[ELITE]#5
im not sure what's was im doing now(newbie lol)
i just copy and paste those code to the top of my form1.vb and get this error
Code:
Class 'Form1' must implement 'Function PreFilterMessage(ByRef m As Message) As Boolean' for interface 'System.Windows.Forms.IMessageFilter'.

Name 'InitializeComponent' is not declared.
did i must create new class then paste the code there? if yes how to call this function?
i'm dead playing with it >.<
01/16/2012 11:46 Al Kappaccino#6
Quote:
Originally Posted by PraDevil[ELITE] View Post
im not sure what's was im doing now(newbie lol)
i just copy and paste those code to the top of my form1.vb and get this error
Code:
Class 'Form1' must implement 'Function PreFilterMessage(ByRef m As Message) As Boolean' for interface 'System.Windows.Forms.IMessageFilter'.

Name 'InitializeComponent' is not declared.
did i must create new class then paste the code there? if yes how to call this function?
i'm dead playing with it >.<
Just tested it with some settings you told.

It should look like this:

Code:
Imports System
Imports System.Windows.Forms

Public Class Form1 : Implements IMessageFilter


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate("www.google.de")
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        WebBrowser1.Navigate("C:/")
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(Me)
        AddHandler FormClosed, AddressOf Me.Form1_FormClosed
    End Sub

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        
        If ((m.Msg = 164) _
                    OrElse ((m.Msg = 165) _
                    OrElse (m.Msg = 166))) Then
            Return True
        End If
  
        If ((m.Msg = 516) _
                    OrElse ((m.Msg = 517) _
                    OrElse (m.Msg = 518))) Then
            Return True
        End If
        Return False
    End Function

    Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Application.RemoveMessageFilter(Me)
    End Sub

End Class
01/16/2012 17:27 PraDevil[ELITE]#7
man, really thanks! its work :D <3
01/21/2012 21:15 mrapc#8
Bot the easyest way is when you choose in the OPtions Left at Webborwser1 -> Contextmenustript = ContextMenuStript1 ;)