Adding a custom string as a bitmap (or simply text) alongwith the mouse pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanjayamladi
    New Member
    • Apr 2010
    • 7

    Adding a custom string as a bitmap (or simply text) alongwith the mouse pointer

    During a drag and drop operation involving two DataGridViews, as source and targets, I need to display a custom string (either as a bitmap or simply text) ALONGSIDE (or at) the mouse pointer. I know how to replace the default cursor with a custom text, but this replace the pointer. I need to show both the pointer as well as the text (Similar to the cursor in Windows explorer when you drag n drop a file: you will notice theres a file name in addition to the mouse pointer). Thanks.

    I am doing the below to replace the cusror with my custom string, but this replaces the pointer.

    Code:
     private void SetCustomCursor(string limitPrice)
            {
                   SizeF size;
                    Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
                    using (Bitmap tmpBmp = new Bitmap(1, 1))
                    using (Graphics g = Graphics.FromImage(tmpBmp))
                        size = g.MeasureString(limitPrice, f);
    
                    Bitmap bitmap = new Bitmap((int)Math.Ceiling(size.Width),
                        (int)Math.Ceiling(size.Height));
                    using (Graphics g = Graphics.FromImage(bitmap))
                        g.DrawString(limitPrice, f, Brushes.DarkOrange, 0, 0);
    
                    Cursor.Current = CreateCursor(bitmap, 10, 10);
    
                    //Cursor.Current = Cursors.Arrow;
    
                    bitmap.Dispose();
                    f.Dispose();
                }
                            
            }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Is there any way to get the current cursor as a bitmap, then merge the two and use that as the cursor?

    Comment

    • sanjayamladi
      New Member
      • Apr 2010
      • 7

      #3
      I am not aware of a way to do that. I am wondering if I need to only display a custom string at the pointer location without really doing anything with the cursor at all! Thanks for your response

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by GaryTexmo
        Is there any way to get the current cursor as a bitmap, then merge the two and use that as the cursor
        I am not aware of a way to do that.

        Gary is right. You are already making new bitmap out of your text. You need to make a bitmap from the current cursor, then add your text a little to the right of that to get the effect you are looking for.

        It's time step up from just copy/pasting other people's code to actually writing a little of your own. {Yes I found the article where 'your' code came from}

        Give it a little research and a lot of experimentation . Once you have some original code of your own but need some help, show us what you have created and we'll try to help. But taking someone else's code, then asking others to customize it for you without at least making your own effort first is just not in the spirit of Bytes or the coding community in general.

        Comment

        • sanjayamladi
          New Member
          • Apr 2010
          • 7

          #5
          First off I did not claim that code I posted was mine. I wanted to simply let everyone know what makes my cursor get replaced by the custom cursor. Also, I am not sure why you are assuming that I have not spent sufficient time experimenting. Thanks for wasting my time with your verbose any utterly useless reply. The 'spirit' of this forum is to contribute ONLY when you have something useful to contribute, not just because you have spare time.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            There's a way... google turned this up.



            See the CaptureCursor method. After that, I made the following list of modifications to your code...
            • Get the current cursor as a bitmap
            • Create a new bitmap that has room for both images
            • Draw the cursor bitmap into the destination bitmap
            • Draw the text into the destination bitmap
            • Create a cursor from the final bitmap


            I'll let you work out the details of how to actually implement this though.

            It's working rather nicely for me :D If you change the cursor several times though you might want to set it back to the default cursor before you take a capture of the current cursor, otherwise you'll get your modified one. Give it a try and let me know how it goes for you.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Originally posted by sanjayamladi
              Also, I am not sure why you are assuming that I have not spent sufficient time experimenting.
              Then please, by all means show us the code that you wrote that comes closest to merging in the cursor arrow with your text. We will do our best to help you get over whatever hurdle is stopping your code from working properly.

              Originally posted by sanjayamladi
              The 'spirit' of this forum is to contribute ONLY when you have something useful to contribute, not just because you have spare time.
              <laugh> 'spare time' <laugh> My work week is about 70 hours. I don't really have 'spare time'. But I do think it is important to help people further their education. Like all the other experienced coders here that have decided to give back to the coding community I started at the beginning: A lot of reading, a lot of experimenting, a lot of research, a lot of examining of code provided to the community by those before me.

              I can't speak for every other coding forum... but the 'spirit' here is toward education... toward helping people learn and actually understand the code they are writing. There are at least 10 people here that could write what you need in less than 20 minutes. But in the long run that doesn't help you learn. The Bytes volunteers are not here to write your code for you. This is not a free homework service.
              Bytes is very much a "Give me a fish I eat for a day. Teach me to fish I eat for a lifetime" kind of place. Just giving you the code doesn't help you learn near as effectively as good old-fashioned trial and error.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by GaryTexmo
                See the CaptureCursor method. After that, I made the following list of modifications to your code...
                Get the current cursor as a bitmap
                Create a new bitmap that has room for both images
                Draw the cursor bitmap into the destination bitmap
                Draw the text into the destination bitmap
                Create a cursor from the final bitmap

                I'll let you work out the details of how to actually implement this though.
                Nicely done, Gary. And in less than 20 minutes as I predicted. ;-)

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  Haha, thanks :)

                  Comment

                  • sanjayamladi
                    New Member
                    • Apr 2010
                    • 7

                    #10
                    Thanks Gary for a quick response. The resulting bitmap comes very close to what I was looking for. I do have some flicker, but I would be able to fix that.

                    Tlhintoq: Based on your verbose replies, I can easily believe you spend 70 hours a week ;-)

                    Comment

                    Working...