728x90
반응형
728x170
▶ MouseColorSelectBehavior.cs
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
}
}
728x90
▶ MainWindow.xaml
<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>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] ListBox 엘리먼트 : 항목 추가시 애니메이션 사용하기 (0) | 2021.02.04 |
---|---|
[C#/WPF] Window 클래스 : WINFORM Control 클래스의 BringToFront 메소드 흉내내기 (0) | 2021.02.03 |
[C#/WPF] Window 클래스 : WINFORM Control 클래스의 BringToFront 메소드 흉내내기 (0) | 2021.02.02 |
[C#/WPF] 잠금 화면 설정하기 (기능 개선) (0) | 2021.02.02 |
[C#/WPF] ScrollViewer 클래스 : 마우스를 사용해 스크롤하기 (0) | 2021.01.27 |
[C#/WPF] ScrollViewer 클래스 : 마우스를 사용해 스크롤하기 (0) | 2021.01.26 |
[C#/WPF] Behavior<T> 클래스 : 윈도우 종료시 동작 사용하기 (0) | 2021.01.26 |
[C#/WPF] Behavior<T> 클래스 : 윈도우 종료시 동작 사용하기 (0) | 2021.01.26 |
[C#/WPF] Image 엘리먼트 : 이미지 흐릿하게 출력 방지하기 (0) | 2021.01.24 |
[C#/WPF] Window 클래스 : DPI 인식 윈도우 사용하기 (0) | 2021.01.24 |
댓글을 달아 주세요