如何在Silverlight2.0中使用滑鼠右鍵

來源:互聯網
上載者:User

silverlight中可以處理很多鍵盤和滑鼠事件,然而遺憾的是這些事件並不包括滑鼠右鍵事件:滑鼠右鍵事件被Silverlight 自己的操作功能表“Silverlight Configuration”佔用;然而富互動程式大都希望使用滑鼠右鍵彈出操作功能表或實現快捷操作。

幸運的是,前輩大牛們早已解決了這個問題,要實現在程式中使用右鍵,實際上也挺簡單的,廢話不說,直接看實現步驟。

第一步、開啟嵌入Silverlight控制項對象的Aspx或html檔案,設定Silverlight控制項的Windowless屬性為 true 。例如:
Aspx檔案:

Code
<asp:Silverlight ID="Xaml1" runat="server" Windowless="true" Source="~/ClientBin/SlControl.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

Html檔案:

Code
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/CreateChartFromManagedCode.xap"/>
    <param name="onerror" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="2.0.31005.0" />
    <param name="autoUpgrade" value="true" />    
    <param name="Windowless" value="true" />        
    <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
    </a>
</object>

 

第二步、在.CS檔案中添加Page_Loaded事件處理,替換原來Silverlight的右鍵操作功能表處理,代碼如下:

Code
private void Page_Loaded(object sender, RoutedEventArgs e)
{

    HtmlPage.Document.AttachEvent("onmousedown", CatchOnMouseDown);
    HtmlPage.Document.AttachEvent("onmouseup", CatchOnMouseUp);
    HtmlPage.Document.AttachEvent("oncontextmenu", CatchOnContextMenu);
}
private void CatchOnMouseDown(object sender, HtmlEventArgs e)
{
    if (e.MouseButton == MouseButtons.Right)
    {
        //MessageBox.Show("Right Clicked Blocked at " + e.OffsetX + "," + e.OffsetY);
    }
}
private void CatchOnMouseUp(object sender, HtmlEventArgs e)
{
    if (e.MouseButton == MouseButtons.Right)
    {
        //MessageBox.Show("Right Clicked Blocked at " + e.OffsetX + "," + e.OffsetY);
    }
}
private void CatchOnContextMenu(object sender, HtmlEventArgs e)
{
    MessageBox.Show( "Right Clicked Blocked at " + e.OffsetX + "," + e.OffsetY);
    e.PreventDefault();              
}

 

至此,您就完成了對原來Silverlight操作功能表的屏蔽,也即可以使用右鍵功能了。

 

本文參考了:http://blogs.msdn.com/msnow/archive/2008/11/19/silverlight-tip-of-the-day-14-how-to-right-click-on-a-silverlight-application.aspx

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.