Application/C# (WinForm)

[C# WinForm] 마우스 커서 정보 얻기

devsalix 2014. 8. 13. 15:43
728x90

현재 마우스의 커서 위치 및 마우스 커서의 모양을 알고자 할때 쓰는 유용한 함수 Class

 

using System;
using System.Drawing;
using System.Runtime.InteropServices;

class CGetCursor {

     [StructLayout(LayoutKind.Sequential)]
     public struct ICONINFO
	 {
		public bool fIcon;
        public Int32 xHotspot;
		public Int32 yHotspot;
		public IntPtr hbmMask;
		public IntPtr hbmColor;
	}
	
	[StructLayout(LayoutKind.Sequential)]
	public struct CURSORINFO
	{
		public Int32 cbSize;
		public Int32 flags;
		public IntPtr hCursor;
		public Point stScreenPos;
	}
	
	[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
	public static extern bool GetCursorInfo(out CURSORINFO pci);
	
	[DllImport("user32.dll", EntryPoint = "CopyIcon")]
	public static extern IntPtr CopyIcon(IntPtr hIcon);
	
	[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
	public static extern bool getIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
	
	private const Int32 CURSOR_SHOWING = 0x00000001;
	private Point Position;
	private Icon Cursor;
	
	public CGetCursor()
	{
		Position = new Point(0, 0);
		Cursor = null;
		CursorFind();
	}
	
	private void CursorFind()
	{
		CURSORINFO ci = new CURSORINFO();
		IntPtr hicon;
		ICONINFO icInfo;
		ci.cbSize = Marshal.SizeOf(ci);
		if (GetCursorInfo(out ci))
		{
			if (ci.flags == CURSOR_SHOWING)
			{
				hicon = CopyIcon(ci.hCursor);
				if (getIconInfo(hicon, out icInfo))
				{
					Cursor = Icon.FromHandle(hicon);
					Position = ci.stScreenPos;
				}
			}
		}
	}

	//현재 CURSOR의 iCon모양 return
	public Icon GetCursoriCon
	{
		get
		{
			return Cursor;
		}
	}

	//현재 CURSOR의 iCon위치 return
	public Point GetCursorPosition
	{
		get
		{
			return Position;
		}
	}
}
728x90
반응형