■ 비트맵에서 커서를 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; #region 아이콘 정보 - IconInfo /// <summary> /// 아이콘 정보 /// </summary> public struct IconInfo { /// <summary> /// 아이콘 여부 /// </summary> /// <remarks>아이콘 : true, 커서 : false</remarks> public bool IsIcon; /// <summary> /// 핫스팟 X /// </summary> public int HotSpotX; /// <summary> /// 핫스팟 Y /// </summary> public int HotSpotY; /// <summary> /// 마스크 비트맵 핸들 /// </summary> public IntPtr MaskBitmapHandle; /// <summary> /// 색상 비트맵 핸들 /// </summary> public IntPtr ColorBitmapHandle; } #endregion #region 아이콘 정보 구하기 - GetIconInfo(iconHandle, iconInfo) /// <summary> /// 아이콘 정보 구하기 /// </summary> /// <param name="iconHandle">아이콘 핸들</param> /// <param name="iconInfo">아이콘 정보</param> /// <returns>처리 결과</returns> [DllImport("user32")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetIconInfo(IntPtr iconHandle, ref IconInfo iconInfo); #endregion #region 아이콘 간접 생성하기 - CreateIconIndirect(iconInfo) /// <summary> /// 아이콘 간접 생성하기 /// </summary> /// <param name="iconInfo">아이콘 정보</param> /// <returns>처리 결과</returns> [DllImport("user32")] public static extern IntPtr CreateIconIndirect(ref IconInfo iconInfo); #endregion #region 객체 삭제하기 - DeleteObject(handle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="handle">핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32")] public static extern bool DeleteObject(IntPtr handle); #endregion #region 아이콘 제거하기 - DestroyIcon(iconHandle) /// <summary> /// 아이콘 제거하기 /// </summary> /// <param name="iconHandle">아이콘 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto)] extern static bool DestroyIcon(IntPtr iconHandle); #endregion #region 커서 구하기 - GetCursor(sourceBitmap, hotSpotX, hotSpotY) /// <summary> /// 커서 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="hotSpotX">핫스팟 X</param> /// <param name="hotSpotY">핫스팟 Y</param> /// <returns>커서</returns> public static Cursor GetCursor(Bitmap sourceBitmap, int hotSpotX, int hotSpotY) { IntPtr cursorHandle; IntPtr iconHandle = sourceBitmap.GetHicon(); IconInfo iconInfo = new IconInfo(); GetIconInfo(iconHandle, ref iconInfo); iconInfo.HotSpotX = hotSpotX; iconInfo.HotSpotY = hotSpotY; iconInfo.IsIcon = false; cursorHandle = CreateIconIndirect(ref iconInfo); if(iconInfo.ColorBitmapHandle != IntPtr.Zero) { DeleteObject(iconInfo.ColorBitmapHandle); } if(iconInfo.MaskBitmapHandle != IntPtr.Zero) { DeleteObject(iconInfo.MaskBitmapHandle); } if(iconHandle != IntPtr.Zero) { DestroyIcon(iconHandle); } return new Cursor(cursorHandle); } #endregion |