Skip to main content

Microsoft Silverlight

Answered Question Dig deeper into right-mouse-click/oncontextmenu/windowlessRSS Feed

(0)

goldenbull
goldenbull

Member

Member

1 points

3 Posts

Dig deeper into right-mouse-click/oncontextmenu/windowless

In Silverlight 2.0, it's quite easy to hide the default context menu when user right click in Silverlight app:

1. Set Windowless attribute to true in html or aspx

2. In C# code, add a line: HtmlPage.Document.AttachEvent("oncontextmenu", myHandler);

3. Implement myHandler to deal with right click event.

For details, please check these links:

http://silverlight.net/forums/p/48157/127981.aspx

http://silverlight.net/blogs/msnow/archive/2008/07/01/tip-of-the-day-14-how-to-right-click-on-a-silverlight-application.aspx

However, I want to dig deeper to make the whole thing more clear. My question is why the Silverlight app must in Windowless mode in order to prevent the default contextmenu?

I try to list "what happened" one by one:

1. user right click the mouse, and the browser gets the action through WM_RBUTTONDOWN (suppose we are on Windows OS, the browser must be a windows application.)

2. browser will invoke oncontextmenu handler? well, I don't know the detail here, but I guess this is the crucial step of my question.

3. the managed code myHandler method will be invoked

4. e.PreventDefault() will invoke DOM_PreventDefault(IntPtr pBrowserService, IntPtr pObject) in agcore.dll

Great thanks to anybody helping me expand and complete the step 2

SharpGIS
SharpGIS

Contributor

Contributor

3387 points

611 Posts

Answered Question

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

The reason that it must be in windowless mode is that we rely on the browser for the right click event, and for the browser to receive the rightclick, the browser should take care of rendering the plugin.
I recently wrote a blogpost that went a little deeper with mouse gestures and rightclick, and also works with mozilla browsers: http://www.sharpgis.net/post/2009/05/09/Adding-rightclick-to-Silverlight.aspx

Having said that, no matter how you do this, it is a hack, and I think you should try and think outside the box and not use right click (my blog post was initially about mouse gestures that would function as alternative to right click). What I'm getting at is that the right click menu is there for the user, and we shouldn't be telling the user what they can or cant do. The equivalient in Windows would be if you create a windows application and you take over the start key for something other than opening the start menu.

--
/Morten | blog - twitter
Please click on "Mark as Answer" if this answered your question.

goldenbull
goldenbull

Member

Member

1 points

3 Posts

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

Thanks for your reply, but my customers can not live without right mouse...

I have some more questions. I know that IE and Firefox can run xbap application, in which right click is just as simple as windows application. Why Silverlight doesn't use similar approach?

And why should the browser deal with rendering jobs when it deal with the right click event? I think they are two different things. Just suppose:

1. Browser gets the right click event

2. Browser knows the right click is on silverlight plugin, including the position information

3. Browser fire a right click event to the plugin, and do nothing more. The plugin renders itself in window mode.

Is this possible?

SharpGIS
SharpGIS

Contributor

Contributor

3387 points

611 Posts

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

There are several reason why Silverlight doesn't give you a right click option, but none of them are because it wasn't possible for Microsoft to do, but it was made like this by choice. Also remember that some systems don't have right click.

The fact of the matter is that this is how Silverlight currently works and you will have to live with it. Yes there are hacks you can do, but they are and always will be hacks, and the nature of these hacks requires the browser to render the control to be able to intercept rightclick before the silverlight plugin handles it.

xbap is a whole different story and doesn't compare to Silverlight.

--
/Morten | blog - twitter
Please click on "Mark as Answer" if this answered your question.

goldenbull
goldenbull

Member

Member

1 points

3 Posts

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

Thanks for your help! As far as now, my app need not heavy animations, with these hacks, silverlight works fine for me.

Harish L
Harish L

Member

Member

5 points

10 Posts

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

 Hi Morten,

Do you know any similar hack for chrome? 

Best,

Harish.

SharpGIS
SharpGIS

Contributor

Contributor

3387 points

611 Posts

Re: Dig deeper into right-mouse-click/oncontextmenu/windowless

I think the keyword in your question is :"Hack".

In other words, don't even bother trying to do this. It's a poor approach to circumvent the default right click menu. Instead try and think of other gestures you could use.

--
/Morten | blog - twitter
Please click on "Mark as Answer" if this answered your question.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities