Move事件,拖动控件的问题,内附代码,请大家帮帮忙吧~!

ylly2508 2010-04-14 01:03:41
用鼠标拖动容器,然后让容器上的控件按照鼠标走的值去走,用的是故事版,但是拖动快的时候,为什么会串位呢?

public Point mouseStart , mouseEnd;//记录鼠标的起始点,来计算移动位置
public void MouseLeftDown( object sender , EventArgs e )
{
mouseStart=Mouse.GetPosition( this );
}
public void Move( object sender , EventArgs e )
{
mouseEnd=Mouse.GetPosition( this );
double y=mouseEnd.Y-mouseStart.Y;
//Menumain 是用户控件,grid是容器
Menumain menu=grid.Children[1] as Menumain;
DB.DB.timeSpan=0;
items item=null;
if ( y!=0 )
{
for ( int i=0 ; i<menu.sdf.Length ; i++ )
{
item=menu.sdf[i] as items;

if ( item.Margin.Top+y>=336 && item.Margin.Top+y<=528.5 )
{
storyTapMove( item , 216 , item.Margin.Top+menuSlidePointY( item.Margin.Top , i , y ) , DB.DB.timeSpan );
DB.DB.NowIng=i+1;//这个是记录中心位置的

}
else
{

storyTapMove( item , 240, item.Margin.Top+menuSlidePointY( item.Margin.Top , i , y ) , DB.DB.timeSpan );
}
}

}

}


#region 返回控件应移动的距离(正常值和特殊值的正常比例)
/// <summary>
/// 返回控件应移动的距离(正常值和特殊值的正常比例)
/// </summary>
/// <param name="top"></param>
/// <param name="i"></param>
/// <param name="y"></param>
/// <returns></returns>
public float menuSlidePointY( double top , int i , float y )
{
float temp=y/70;
if ( top>382 &&top <675 )
{
return 293*temp;
}
else if ( top<382 && top>290 )
{
return 92*temp;
}
return y;
}
#endregion


#region Thickness移动主菜单动画(按margin移动)
/// <summary>
/// Thickness移动主菜单动画(按margin移动)
/// </summary>
/// <param name="item">移动控件</param>
/// <param name="setLeft">控件靠左侧宽度</param>
/// <param name="setTop">控件靠顶部宽度</param>
public void storyTapMove( items item , double setLeft , double setTop , int timeSpan )
{
Storyboard sb = new Storyboard( );
//sb.FillBehavior = FillBehavior.Stop;
//item.BeginStoryboard( sb , HandoffBehavior.SnapshotAndReplace , true );

TransformGroup tfg = new TransformGroup( );
ScaleTransform sf = new ScaleTransform( );
SkewTransform qx = new SkewTransform( );
RotateTransform xz = new RotateTransform( );
TranslateTransform py = new TranslateTransform( );

tfg.Children.Add( sf );
tfg.Children.Add( qx );
tfg.Children.Add( xz );
tfg.Children.Add( py );

ThicknessAnimationUsingKeyFrames animatX=new ThicknessAnimationUsingKeyFrames( );

SplineThicknessKeyFrame sk=new SplineThicknessKeyFrame( new Thickness( setLeft , setTop , 0 , 0 ) , KeyTime.FromTimeSpan( new TimeSpan( 0 , 0 , 0 , 0 , timeSpan ) ) , new KeySpline( 0.37 , 1 , 1 , 1 ) );

animatX.KeyFrames.Add( sk );

Storyboard.SetTarget( animatX , item );
Storyboard.SetTargetProperty( animatX , new PropertyPath( "(FrameworkElement.Margin)" ) );

sb.Children.Add( animatX );
sb.Begin( );
}
#endregion





...全文
146 10 打赏 收藏 转发到动态 举报
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylly2508 2010-05-06
  • 打赏
  • 举报
回复
还是没有解决啊~~自己顶下~~
ylly2508 2010-04-22
  • 打赏
  • 举报
回复
好久了啊~~~都没有解决~~~有思路的能说说不???
mengKzhaoyun 2010-04-22
  • 打赏
  • 举报
回复
拖动的时候出现闪烁很可能是拖动占用了太多的系统资源,我觉得可以模仿一下Window的任务管理器,当用户点击要拖动的控件时首先获取描述其长宽的一个矩形,然后拖动这个矩形,拖动完成之后将控件移动到新的地方,这样可能比直接拖动控件更加的节约资源.
beniao277 2010-04-22
  • 打赏
  • 举报
