Silverlight控件的拖拽与缩放

 

源代码下载(vs2010版本):/Files/timy/dragAndreSize.rar

 

以下是拖放控件的实例代码:

< UserControl x:Class = " SilverlightApplication26.Page "
    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 = " #FFD9D7D7 " >
        
< StackPanel >
            
< Border Width = " 100 "  Height = " 50 "  CornerRadius = " 5 "   BorderBrush = " Black "  BorderThickness = " 3 "  MouseLeftButtonDown = " Border_MouseLeftButtonDown "  MouseMove = " Border_MouseMove "  MouseEnter = " Border_MouseEnter "  MouseLeave = " Border_MouseLeave "   MouseLeftButtonUp = " Border_MouseLeftButtonUp " >
                
< Button ></ Button >
            
</ Border >
        
</ StackPanel >
      
     
    
</ 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;
using  System.Xml;
using  System.Windows.Media.Imaging;
using  System.Windows.Resources;
using  System.IO;
using  System.Windows.Browser;
using  System.ServiceModel.Channels;
using  System.ServiceModel;
using  System.Windows.Threading;
using  System.Threading;
 
namespace  SilverlightApplication26
{
    
public   partial   class  Page : UserControl
    {
 
        
public  Page()
        {
            InitializeComponent();

        }
 
        
bool  captured  =   false ;
        
double  ox  =   - 1 , oy  =   - 1 ;
        
enum  State { None, Left, Buttom, Top, Right };
        State currentstate;
        
private   void  Border_MouseLeftButtonDown( object  sender, MouseButtonEventArgs e)
        {
            Border b 
=  (Border)sender;
            b.CaptureMouse();
            captured 
=   true ;
        }
        
private   void  Border_MouseMove( object  sender, MouseEventArgs e)
        {
            CheckCursor(sender, e);
            
if  (captured)
            {
                Border b 
=  (Border)sender;
                Point p 
=  e.GetPosition( null );
                
if  (ox  !=   - 1 )
                {
                    
switch  (currentstate) 
                    {
                        
case  State.Right: { b.Width  +=  (p.X  -  ox) * 2 break ;}
                        
case  State.Buttom: { b.Height  +=  (p.Y  -  oy) * 2 break ; }
                        
case  State.Top: { b.Height  +=  (oy  -  p.Y) * 2 break ; }
                        
case  State.Left: { b.Width  +=  (ox  -  p.X) * 2 break ; }
                    }
                   
                }
                ox 
=  p.X;
                oy 
=  p.Y;
            }
        }
        
private   void  Border_MouseLeftButtonUp( object  sender, MouseButtonEventArgs e)
        {
            Cursor 
=  Cursors.Arrow;
            Border b 
=  (Border)sender;
            b.ReleaseMouseCapture();
            captured 
=   false ;
            ox 
=   - 1 ; oy  =   - 1 ;
        }
        
private   void  Border_MouseLeave( object  sender, MouseEventArgs e)
        {
if ( ! captured)
            Cursor 
=  Cursors.Arrow;
        }
        
private   void  Border_MouseEnter( object  sender, MouseEventArgs e)
        {
    
            CheckCursor(sender, e);
        }
        
void  CheckCursor( object  sender, MouseEventArgs e)
        {
            Border b 
=  (Border)sender;
            Point p 
=  e.GetPosition(b);
            
if  (p.X  >=  b.ActualWidth  -  b.BorderThickness.Right  ||  p.X  <=  b.BorderThickness.Left)
            {
                Cursor 
=  Cursors.SizeWE;
                
if  (p.X  >=  b.ActualWidth  -  b.BorderThickness.Right) { currentstate  =  State.Right; }
                
else  { currentstate  =  State.Left; }
            }
            
else   if  (p.Y  >=  b.ActualHeight  -  b.BorderThickness.Bottom  ||  p.Y  <=  b.BorderThickness.Top)
            {
                Cursor 
=  Cursors.SizeNS;
                
if  (p.Y  >=  b.ActualHeight  -  b.BorderThickness.Bottom) { currentstate  =  State.Buttom; }
                
else
                {
                    currentstate 
=  State.Top;
                }
            }
        }
 
    }

  }


 

 

转载于:https://www.cnblogs.com/timy/archive/2011/03/16/1985762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值