■ 비트맵으로 커서를 만드는 방법을 보여준다.
▶ 비트맵으로 커서 만들기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System.Drawing; using System.Windows.Forms; ... private CheckBox checkBox; ... Bitmap bitmap = CursorHelper.GetBitmap(this.checkBox); Cursor cursor = CursorHelper.GetCursor(bitmap, 0, 0); |
▶ 비트맵으로 커서 만들기 (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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
using System; using System.Runtime.InteropServices; using System.Drawing; using System.Windows.Forms; /// <summary> /// 커서 헬퍼 /// </summary> public class CursorHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 아이콘 정보 - IconInfo /// <summary> /// 아이콘 정보 /// </summary> private struct IconInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 아이콘 여부 /// </summary> 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 } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 간접 생성하기 - CreateIconIndirect(iconInfo) /// <summary> /// 아이콘 간접 생성하기 /// </summary> /// <param name="iconInfo">아이콘 정보</param> /// <returns>아이콘 핸들</returns> [DllImport("user32.dll")] private static extern IntPtr CreateIconIndirect(ref IconInfo iconInfo); #endregion #region 아이콘 정보 구하기 - GetIconInfo(iconHandle, iconInfo) /// <summary> /// 아이콘 정보 구하기 /// </summary> /// <param name="iconHandle">아이콘 핸들</param> /// <param name="iconInfo">아이콘 정보</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetIconInfo(IntPtr iconHandle, ref IconInfo iconInfo); #endregion #region 객체 삭제하기 - DeleteObject(handle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="handle">핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr handle); #endregion #region 아이콘 제거하기 - DestroyIcon(iconHandle) /// <summary> /// 아이콘 제거하기 /// </summary> /// <param name="iconHandle">아이콘 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool DestroyIcon(IntPtr iconHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 커서 구하기 - GetCursor(bitmap, hotSpotX, hotSpotY) /// <summary> /// 커서 구하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <param name="hotSpotX">핫스팟 X</param> /// <param name="hotSpotY">핫스팟 Y</param> /// <returns>커서</returns> public static Cursor GetCursor(Bitmap bitmap, int hotSpotX, int hotSpotY) { IntPtr cursorHandle; IntPtr iconHandle = bitmap.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 #region 비트맵 구하기 - GetBitmap(control) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="control">컨트롤</param> /// <returns>비트맵</returns> public static Bitmap GetBitmap(Control control) { Bitmap bitmap = new Bitmap(control.Width, control.Height); control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height)); return bitmap; } #endregion } |