Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:54:19+00:00 2026-05-24T07:54:19+00:00

I’m building a desktop app in C# with Windows Forms. I have a custom

  • 0

I’m building a desktop app in C# with Windows Forms. I have a custom Control, and I’d like to be able to drag and drop it within my application (not outside). Right now I’m implementing that with the usual DoDragDrop/OnDragOver/OnDragDrop methods. Is there any way to continuously paint the control as it gets dragged around–sort of what you see with JQuery’s drag-and-drop? I want the actual control to stay in place, but I want to paint a copy of its appearance as the user drags it. Ideally the copy would even be semi-transparent, but that’s more a “nice to have.”

The only way I can think to do this is to put the paint code in the main form’s OnPaint method, but that seems like an inelegant solution. Any other ideas? Are things any easier if the Control paints itself as just a Bitmap?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:54:20+00:00Added an answer on May 24, 2026 at 7:54 am

    I thought I should come back and answer this myself, since I did get it working eventually.

    I created a CursorUtil class with these functions:

    public struct IconInfo {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }
    
    public class CursorUtil {
        [DllImport("user32.dll")]
        public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
    
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr handle);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        extern static bool DestroyIcon(IntPtr handle);
    
        // Based on the article and comments here:
        // http://www.switchonthecode.com/tutorials/csharp-tutorial-how-to-use-custom-cursors
        // Note that the returned Cursor must be disposed of after use, or you'll leak memory!
        public static Cursor CreateCursor(Bitmap bm, int xHotspot, int yHotspot) {
            IntPtr cursorPtr;
            IntPtr ptr = bm.GetHicon();
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.xHotspot = xHotspot;
            tmp.yHotspot = yHotspot;
            tmp.fIcon = false;
            cursorPtr = CreateIconIndirect(ref tmp);
    
            if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor);
            if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask);
            if (ptr != IntPtr.Zero) DestroyIcon(ptr);
    
            return new Cursor(cursorPtr);
        }
    
        public static Bitmap AsBitmap(Control c) {
            Bitmap bm = new Bitmap(c.Width, c.Height);
            c.DrawToBitmap(bm, new Rectangle(0, 0, c.Width, c.Height));
            return bm;
        }
    

    Then I wrote a Drag class (also not object-oriented, alas, but I figured you can only drag one thing at a time in a desktop app). Here is a bit of that code:

        public static void StartDragging(Control c) {
            Dragged = c;
            DisposeOldCursors();
            Bitmap bm = CursorUtil.AsBitmap(c);
            DragCursorMove = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y);      
            DragCursorLink = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y);      
            DragCursorCopy = CursorUtil.CreateCursor(CursorUtil.AddCopySymbol(bm), DragStart.X, DragStart.Y);
            DragCursorNo = CursorUtil.CreateCursor(CursorUtil.AddNoSymbol(bm), DragStart.X, DragStart.Y);
            //Debug.WriteLine("Starting drag");
        }   
    
        // This gets called once when we move over a new control,
        // or continuously if that control supports dropping.
        public static void UpdateCursor(object sender, GiveFeedbackEventArgs fea) {
            //Debug.WriteLine(MainForm.MousePosition);
            fea.UseDefaultCursors = false;
            //Debug.WriteLine("effect = " + fea.Effect);
            if (fea.Effect == DragDropEffects.Move) {
                Cursor.Current = DragCursorMove;
    
            } else if (fea.Effect == DragDropEffects.Copy) {
                Cursor.Current = DragCursorCopy;
    
            } else if (fea.Effect == DragDropEffects.None) {
                Cursor.Current = DragCursorNo;
    
            } else if (fea.Effect == DragDropEffects.Link) {
                Cursor.Current = DragCursorLink;
    
            } else {
                Cursor.Current = DragCursorMove;
            }
        }
    

    You can use these methods when you set up your controls, for example in the constructor:

    GiveFeedback += new GiveFeedbackEventHandler(Drag.UpdateCursor);
    

    and in this method:

        protected override void OnMouseMove(MouseEventArgs mea) {
            if (Drag.IsDragging(mea)) {
                Drag.StartDragging(this);
                DragDropEffects dde = DoDragDrop(Plan, DragDropEffects.Move | DragDropEffects.Copy);
                Drag.StopDragging();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.