Silverlight 开源项目分析Live Geometry CTP 2(2)项目框架分析

Silverlight 开源项目分析Live Geometry CTP 2(2)项目框架分析

在线地址:http://geometry.osenkov.com

源码地址:http://livegeometry.codeplex.com/

没看懂Live Geometry只好自己摸索了。
Silverlight 开源项目分析Live Geometry CTP 2(2)项目框架分析

复制代码
大气象
< UserControl  x:Class ="SilverlightUndo.DrawDrag"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
    Width
="400"  Height ="300" >
    
< Grid  x:Name ="LayoutRoot"  Background ="White" >
        
< Grid.RowDefinitions >
            
< RowDefinition  Height ="50" ></ RowDefinition >
            
< RowDefinition  Height ="*" ></ RowDefinition >
        
</ Grid.RowDefinitions >
        
< Grid.ColumnDefinitions >
            
< ColumnDefinition  Width ="50" ></ ColumnDefinition >
            
< ColumnDefinition  Width ="50" ></ ColumnDefinition >
            
< ColumnDefinition  Width ="*" ></ ColumnDefinition >
        
</ Grid.ColumnDefinitions >
        
< Button  Grid.Row ="0"  Grid.Column ="0"  Content ="拖动"  Name ="btnDrag"  Click ="btnDrag_Click" ></ Button >
        
< Button  Grid.Row ="0"  Grid.Column ="1"  Content ="直线"  Name ="btnLine"  Click ="btnLine_Click" ></ Button >
        
< Canvas  Name ="Board"  Grid.Row ="1"  Grid.Column ="0"  Grid.ColumnSpan ="3"  Background ="Black"  MouseLeftButtonDown ="Canvas_MouseLeftButtonDown" ></ Canvas >
    
</ Grid >
</ UserControl >

 


复制代码
复制代码
大气象
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

namespace  SilverlightUndo
{
    
public   partial   class  DrawDrag : UserControl
    {
        Ellipse ellipse 
=   new  Ellipse(); // 画点
         int  iOper  =   1 ; // 默认画直线
         private   bool  IsStart  =   true ; // 是否是起点。
         private   double  pX1  =   0 , pY1  =   0 ; // 起点坐标
        
//
         bool  trackingMouseMove  =   false ;
        Point mousePosition;
        
public  DrawDrag()
        {
            InitializeComponent();
        }

        
private   void  btnDrag_Click( object  sender, RoutedEventArgs e)
        {
            iOper 
=   0 ;
        }

        
private   void  btnLine_Click( object  sender, RoutedEventArgs e)
        {
            iOper 
=   1 ;
        }
        
private   void  Canvas_MouseLeftButtonDown( object  sender, MouseButtonEventArgs e)
        {
            
if  (iOper  ==   0 return ; // 如果是拖动状态
            Point p  =  e.GetPosition(sender  as  FrameworkElement); // 取得鼠标点下的位置
            Canvas pnl  =  sender  as  Canvas;
            
if  (IsStart)
            {
                pX1 
=  p.X; // 设置起点坐标
                pY1  =  p.Y;
            }
            
if  (IsStart) // 如果是起点,则画起点。
            {
                DrawPoint(pnl, p.X, p.Y);
            }
            
else
            {
                DrawOneLine(pnl, pX1, pY1, p.X, p.Y);
            }
            IsStart 
=   ! IsStart;
        }
        
// 画点
         private   void  DrawPoint(Canvas pnl,  double  pX1,  double  pY1)
        {
            
            ellipse.Stroke 
=   new  SolidColorBrush(Color.FromArgb( 255 255 255 255 )); // 动态设置Stroke属性的方法。
            ellipse.StrokeThickness  =   2 ;
            ellipse.Width 
=   4 ;
            ellipse.Height 
=   4 ;
            Canvas.SetLeft(ellipse, pX1);
// 动态设置Ellipse的Canvas.Top与Canvas.Left
            Canvas.SetTop(ellipse, pY1);

            pnl.Children.Add(ellipse);
        }
        
// 画直线
         private   void  DrawOneLine(Canvas pnl,  double  pX1,  double  pY1,  double  pX2,  double  pY2)
        {
            Line line 
=   new  Line();
            line.X1 
=  pX1;
            line.Y1 
=  pY1;
            line.X2 
=  pX2;
            line.Y2 
=  pY2;
            
// 注册事件
            line.MouseLeftButtonDown  +=   new  MouseButtonEventHandler(OnMouseDown);
            line.MouseMove 
+=   new  MouseEventHandler(OnMouseMove);
            line.MouseLeftButtonUp 
+=   new  MouseButtonEventHandler(OnMouseUp);

            line.Stroke 
=   new  SolidColorBrush(Color.FromArgb( 255 255 255 255 ));
            line.StrokeThickness 
=   4 ;

            pnl.Children.Add(line);
            pnl.Children.Remove(ellipse);
        }

        
void  line_MouseLeftButtonDown( object  sender, MouseButtonEventArgs e)
        {
            
throw   new  NotImplementedException();
        }
        
//
         void  OnMouseDown( object  sender, MouseButtonEventArgs e)
        {
            
if  (iOper  ==   1 return ;
            FrameworkElement element 
=  sender  as  FrameworkElement; // 当前元件
            mousePosition  =  e.GetPosition( null ); // 鼠标位置
            trackingMouseMove  =   true ;
            
if  ( null   !=  element)
            {
                element.CaptureMouse();
// 设置鼠标捕获
                element.Cursor  =  Cursors.Hand;
                Line line 
=  (Line)element; // 选中
                line.Stroke  =   new  SolidColorBrush(Colors.Red);
            }
        }
        
void  OnMouseMove( object  sender, MouseEventArgs e)
        {
            
if  (iOper  ==   1 return ;
            FrameworkElement element 
=  sender  as  FrameworkElement;
            
if  (trackingMouseMove)
            {
                
// 计算位置
                 double  deltaV  =  e.GetPosition( null ).Y  -  mousePosition.Y;
                
double  deltaH  =  e.GetPosition( null ).X  -  mousePosition.X;
                
double  newTop  =  deltaV  +  ( double )element.GetValue(Canvas.TopProperty);
                
double  newLeft  =  deltaH  +  ( double )element.GetValue(Canvas.LeftProperty);
                
// GetValue();SetValue();
                element.SetValue(Canvas.TopProperty, newTop);
                element.SetValue(Canvas.LeftProperty, newLeft);

                mousePosition 
=  e.GetPosition( null );
            }
        }
        
void  OnMouseUp( object  sender, MouseButtonEventArgs e)
        {
            
if  (iOper  ==   1 return ;
            FrameworkElement element 
=  sender  as  FrameworkElement;
            trackingMouseMove 
=   false ;
            element.ReleaseMouseCapture();
// 释放鼠标

            mousePosition.X 
=  mousePosition.Y  =   0 ;
            element.Cursor 
=   null ;
            Line line 
=  (Line)element; // 失去焦点
            line.Stroke  =   new  SolidColorBrush(Colors.White);
        }
    }
}
复制代码

源码:http://files.cnblogs.com/greatverve/SilverlightDrawDrag.rar

凡事以大气象去面对,优秀是一种习惯。

你可能感兴趣的:(silverlight)