Windows 8 practical tips series: 16. Windows 8 right-click menu

Source: Internet
Author: User

In Windows 8, controls include contextmenuopening events of input controls such as textbox and righttapped events of non-input controls such as buttons.

This article describes the usage of two events. The

Popupmenu is a class of the shortcut menu.

First, let's take a look at the instantiation of the specific menu class and get the project code.

/// <Summary> /// right-click and choose processing details /// </Summary> /// <Param name = "sender"> </param> Public async void setrightclick (Object sender) {// Add menu popupmenu menu = new popupmenu (); menu. commands. add (New uicommand ("Copy 0", null, 0); menu. commands. add (New uicommand ("Cut 1", null, 1); menu. commands. add (New uicommand ("paste 2", null, 2); menu. commands. add (New uicommandseparator (); menu. commands. add (New uicommand ("F Ull screen ", null, 3); menu. commands. add (New uicommand ("snap screen", null, 4); // obtain the selected menu item var cmd = await menu. showforselectionasync (getrectposition (frameworkelement) sender); If (CMD! = NULL) {Switch (INT) cmd. ID) {Case 0: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 1: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 2: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 3: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 4: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break ;}} else {tbtext. TEXT = "context menu" ;}}// obtain the menu position rect getrectposition (frameworkelement element) {generaltransform btnform = element. transformtovisual (null); point = btnform. transformpoint (new point (); return New rect (point, new size (element. actualwidth, element. actualheight ));}

Then let's take a look at the event of registering the right-click processing of the two controls as follows:

/// <Summary> /// invoked when this page is about to be displayed in a frame. /// </Summary> /// <Param name = "E"> event data that describes how this page was reached. the parameter // property is typically used to configure the page. </param> protected override void onnavigatedto (navigationeventargs e) {base. onnavigatedto (E); // context menu this. tbname. contextmenuopening + = tbname_contextmenuopening; // right-click the menu this. gdmenu. righttapped + = gdmenu_righttapped ;} /// <summary> /// right-click the menu event /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void gdmenu_righttapped (Object sender, righttappedroutedeventargs e) {setrightclick (sender );} /// <summary> /// context menu /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void tbname_contextmenuopening (Object sender, contextmenueventargs e) {setrightclick (sender );} /// <summary> /// event uninstall /// </Summary> /// <Param name = "E"> </param> protected override void onnavigatedfrom (navigationeventargs E) {base. onnavigatedfrom (E); // context menu this. tbname. contextmenuopening-= tbname_contextmenuopening; // right-click the menu this. gdmenu. righttapped-= gdmenu_righttapped ;}

The front-end XAML code is as follows:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="gdMenu">        <TextBox HorizontalAlignment="Left" Margin="141,164,0,0" TextWrapping="Wrap"                 Name="tbName" Text="" VerticalAlignment="Top" Height="157" Width="540"/>        <TextBox HorizontalAlignment="Left" Margin="210,353,0,0" TextWrapping="Wrap"                  Name="tbText" Text="" VerticalAlignment="Top" Width="290"/>    </Grid>

To download the source code, click win8menu.rar.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.