Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Change the global cursor

Change the global cursor

Scheduled Pinned Locked Moved C#
csharphtmlcomhelptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QzRz
    wrote on last edited by
    #1

    Hello I have currently made a small colorpicker application. I want to know if it is possible to change the global cursor when I press a button in my application, the reason is I want to see the crooshair cursor outside my application too. I have searched the internet and the best thing I could find was to use the "user32.dll" with the function SetSystemCursor, however I have no clue if this is the right thing to do. the SetSystemCursor needs an 'IntPtr hcur, uint id' but I do not know how to get the correct values for them. I searched the msdn from my vs05 and it said the id could must be some constants which needs to be defined before loading the windows.h header. To help people help me, the link for pinvoke.net is here: pinvoke.net and the link for the info I used on msdn: msdn I hope someone can help me regards QzRz

    P 1 Reply Last reply
    0
    • Q QzRz

      Hello I have currently made a small colorpicker application. I want to know if it is possible to change the global cursor when I press a button in my application, the reason is I want to see the crooshair cursor outside my application too. I have searched the internet and the best thing I could find was to use the "user32.dll" with the function SetSystemCursor, however I have no clue if this is the right thing to do. the SetSystemCursor needs an 'IntPtr hcur, uint id' but I do not know how to get the correct values for them. I searched the msdn from my vs05 and it said the id could must be some constants which needs to be defined before loading the windows.h header. To help people help me, the link for pinvoke.net is here: pinvoke.net and the link for the info I used on msdn: msdn I hope someone can help me regards QzRz

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Well, this works for me:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;
      
      namespace WindowsCursorTest
      {
      
          public partial class Form1 : Form
          {
              private const int OCR_NORMAL = 32512;
              [DllImport("user32.dll")]
              static extern bool SetSystemCursor(IntPtr hcur, uint id);
              [DllImport("user32.dll")]
              static extern bool DestroyCursor(IntPtr hcur);
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  IntPtr cursor = Cursors.Cross.CopyHandle();
                  SetSystemCursor(cursor, OCR_NORMAL);
                  DestroyCursor(cursor);
              }
          }
      }
      

      Deja View - the feeling that you've seen this post before.

      Q 1 Reply Last reply
      0
      • P Pete OHanlon

        Well, this works for me:

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Text;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;
        
        namespace WindowsCursorTest
        {
        
            public partial class Form1 : Form
            {
                private const int OCR_NORMAL = 32512;
                [DllImport("user32.dll")]
                static extern bool SetSystemCursor(IntPtr hcur, uint id);
                [DllImport("user32.dll")]
                static extern bool DestroyCursor(IntPtr hcur);
        
                public Form1()
                {
                    InitializeComponent();
                }
        
                private void button1_Click(object sender, EventArgs e)
                {
                    IntPtr cursor = Cursors.Cross.CopyHandle();
                    SetSystemCursor(cursor, OCR_NORMAL);
                    DestroyCursor(cursor);
                }
            }
        }
        

        Deja View - the feeling that you've seen this post before.

        Q Offline
        Q Offline
        QzRz
        wrote on last edited by
        #3

        It works for me too, thanks :) I just have 2 questions now. Where did you find the number for ORC_NORMAL? and how to chance the cursor back to normal again? I will assume what you need to do is add another const int with another value which?

        P 1 Reply Last reply
        0
        • Q QzRz

          It works for me too, thanks :) I just have 2 questions now. Where did you find the number for ORC_NORMAL? and how to chance the cursor back to normal again? I will assume what you need to do is add another const int with another value which?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Well, the normal way to do this is to copy the old cursor (using CopyCursor) and then reinstate it when you need to.

          QzRz wrote:

          Where did you find the number for ORC_NORMAL?

          I googled OCR_NORMAL to find the value. BTW - the value for OCR_CROSS is 32515.

          Deja View - the feeling that you've seen this post before.

          Q 1 Reply Last reply
          0
          • P Pete OHanlon

            Well, the normal way to do this is to copy the old cursor (using CopyCursor) and then reinstate it when you need to.

            QzRz wrote:

            Where did you find the number for ORC_NORMAL?

            I googled OCR_NORMAL to find the value. BTW - the value for OCR_CROSS is 32515.

            Deja View - the feeling that you've seen this post before.

            Q Offline
            Q Offline
            QzRz
            wrote on last edited by
            #5

            I got it to work now, thanks :D

            P 1 Reply Last reply
            0
            • Q QzRz

              I got it to work now, thanks :D

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              No problem. Glad to help.

              Deja View - the feeling that you've seen this post before.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups