[10 行でズバリ!! [C#] マウス イベントの制御]をダウンロードする。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Grid.Row="0" x:Name="border1" Background="LightCyan">
<TextBlock Text="右クリックでメッセージ ボックスを表示します。" />
</Border>
<Border Grid.Row="1" x:Name="border2" Background="LightYellow">
<TextBlock Text="マウスのホイール操作で色が変わります。" />
</Border>
<Canvas Grid.Row="2" x:Name="canvas1" Background="LightSkyBlue">
<TextBlock Text="右クリックでメニューを表示します。" />
<Popup x:Name="contextMenu">
<ListBox>
<TextBlock Text="Blue" MouseLeftButtonUp="menu_MouseLeftButtonUp" />
<TextBlock Text="Red" MouseLeftButtonUp="menu_MouseLeftButtonUp" />
<TextBlock Text="Green" MouseLeftButtonUp="menu_MouseLeftButtonUp" />
<TextBlock Text="Yellow" MouseLeftButtonUp="menu_MouseLeftButtonUp" />
</ListBox>
</Popup>
</Canvas>
</Grid>
</Window>
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MouseEvents
{
public partial class MainWindow : Window
{
Boolean LeftButtonDown = false;
int colorValue = 0;
public MainWindow()
{
InitializeComponent();
//border1.MouseRightButtonDown += new MouseButtonEventHandler(border1_MouseRightButtonDown);
//border1.MouseRightButtonUp += new MouseButtonEventHandler(border1_MouseRightButtonUp);
//border2.MouseWheel += new MouseWheelEventHandler(border2_MouseWheel);
//canvas1.MouseRightButtonDown += new MouseButtonEventHandler(canvas1_MouseRightButtonDown);
//canvas1.MouseRightButtonUp += new MouseButtonEventHandler(canvas1_MouseRightButtonUp);
}
private void border1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void border1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("右クリックしました");
}
private void border2_MouseWheel(object sender, MouseWheelEventArgs e)
{
colorValue += e.Delta / 4;
if (colorValue < 0) colorValue = 0;
if (colorValue > 255) colorValue = 255;
Color c = Color.FromArgb(255,
255, Convert.ToByte(255 - colorValue), Convert.ToByte(colorValue));
border2.Background = new SolidColorBrush(c);
}
private void canvas1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void canvas1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
Point pt = e.GetPosition(canvas1);
contextMenu.SetValue(Canvas.LeftProperty, pt.X);
contextMenu.SetValue(Canvas.TopProperty, pt.Y);
contextMenu.IsOpen = true;
}
private void menu_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (sender is TextBlock)
{
TextBlock tblock = sender as TextBlock;
System.Reflection.PropertyInfo pinfo = typeof(Colors).GetProperty(tblock.Text);
if (pinfo != null)
{
Color c = (Color)pinfo.GetValue(null, null);
canvas1.Background = new SolidColorBrush(c);
}
}
contextMenu.IsOpen = false;
}
}
}