mardi 21 avril 2015

SetSystemCursor() for multiple cursors behavior

I am trying to change multiple cursors to Cross cursor. This is the code I am using for that matter:

[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, 
    UInt32 uiParam, String pvParam, UInt32 fWinIni);

//Normal cursor
private static uint OCR_NORMAL = 32512;
//The text selection (I-beam) cursor.
private static uint OCR_IBEAM = 32513;
//The cross-shaped cursor.
private static uint OCR_CROSS = 32515;

Then I use these two functions I made:

static public void ChangeCursors() {
    SetSystemCursor(LoadCursor(IntPtr.Zero, (int)OCR_NORMAL), OCR_CROSS);
    SetSystemCursor(LoadCursor(IntPtr.Zero, (int)OCR_IBEAM), OCR_CROSS);
}

static public void RevertCursors() {
    SystemParametersInfo(0x0057, 0, null, 0);
}

If I just use SetSystemCursor(LoadCursor(IntPtr.Zero, (int)OCR_NORMAL), OCR_CROSS);, everything works fine. The Normal cursor gets replaced by Cross cursor.

My problem is when I try to change multiple cursors to Cross cursor. If I call ChangeCursors(), the expected result would be Normal cursor AND I-beam cursor gets replaced by Cross cursor. But the result is something really weird.

When the software starts depending on the current state of the cursor when the program started, the following strange things happen:

  • If cursor was Normal when the software started, it changes to Cross (that's good). Also, I-beam is replaced with Normal (that's bad, it should be Cross).
  • If cursor was I-beam when the software started, it stays I-beam(that's bad, because it should be Cross). Then, by hovering over to where previously the cursor should be Normal it is now Cross (that's good). Then, if I hover over to where the cursor was I-beam 1 second ago, it magically changes to Normal (that's weird) and stays that way.

So, my question is, how can I change 2 or more Cursors to Cross cursor, using SetSystemCursor() ?

Aucun commentaire:

Enregistrer un commentaire