MainWindow.xaml.csの全コード
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;
}
}
}