目标: 实现一个效果: 在桌面实现任意拖动一个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,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" >
<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">
<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" >
<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">
<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">
<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>
/// 鼠标左键被d起来
/// <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 工具栏拖动事件结束
就写到这,时间很紧,仓促的和大家分享啦
@H_301_4@ @H_301_4@ @H_301_4@ 总结以上是内存溢出为你收集整理的silverlight 中鼠标任意拖动控件的实例分享全部内容,希望文章能够帮你解决silverlight 中鼠标任意拖动控件的实例分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)