Skip to main content
Home Forums General Silverlight Getting Started Dig deeper into right-mouse-click/oncontextmenu/windowless
6 replies. Latest Post by SharpGIS on September 16, 2009.
(0)
goldenbull
Member
1 points
3 Posts
06-03-2009 1:23 AM |
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
Contributor
3387 points
611 Posts
06-03-2009 2:49 AM |
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.
06-03-2009 8:57 AM |
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?
06-03-2009 2:12 PM |
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.
06-03-2009 9:35 PM |
Thanks for your help! As far as now, my app need not heavy animations, with these hacks, silverlight works fine for me.
Harish L
5 points
10 Posts
09-16-2009 9:46 AM |
Hi Morten,
Do you know any similar hack for chrome?
Best,
Harish.
09-16-2009 6:57 PM |
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.