Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable right click in silverlight

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?

like image 505
Jeremy Avatar asked Feb 10 '09 16:02

Jeremy


3 Answers

//in the SharePoint I have added a Small code to tell SP to run the script after every part is loaded. Works like a charm :)

// EDIT

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

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

<script>
_spBodyOnLoadFunctionNames.push ('setupElement');

function setupElement ()

{

document.getElementById('silverlightObjDiv').oncontextmenu =      disableRightClick;

}

function disableRightClick(e) {
if (!e) e = window.event;
if (e.preventDefault) {
    e.preventDefault();
} else {
    e.returnValue = false;
}
}
</script>
like image 54
Eric Ness Avatar answered Sep 20 '22 07:09

Eric Ness


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; };
}

The catch
In Firefox and Chrome you have to choose between having a context menu or having mousewheel scroll capabilities. Sadly you can't have both, hopefully this will change in Silverlight 5.

like image 32
Chris S Avatar answered Sep 20 '22 07:09

Chris S


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

like image 29
dain Avatar answered Sep 23 '22 07:09

dain