cookieChoices = {};

Friday, 25 April 2014

Handling Mouse Right Click Event Example

Main.Xaml



<Grid x:Name="LayoutRoot" Background="Black">
        <Border x:Name="BorderCtrl" Height="400" Width="400" MouseLeftButtonDown="BorderCtrl_MouseLeftButtonDown"
                MouseRightButtonDown="BorderCtrl_MouseRightButtonDown" MouseRightButtonUp="BorderCtrl_MouseRightButtonUp" >
            <Border.BorderBrush>
                <LinearGradientBrush EndPoint="0,0" StartPoint="1,1">
                    <GradientStop Color="#333333" Offset="0"/>
                    <GradientStop Color="#999333" Offset="1"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">

                    <GradientStop Color="#1F1F1F" Offset="0"/>
                    <GradientStop Color="#060606" Offset="1"/>
                </LinearGradientBrush>

            </Border.Background>

        </Border>
        <TextBlock Text="Right Click on App" IsHitTestVisible="False" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" />
        <Canvas>
            <Popup x:Name="ContextMenu" Visibility="Collapsed">
                <Border CornerRadius="5" BorderBrush="White" Background="Black" BorderThickness="1">
                    <StackPanel>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Green" Content="Green" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Red" Content="Red" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Blue" Content="Blue" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="White" Content="White" Foreground="White" ></HyperlinkButton>


                    </StackPanel>
                </Border>
            </Popup>

        </Canvas>



    </Grid>

Main.Xaml.cs


 private void BorderCtrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContextMenu.Visibility = Visibility.Collapsed;
            ContextMenu.IsOpen = true;
        }

        private void BorderCtrl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void BorderCtrl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(null);
            e.Handled = true;
            ContextMenu.Visibility = Visibility.Visible;
            ContextMenu.IsOpen = true;
            ContextMenu.SetValue(Canvas.LeftProperty, (double)p.X);
            ContextMenu.SetValue(Canvas.TopProperty, (double)p.Y);
        }

        private void Red(object Sender, RoutedEventArgs e)
        {
                   
            Brush br = new SolidColorBrush(Color.FromArgb(100,250,0,0));
            LayoutRoot.Background = br;
        }

        private void Green(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 250, 0));
            LayoutRoot.Background = br;
        }

        private void Blue(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 0, 250));
            LayoutRoot.Background = br;
        }

        private void White(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100,250, 250, 250));
            LayoutRoot.Background = br;
        }
        

No comments:

Post a Comment