vineri, 5 februarie 2010

[C#] How to change the mouse cursor pic?

I want to be able to change the pic for a cursor of any control that support this option.
Let say that I want when I hover the mouse over a panel to have a different cursor.

  1. The most simple way is to use Cursors class which provides a collection of Cursor objects for use by a Windows Forms application.

    m_panel.Cursor = Cursors.Arrow;

  2. Another way is to create a cursor from .cur/.ani file.

    Cursor _newCursor = new Cursor("myCurFile.cur");

  3. Or you can use two Windows native methods in order to create a cursor.

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    
    namespace Cursor
    {
    class CursorUtils
    {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    
    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
    
    public static System.Windows.Forms.Cursor CreateCursor(Bitmap i_bitmap, int i_xHotspot, int i_yHotspot)
    {
    IntPtr _ptr = i_bitmap.GetHicon();
    IconInfo _iconInfo = new IconInfo();
    GetIconInfo(_ptr, ref _iconInfo);
    _iconInfo.xHotspot = i_xHotspot;
    _iconInfo.yHotspot = i_yHotspot;
    _iconInfo.fIcon = false;
    _ptr = CreateIconIndirect(ref _iconInfo);
    return new System.Windows.Forms.Cursor(_ptr);
    }
    }
    
    /// <summary>
    /// The structure contains information about an icon or a cursor.
    /// </summary>
    public struct IconInfo
    {
    /// <summary>
    /// Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies an icon; FALSE specifies a cursor.
    /// </summary>
    public bool fIcon;
    
    /// <summary>
    /// Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the icon, and this member is ignored.
    /// </summary>
    public int xHotspot;
    
    /// <summary>
    /// Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the icon, and this member is ignored.
    /// </summary>
    public int yHotspot;
    
    /// <summary>
    /// Specifies the icon bitmask bitmap. If this structure defines a black and white icon, this bitmask is formatted so that the upper half is the icon AND bitmask and the lower half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two. If this structure defines a color icon, this mask only defines the AND bitmask of the icon. 
    /// </summary>
    public IntPtr hbmMask;
    
    /// <summary>
    /// Handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND  flag to the destination; subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag.
    /// </summary>
    public IntPtr hbmColor;
    }
    }
    
    You can use it in the folowing ways:
     - with a Bitmap:
    
    Bitmap _bitmap2x = myProject.Properties.Resources._2x_icon;
    m_panelTop.Cursor = CursorUtils.CreateCursor(_bitmap2x, 3, 3);
    
    - with a text transformed in a bitmap:
    
    using (Bitmap _bitmap = new Bitmap(64, 16))
    {
    Graphics _graphics = Graphics.FromImage(_bitmap);
    using (Font _f = new Font(FontFamily.GenericSansSerif, 6, FontStyle.Bold))
    {
    _graphics.DrawString("2x to open menu", _f, Brushes.Black, 0, 0);
    }
    
    m_panelTop.Cursor = CursorUtils.CreateCursor(_bitmap, 3, 3);
    }

Niciun comentariu:

Trimiteți un comentariu