#713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)

原文地址:https://wpf.2000things.com/2012/12/17/713-setting-the-cursor-to-an-image-of-an-uielement-while-dragging/

在拖拽操作的过程中,你可以通过GiveFeedback 事件的处理函数来修改鼠标光标的样子。甚至可以将拖拽控件生成图片作为鼠标光标。

下面是根据Brandon Cannaday的代码 http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-custom-cursors 写的一个例子。他的代码有给鼠标帮助类,

public class CursorHelper
{
    private static class NativeMethods
    {
        public struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }
 
        [DllImport("user32.dll")]
        public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon);
 
        [DllImport("user32.dll")]
        public static extern bool DestroyIcon(IntPtr hIcon);
 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    }
 
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    private class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        public SafeIconHandle()
            : base(true)
        {
        }
 
        override protected bool ReleaseHandle()
        {
            return NativeMethods.DestroyIcon(handle);
        }
    }
 
    private static Cursor InternalCreateCursor(System.Drawing.Bitmap bmp)
    {
        var iconInfo = new NativeMethods.IconInfo();
        NativeMethods.GetIconInfo(bmp.GetHicon(), ref iconInfo);
 
        iconInfo.xHotspot = 0;
        iconInfo.yHotspot = 0;
        iconInfo.fIcon = false;
 
        SafeIconHandle cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);
        return CursorInteropHelper.Create(cursorHandle);
    }
 
    public static Cursor CreateCursor(UIElement element)
    {
        element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        element.Arrange(new Rect(new Point(), element.DesiredSize));
 
        RenderTargetBitmap rtb =
          new RenderTargetBitmap(
            (int)element.DesiredSize.Width,
            (int)element.DesiredSize.Height,
            96, 96, PixelFormats.Pbgra32);
 
        rtb.Render(element);
 
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(rtb));
 
        using (var ms = new MemoryStream())
        {
            encoder.Save(ms);
            using (var bmp = new System.Drawing.Bitmap(ms))
            {
                return InternalCreateCursor(bmp);
            }
        }
    }
}
然后我们做一个简单的界面,将一个Label拖入另一个Label中,并且将Label作为鼠标。

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="45">
    <Label Content="Data to drag" Background="AliceBlue" Padding="15,10"
           MouseLeftButtonDown="Label_MouseLeftButtonDown"
           GiveFeedback="Label_GiveFeedback"/>
    <Label Content="Drag to here" Background="MediumSpringGreen" Padding="15,10" Margin="20"
           AllowDrop="True" Drop="Label_Drop"/>
</StackPanel>
下面是拖拽操作的代码,在 GiveFeedback 事件处理函数中判断是否是Copy操作,如果是则改变鼠标。

private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataObject data = new DataObject(DataFormats.Text, ((Label)e.Source).Content);
 
    DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy);
}
 
private void Label_Drop(object sender, DragEventArgs e)
{
    ((Label)e.Source).Content = (string)e.Data.GetData(DataFormats.Text);
}
 
private Cursor customCursor = null;
 
private void Label_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    if (e.Effects == DragDropEffects.Copy)
    {
        if (customCursor == null)
            customCursor = CursorHelper.CreateCursor(e.Source as UIElement);
 
        if (customCursor != null)
        {
            e.UseDefaultCursors = false;
            Mouse.SetCursor(customCursor);
        }
    }
    else
        e.UseDefaultCursors = true;
 
    e.Handled = true;
}


在 WPF ,可以使用鼠标事件和 Canvas.SetLeft / Canvas.SetTop 方法来实现拖动控件在 Canvas 上移动的效果。以下是一个示例: ```xaml <Canvas> <Button Content="Drag me!" Canvas.Left="50" Canvas.Top="50" MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseMove="Button_MouseMove" MouseLeftButtonUp="Button_MouseLeftButtonUp"/> </Canvas> ``` 在代码,我们为 Button 控件注册了三个鼠标事件:MouseLeftButtonDown、MouseMove 和 MouseLeftButtonUp。这三个事件将分别在鼠标按下、移动和松开时触发。 ```csharp private bool isDragging; private Point startPoint; private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 开始拖动 isDragging = true; startPoint = e.GetPosition(null); ((UIElement)sender).CaptureMouse(); } private void Button_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { // 计算拖动距离 Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; // 移动控件 Button button = sender as Button; double left = Canvas.GetLeft(button); double top = Canvas.GetTop(button); Canvas.SetLeft(button, left - diff.X); Canvas.SetTop(button, top - diff.Y); // 更新起始点 startPoint = mousePos; } } private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // 结束拖动 isDragging = false; ((UIElement)sender).ReleaseMouseCapture(); } ``` 在这个示例,我们首先定义了两个变量:isDragging 和 startPoint。isDragging 变量表示当前是否正在拖动控件,startPoint 变量表示拖动开始时鼠标的位置。 在 MouseLeftButtonDown 事件,我们将 isDragging 设为 true,并记录当前鼠标的位置。然后,我们调用 CaptureMouse 方法来捕获鼠标,以确保鼠标移动事件被正确处理。 在 MouseMove 事件,我们首先判断当前是否正在拖动。如果是,就计算出当前鼠标位置和上一次鼠标位置的差值,然后使用 Canvas.SetLeft 和 Canvas.SetTop 方法更新控件的位置。最后,我们将当前鼠标位置保存到 startPoint 变量,以便下一次计算差值。 在 MouseLeftButtonUp 事件,我们将 isDragging 设为 false,并调用 ReleaseMouseCapture 方法来释放鼠标捕获。 通过这种方式,我们就可以实现在 Canvas 上拖动控件移动的效果。如果需要支持多个控件同时拖动,可以使用类似的方法为每个控件注册鼠标事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值