views:

628

answers:

3

We're using silverlight in a kiosk type scenario. Is there a way to disable the right click ability to enter the silverlight configuration dialog?

+5  A: 

// EDIT

or better yet the silverlight forum recommends you do this: Silverlight Forum

<div id="silverlightObjDiv">
    <!-- silverlight object here -->
</div>

<script>
document.getElementById('silverlightObjDiv').oncontextmenu = disableRightClick;
function disableRightClick(e) {
    if (!e) e = window.event;
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}
</script>
Eric Ness
Genius! Thanks.
Jeremy
A: 

In Silverlight 4 you can do it in C#, without fiddling with and being dependent on any HTML.

The example below shows how to implement right click to be actually used by a control, but you can just create a clicktrap if you only want to disable.

 public partial class MainPage : UserControl
 {
      public MainPage()
      {
          InitializeComponent();

          // wire up the event handlers for the event on a particular UIElement
          ChangingRectangle.MouseRightButtonDown += new MouseButtonEventHandler(RectangleContextDown);
          ChangingRectangle.MouseRightButtonUp += new MouseButtonEventHandler(RectangleContextUp);
      }

     void RectangleContextUp(object sender, MouseButtonEventArgs e)
     {
         // create custom context menu control and show it.
         ColorChangeContextMenu contextMenu = new ColorChangeContextMenu(ChangingRectangle);
         contextMenu.Show(e.GetPosition(LayoutRoot));
     }

     void RectangleContextDown(object sender, MouseButtonEventArgs e)
     {
         // handle the event so the default context menu is hidden
         e.Handled = true;
     }
 }

Reference: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#rightclick

dain
+1  A: 

As Dain mentioned, in Silverlight 4 you can do this easily:

Make the control windowless:

<param name="windowless" value="true" />

Trap the right click in your root grid/layout control:

public MainPage()
{
    LayoutRoot.MouseRightButtonDown += (s, e) => { e.Handled = true; };
}
Chris S