3
我想模擬WPF中的拖放事件。
爲此,我需要訪問存儲在「拖放緩衝區」中的數據,並且還需要創建一個DragEventArgs
。 我注意到DragEventArgs
是密封的,沒有公共的ctor。模擬WPF中的拖放事件
所以我的問題是:
1.如何創建DragEventArgs
的實例?
2.如何訪問拖放緩衝區?
我想模擬WPF中的拖放事件。
爲此,我需要訪問存儲在「拖放緩衝區」中的數據,並且還需要創建一個DragEventArgs
。 我注意到DragEventArgs
是密封的,沒有公共的ctor。模擬WPF中的拖放事件
所以我的問題是:
1.如何創建DragEventArgs
的實例?
2.如何訪問拖放緩衝區?
我最近這樣做!我使用MouseDown,MouseMove和MouseUp事件模擬拖放。例如我的應用程序,我有一些畫布,我想要拖放它們。每個畫布都有一個ID。在MouseDown事件中,我緩存它的id並在MouseMove和MouseUp事件中使用它。 Desktop_Canvas是我的主帆布,其中包含一些畫布。這些油畫是在我的字典(詞典)。
這裏是我的代碼:
private Dictionary<int, Win> dic = new Dictionary<int, Win>();
private Point downPoint_Drag = new Point(-1, -1);
private int id_Drag = -1;
private bool flag_Drag = false;
public class Win
{
public Canvas canvas = new Canvas();
public Point downpoint = new Point();
public Win()
{
canvas.Background = new SolidColorBrush(Colors.Gray);
}
}
private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
{
try
{
Point movePoint = e.GetPosition(Desktop_Canvas);
if (flag_Drag && downPoint_Drag != new Point(-1, -1))
{
double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
downPoint_Drag = movePoint;
if (x == -1)
x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
if (y == -1)
y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
Canvas.SetLeft(dic[id_Drag].canvas, x);
Canvas.SetTop(dic[id_Drag].canvas, y);
}
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
downPoint_Drag = new Point(-1, -1);
id_Drag =-1;
flag_Drag = false;
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
{
MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
Desktop_Canvas_MouseLeftButtonUp(null, ee);
}
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
downPoint_Drag = e.GetPosition(Desktop_Canvas);
int hoverId = HoverWin(downPoint_Drag);
flag_Drag = true;
id_Drag = hoverId;
dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
}
private int HoverWin(Point p)
{
foreach (int i in dic.Keys)
{
if (dic[i].canvas.IsMouseOver)
return i;
}
return -1;
}