MainPage.xaml |
[그림 50-1] <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <Button x:Name="btnClear" Content="전u체¨ù 지o우¯i기¾a"></Button> <InkPresenter x:Name="ipPen" Background="Silver" Width="400" Height="300"/> </StackPanel> </Grid> |
MainPage.xaml.cs |
public partial class MainPage : UserControl { private
Stroke stroke; //스트로크 개체 : 펜의 모양, 점 하나의 모양을 나타냄 private
Brush brush = new
SolidColorBrush(Colors.Red);
//[A] 펜의 색상 저장 public
MainPage() { InitializeComponent(); //4가지 마우스 관련 이벤트 등록 this.ipPen.MouseLeftButtonDown += new MouseButtonEventHandler(ipPen_MouseLeftButtonDown); this.ipPen.MouseMove
+= new MouseEventHandler(ipPen_MouseMove); this.ipPen.MouseLeftButtonUp += new MouseButtonEventHandler(ipPen_MouseLeftButtonUp); this.ipPen.MouseLeave
+= new MouseEventHandler(ipPen_MouseLeave); this.ipPen.LostMouseCapture
+= new MouseEventHandler(ipPen_LostMouseCapture); this.btnClear.Click
+= new System.Windows.RoutedEventHandler(btnClear_Click); this.ipPen.KeyDown
+= new KeyEventHandler(ipPen_KeyDown); this.LayoutRoot.KeyDown
+= new KeyEventHandler(ipPen_KeyDown);//[D]펜 색상 변경 } //[0] 펜의 모양 초기화 private
void InitializePen() { //[C] 펜 색상 적용 this.stroke.DrawingAttributes.Color
= ((SolidColorBrush)brush).Color; this.stroke.DrawingAttributes.OutlineColor
= Colors.Blue; // 테두리 색상은 Blue } //[1] 왼쪽 void
ipPen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //현재 잉크 영역에서 마우스를 캡쳐 ipPen.CaptureMouse(); //마우스 위치 캡쳐 //현재 잉크의 위치를 기준으로 마우스 (스타일러스) 정보를 스트로크에 담음 this.stroke
= new Stroke(e.StylusDevice.GetStylusPoints(ipPen));
//펜 색상 초기화 InitializePen();
//모양 출력 ipPen.Strokes.Add(stroke); } //[2] 마우스 이동 void
ipPen_MouseMove(object sender, MouseEventArgs e) { //현재 설정된 스트로크가 있다면... if
(stroke != null) { //이동할 때마다 좌표값을 얻어서 해당 스트로크를 출력 : 스트로크 색상과 위치추가 this.stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(ipPen)); } } //[3]마우스 왼쪽버튼 놓기 void
ipPen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ipPen.ReleaseMouseCapture(); //마우스 캡쳐 stroke = null;
//스트로크 지우기 } //[4]영역을 벗어날 때 void
ipPen_MouseLeave(object sender, MouseEventArgs e) { ipPen_MouseLeftButtonUp(null, null); //[3]번 메서드 호출 } //[5]마우스 캡쳐를 잃었을 때 void
ipPen_LostMouseCapture(object sender, MouseEventArgs e) { stroke = null; } //[6]전체 지우기 버튼 클릭시 void
btnClear_Click(object sender, RoutedEventArgs e) { ipPen.Strokes.Clear(); //포함된 모든 Stroke를 클리어 } //[7]키보드의 키가 눌렸을 때 void
ipPen_KeyDown(object sender, KeyEventArgs e) { if
(e.Key == Key.R) { brush = new SolidColorBrush(Colors.Red); //[B]현재 펜의 색상을 Red } else
if (e.Key == Key.G) { brush = new SolidColorBrush(Colors.Green); } else
if (e.Key == Key.B) { brush = new SolidColorBrush(Colors.Blue); } else { brush = new SolidColorBrush(Colors.Black); } } } |
결과화면 |
[그림 50-2] |
'Silverlight' 카테고리의 다른 글
52.SilverLight3 - AttachedProperty (0) | 2009.12.02 |
---|---|
51.SilverLight3 - RoutedEventArgs (0) | 2009.12.01 |
49.SilverLight3 - DragAndDrop (0) | 2009.12.01 |
48.SilverLight3 - TextBlock (0) | 2009.12.01 |
47.SilverLight3 - PasswordBox (0) | 2009.12.01 |
Comments