첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
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.dll")]
[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.dll")]
public static extern IntPtr CreateIconIndirect(ref IconInfo iconInfo);

#endregion
#region 객체 삭제하기 - DeleteObject(handle)

/// <summary>
/// 객체 삭제하기
/// </summary>
/// <param name="handle">핸들</param>
/// <returns>처리 결과</returns>
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr handle);

#endregion
#region 아이콘 제거하기 - DestroyIcon(iconHandle)

/// <summary>
/// 아이콘 제거하기
/// </summary>
/// <param name="iconHandle">아이콘 핸들</param>
/// <returns>처리 결과</returns>
[DllImport("user32.dll", 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
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요