■ Behavior<T> 클래스에서 마우스를 사용해 색상 선택 동작을 만드는 방법을 보여준다.
▶ MouseColorSelectBehavior.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interactivity; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 마우스 색상 선택 동작 /// </summary> public class MouseColorSelectBehavior : Behavior<Image> { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 선택 색상 속성 - SelectedColorProperty /// <summary> /// 선택 색상 속성 /// </summary> public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register ( "SelectedColor", typeof(Color), typeof(MouseColorSelectBehavior), new UIPropertyMetadata(Colors.White) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 선택 색상 - SelectedColor /// <summary> /// 선택 색상 /// </summary> public Color SelectedColor { get { return (Color)GetValue(SelectedColorProperty); } set { SetValue(SelectedColorProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.MouseDown += AssociatedObject_MouseDown; AssociatedObject.MouseMove += AssociatedObject_MouseMove; } #endregion #region 탈착시 처리하기 - OnDetaching() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.MouseDown -= AssociatedObject_MouseDown; AssociatedObject.MouseMove -= AssociatedObject_MouseMove; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 결합 객체 마우스 DOWN 처리하기 - AssociatedObject_MouseDown(sender, e) /// <summary> /// 결합 객체 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs e) { SetSelectedColor(); } #endregion #region 결합 객체 마우스 이동시 처리하기 - AssociatedObject_MouseMove(sender, e) /// <summary> /// 결합 객체 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_MouseMove(object sender, MouseEventArgs e) { if(e.LeftButton == MouseButtonState.Pressed) { SetSelectedColor(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 선택 색상 설정하기 - SetSelectedColor() /// <summary> /// 선택 색상 설정하기 /// </summary> private void SetSelectedColor() { Point point = Mouse.GetPosition(AssociatedObject); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap ( (int)AssociatedObject.ActualWidth, (int)AssociatedObject.ActualHeight, 96, 96, PixelFormats.Default ); renderTargetBitmap.Render(AssociatedObject); if((point.X <= renderTargetBitmap.PixelWidth) && (point.Y <= renderTargetBitmap.PixelHeight)) { CroppedBitmap croppedBitmap = new CroppedBitmap ( renderTargetBitmap, new Int32Rect((int)point.X, (int)point.Y, 1, 1) ); byte[] pixelByteArray = new byte[4]; croppedBitmap.CopyPixels(pixelByteArray, 4, 0); SelectedColor = Color.FromArgb(255, pixelByteArray[2], pixelByteArray[1], pixelByteArray[0]); } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="Behavior<T> 클래스 : 마우스를 사용해 색상 선택 동작 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Image Grid.Column="0" Stretch="Fill" Width="500" Height="500" Source="IMAGE\sample.png"> <i:Interaction.Behaviors> <local:MouseColorSelectBehavior x:Name="behavior" /> </i:Interaction.Behaviors> </Image> <Rectangle Grid.Column="2" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Bottom" Stroke="Black" StrokeThickness="1" Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush Color="{Binding ElementName=behavior, Path=SelectedColor}" /> </Rectangle.Fill> </Rectangle> </Grid> </Grid> </Window> |