回复
根据鼠标的点去移动 ,你可以自己对鼠标坐标进行统计,让物体根据坐标进行移动。
ylly2508 2010-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuanxinxuan 的回复:]
// <summary>
/// 移动过程
/// </summary>
private void drawCanv_MouseMove(object sender, MouseEventArgs e)
{
ElementLayer el = MainMap.Layers[2] as ElementLayer;
if (gisState == GisState.……
[/Quote]
请问你的意思是说将控件删除,然后在要移动到的那个位置,添加上么?那样会闪啊~~,而且控件距离越小,闪的越厉害~~ —.—!
ylly2508 2010-04-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beniao277 的回复:]
namespace xxxx.Controls
{
public class DragControl : UserControl
{
public delegate void DragerEventHandler(UIElement sender, EventArgs e);

public event DragerEventHandler DragerMove;
……
[/Quote]
首先感谢你的帮助~~~
看了下你的代码,你是通过改变控件高度,然后改变zIndex值,让控件显示在最上边,但是如果是并排排列的控件,并且通过Canvas.SetLeft和Canvas.SetTop定义的位置已固定,那么,高度变大的控件,就会覆盖到下面的控件上。这样并不能解决,控件之间距离变大的问题啊~~
XuanXInXuan 2010-04-15
  • 打赏
  • 举报
回复
// <summary>
/// 移动过程
/// </summary>
private void drawCanv_MouseMove(object sender, MouseEventArgs e)
{
ElementLayer el = MainMap.Layers[2] as ElementLayer;
if (gisState == GisState.Edit && editStation != null)
{
System.Windows.Point screenPoint = e.GetPosition(MainMap);
ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MainMap.ScreenToMap(screenPoint);
ElementLayer.SetEnvelope(editStation, mapPoint.Extent);
var value = editStation;
el.Children.Remove(value);
el.Children.Add(value);
}
}
beniao277 2010-04-15
  • 打赏
  • 举报
回复
namespace xxxx.Controls
{
public class DragControl : UserControl
{
public delegate void DragerEventHandler(UIElement sender, EventArgs e);

public event DragerEventHandler DragerMove;
public event DragerEventHandler DragerMoveStop;

private bool isMouseDown;
public Point lastPosition;
public Point elPosition;
int zIndex = 0;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this.Cursor = Cursors.Hand;
isMouseDown = true;
BringToTop();
this.CaptureMouse();
lastPosition = e.GetPosition(Application.Current.RootVisual);
elPosition = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
isMouseDown = false;
zIndex = 0;
this.ReleaseMouseCapture();
this.Cursor = Cursors.Arrow;
base.OnMouseLeftButtonUp(e);
if (DragerMoveStop != null) { DragerMoveStop(this, e); }
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isMouseDown)
{
Point currentPosition = e.GetPosition(Application.Current.RootVisual);
double curX = currentPosition.X - lastPosition.X + elPosition.X;
double curY = currentPosition.Y - lastPosition.Y + elPosition.Y;
SetPosition(new Point(curX, curY));
lastPosition = currentPosition;
if (DragerMove != null)
{
DragerMove(this, e);
}
}
base.OnMouseMove(e);
}

public virtual void SetPosition(Point p)
{
Canvas.SetLeft(this, p.X);
Canvas.SetTop(this, p.Y);
elPosition = p;
//var mainCanvas = this.Parent as Canvas;
//mainCanvas.SetValue(BackgroundProperty,new SolidColorBrush(Colors.Red));
//mainCanvas.Height = 3000;
}
private double GetMaxHeight(Canvas panel)
{
double height = 0;
foreach (UIElement ele in panel.Children)
{
if (Canvas.GetTop(ele) > height)
height = Canvas.GetTop(ele);
}
return height;
}

public virtual Point GetPosition()
{
return this.elPosition;
}
public void Show()
{
this.Visibility = Visibility.Visible;
}
public void Hide()
{
this.Visibility = Visibility.Collapsed;
}
protected void BringToTop()
{
if (zIndex == 0)
{
var oldIndex = this.zIndex;
var mainCanvas = this.Parent as Canvas;
foreach (FrameworkElement fElement in mainCanvas.Children)
{
Canvas.SetZIndex(fElement, 0);
}
Canvas.SetZIndex(this, 2);
zIndex = 1;
}
}
}
}


我写的基础控件,供参考。
ylly2508 2010-04-14
  • 打赏
  • 举报
回复
还希望有高手指点下啊~~~

还有,控件之间的距离是不平均的,中间因为有一个要显示图片,所以距离比较大,我感觉就是这段距离,弄得控件走的不一样了,

但是我用

public float menuSlidePointY( double top , int i , float y )
{
float temp=y/70;
if ( top>382 &&top <675 )
{
return 293*temp;
}
else if ( top<382 && top>290 )
{
return 92*temp;
}
return y;
}

这个方法来获得比较大的距离的比例~~~

还请大家多多帮忙啊~~~!!!
TimDavid 2010-04-14
  • 打赏
  • 举报
回复
这个问题我也遇到过,我写的拖动控件也是。
到现在还是找不到好的方法解决。
相关推荐

8,707

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告