wpf
Drag and Drop Cursor (커서) 변경
동동(이재동)
2016. 8. 23. 17:22
-일반적인 cur 파일로 변경방법
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 = new Cursor(new FileStream("Earth.cur", FileMode.Open));e.UseDefaultCursors = false;Mouse.SetCursor(customCursor);}elsee.UseDefaultCursors = true;e.Handled = true;}-UserControl(UI Element)로 바꾸는 방법namespace VISION.IdeaParkinglot.Managers{public class CursorManager{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);}}}}}요기는 giveFeedBack에 쓸것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);}}elsee.UseDefaultCursors = true;e.Handled = true;}참고 : https://wpf.2000things.com/2012/12/17/713-setting-the-cursor-to-an-image-of-an-uielement-while-dragging/