#713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
来源:互联网 发布:餐厅排号软件 编辑:程序博客网 时间:2024/06/05 15:47
原文地址: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;}
阅读全文
0 0
- #713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
- #711 – 在拖拽的过程中改变鼠标样式(Changing the Mouse Cursor While Dragging)
- #251 – 在FlowDocument中嵌入UIElement (Embedding an UIElement Within a FlowDocument)
- Changing The Size Of An Image
- #686 – 使用动画是控件具有闪烁效果(Using Animation to Make an Image Pulse)
- Drawing to an Image
- Where to put the Image in an Image Caption Generator
- convert the contents of an NSData object to an NSString
- An Introduction to the Theory of Computation
- The server has encountered an error while loading an application during the processing of your reque
- The server has encountered an error while loading an application during the processing of your reque
- Setting or removing an image for an CListCtrl item
- Setting an Accessible Name for an Image Button
- How to access pixel data of an image
- Reducing the Number of Gray Levels in an Image
- Changing the contrast and brightness of an image!
- jQuery:mouseover and Increase the Size of an Image
- jQuery:mouseover and Increase the Size of an Image
- 23.使用Intent返回数据给上一个活动
- openstack4j 源码分析(一) 入口
- Andorid webview 常见问题汇总
- git使用使出现的提交问题
- Android 壁纸应用之动态壁纸
- #713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
- 有关SQLite的优缺点
- 基于vue的倒计时demo
- 手风琴效果
- Layotparams布局参数的详解
- Master HA解密
- ubuntu16.04 安装 caffe
- Eclipse CDT插件
- Android data/data目录