目标: 实现一个效果: 在桌面实现任意拖动一个border元素里面的控件,并且规定 元素拖动的区域
环境:silverlight4.0
实现的步骤我就不啰嗦啦,代码奉上:
xaml代码(注意,原来的界面很复杂,为了方便大家看,一些修饰的样式没了,仅供参考,可以自行的修改)
<Border x:Name="Border1" BorderThickness="1" Visibility="Visible" Canvas.ZIndex="1" Canvas.Left="0" Canvas.Top="0" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" MouseMove="StackPanel_MouseMove" >
<StackPanel x:Name="sds" Orientation="Horizontal" Background="#424D85" Height="60" >
<Button x:Name="btnAdd" Height="50" Width="50" Margin="2,0,2,0" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="新建" Width="22" Height="22" FontSize="9"/>
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="btnUpd" Height="50" Width="50" Margin="2,0,2,0" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="修改" Width="22" Height="22" FontSize="9"/>
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="btnSave" Height="50" Width="50" Margin="2,0,2,0">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="保存" Width="22" Height="22" FontSize="9"/>
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="btnDel" Height="50" Width="50" Margin="2,0,2,0" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="删除" Width="22" Height="22" FontSize="9"/>
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="btnQry" Height="50" Width="50" Margin="2,0,2,0">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="查询" Width="22" Height="22" FontSize="9" />
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="btnRefresh" Height="50" Width="50" Margin="2,0,2,0">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="刷新" Width="22" Height="22" FontSize="9"/>
</StackPanel>
</Button.Content>
</Button>
<Button Content="关闭" Height="50" Width="50" ></Button>
</StackPanel>
</Border>
看看后台啦:
#region 工具栏随意拖动事件
//桌面按钮拖放变量
bool trackingMouseMove = false;
Point mousePosition;
Border BtnCurrent = new Border(); //当前移动的按钮
bool isMoved = false; //是否曾经移动
/// <summary>
/// 鼠标左键被按下
/// <author>Sun</author>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
trackingMouseMove = true;
BtnCurrent = (Border)sender;
if (null != element)
{
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
/// <summary>
/// 鼠标左键被弹起来
/// <author>Sun</author>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
BtnCurrent = (Border)sender;
double ileft = System.Convert.ToDouble(this.sds.GetValue(Canvas.LeftProperty));
double iTop = System.Convert.ToDouble(this.sds.GetValue(Canvas.TopProperty));
if (IsInRegions(mousePosition)) //如果超出了规定的区域,就让
{
Point p= e.GetPosition((e.OriginalSource as FrameworkElement));
BtnCurrent.SetValue(Canvas.LeftProperty,0.0);
BtnCurrent.SetValue(Canvas.TopProperty,0.0);
}
else
{
mousePosition.X = mousePosition.Y = 0;
this.sds.SetValue(Canvas.TopProperty, iTop + 52);
this.sds.SetValue(Canvas.LeftProperty, ileft + 52);
}
if (!isMoved) //单击按钮 但是用户没有拖动 如果拖动了 这个不能运行
{ }
isMoved = false;
}
/// <summary>
/// <author>Sun</author>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
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);
element.SetValue(Canvas.TopProperty, newTop);
element.SetValue(Canvas.LeftProperty, newLeft);
isMoved = true; //在移动时候 开关变量 为真
mousePosition = e.GetPosition(null);
}
}
/// <summary>
/// <author>Sun</author>
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
private bool IsInRegions(Point p)
{
double x = p.X;
double y = p.Y;
return y < 88 || x < 356|| x>1400 || y>660;//规定的临界范围
}
#endregion 工具栏拖动事件结束
就写到这,时间很紧,仓促的和大家分享啦