첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DS.Core.WinForm.Graphic
{
/// <summary>
/// 캐럿 관리자
/// </summary>
public class CaretManager
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Import
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

    #region 캐럿 생성하기 - CreateCaret(windowHandle, bitmapHandle, width, height)

    /// <summary>
    /// 캐럿 생성하기
    /// </summary>
    /// <param name="windowHandle">윈도우 핸들</param>
    /// <param name="bitmapHandle">비트맵 핸들</param>
    /// <param name="width">너비</param>
    /// <param name="height">높이</param>
    /// <returns>처리 결과</returns>
    [DllImport("user32.dll")]
    public static extern int CreateCaret(IntPtr windowHandle, IntPtr bitmapHandle, int width, int height);

    #endregion

    #region 캐럿 제거하기 - DestroyCaret()

    /// <summary>
    /// 캐럿 제거하기
    /// </summary>
    /// <returns>처리 결과</returns>
    [DllImport("user32.dll")]
    public static extern int DestroyCaret();

    #endregion

    #region 캐럿 위치 설정하기 - SetCaretPos(x, y)

    /// <summary>
    /// 캐럿 위치 설정하기
    /// </summary>
    /// <param name="x">X 좌표</param>
    /// <param name="y">Y 좌표</param>
    /// <returns>처리 결과</returns>
    [DllImport("user32.dll")]
    public static extern int SetCaretPos(int x, int y);

    #endregion

    #region 캐럿 보여주기 - ShowCaret(windowHandle)

    /// <summary>
    /// 캐럿 보여주기
    /// </summary>
    /// <param name="windowHandle">윈도우 핸들</param>
    /// <returns>처리 결과</returns>
    [DllImport("user32.dll")]
    public static extern int ShowCaret(IntPtr windowHandle);

    #endregion

    #region 캐럿 숨기기 - HideCaret(windowHandle)

    /// <summary>
    /// 캐럿 숨기기
    /// </summary>
    /// <param name="windowHandle">윈도우 핸들</param>
    /// <returns>처리 결과</returns>
    [DllImport("user32.dll")]
    public static extern int HideCaret(IntPtr windowHandle);

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 컨트롤
    /// </summary>
    private Control control;

    /// <summary>
    /// 크기
    /// </summary>
    private Size size;

    /// <summary>
    /// 위치
    /// </summary>
    private Point position;

    /// <summary>
    /// 표시 여부
    /// </summary>
    private bool visible;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 컨트롤 - Control

    /// <summary>
    /// 컨트롤
    /// </summary>
    public Control Control
    {
        get
        {
            return this.control;
        }
    }

    #endregion

    #region 크기 - Size

    /// <summary>
    /// 크기
    /// </summary>
    public Size Size
    {
        get
        {
            return this.size;
        }
        set
        {
            this.size = value;
        }
    }

    #endregion

    #region 위치 - Position

    /// <summary>
    /// 위치
    /// </summary>
    public Point Position
    {
        get
        {
            return this.position;
        }
        set
        {
            this.position = value;

            SetCaretPos(this.position.X, this.position.Y);
        }
    }

    #endregion

    #region 표시 - Visibility

    /// <summary>
    /// 표시
    /// </summary>
    public bool Visibility
    {
        get
        {
            return this.visible;
        }
        set
        {
            if(this.visible = value)
            {
                ShowCaret(Control.Handle);
            }
            else
            {
                HideCaret(Control.Handle);
            }
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - CaretManager(control)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="control">컨트롤</param>
    public CaretManager(Control control)
    {
        this.control = control;

        Position = Point.Empty;
        Size     = new Size(1, control.Font.Height);

        Control.GotFocus  += new EventHandler(ControlOnGotFocus);
        Control.LostFocus += new EventHandler(ControlOnLostFocus);

        if(control.Focused)
        {
            ControlOnGotFocus(control, new EventArgs());
        }
    }

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 생성자 - CaretManager()

    /// <summary>
    /// 생성자
    /// </summary>
    private CaretManager()
    {
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 보여주기 - Show()

    /// <summary>
    /// 보여주기
    /// </summary>
    public void Show()
    {
        Visibility = true;
    }

    #endregion

    #region 숨기기 - Hide()

    /// <summary>
    /// 숨기기
    /// </summary>
    public void Hide()
    {
        Visibility = false;
    }

    #endregion

    #region 해제하기 - Dispose()

    /// <summary>
    /// 해제하기
    /// </summary>
    public void Dispose()
    {
        if(this.control.Focused)
        {
            ControlOnLostFocus(this.control, new EventArgs());
        }
                
        Control.GotFocus  -= ControlOnGotFocus;
        Control.LostFocus -= ControlOnLostFocus;
    }

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Private
    //////////////////////////////////////////////////////////////////////////////// Event

    #region 컨트롤 포커스 획득시 처리하기 - ControlOnGotFocus(sender, e)

    /// <summary>
    /// 컨트롤 포커스 획득시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void ControlOnGotFocus(object sender, EventArgs e)
    {
        CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height);

        SetCaretPos(Position.X, Position.Y);

        Show();
    }

    #endregion

    #region 컨트롤 포커스 상실시 처리하기 - ControlOnLostFocus(sender, e)

    /// <summary>
    /// 컨트롤 포커스 상실시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void ControlOnLostFocus(object sender, EventArgs e)
    {
        Hide();

        DestroyCaret();
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요