C#實(shí)現(xiàn)繪制鼠標(biāo)的示例代碼
更新時(shí)間:2022年12月23日 09:16:54 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)繪制鼠標(biāo)的效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果

代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _X, _Y;
[StructLayout(LayoutKind.Sequential)]
private struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
private struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int mVal);
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
private static extern bool GetCursorInfo(ref CURSORINFO cInfo);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
private static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO iInfo);
private void Form1_Load(object sender, EventArgs e)
{
}
private Bitmap CaptureCursor(ref int _CX, ref int _CY)
{
IntPtr _Icon;
CURSORINFO _CursorInfo = new CURSORINFO();
ICONINFO _IconInfo;
_CursorInfo.cbSize = Marshal.SizeOf(_CursorInfo);
if (GetCursorInfo(ref _CursorInfo))
{
if (_CursorInfo.flags == 0x00000001)
{
_Icon = CopyIcon(_CursorInfo.hCursor);
if (GetIconInfo(_Icon, out _IconInfo))
{
_CX = _CursorInfo.ptScreenPos.X - _IconInfo.xHotspot;
_CY = _CursorInfo.ptScreenPos.Y - _IconInfo.yHotspot;
return Icon.FromHandle(_Icon).ToBitmap();
}
}
}
return null;
}
private void button1_Click(object sender, EventArgs e)
{
int x = Control.MousePosition.X;
int y = Control.MousePosition.Y;
pictureBox1.Image = CaptureCursor(ref x, ref y);
}
}
到此這篇關(guān)于C#實(shí)現(xiàn)繪制鼠標(biāo)的示例代碼的文章就介紹到這了,更多相關(guān)C#繪制鼠標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于WPF實(shí)現(xiàn)帶明細(xì)的環(huán)形圖表
這篇文章主要介紹了如何利用WPF繪制帶明細(xì)的環(huán)形圖表?,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08
使用C#實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)堆的代碼
這篇文章主要介紹了使用C#實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)堆,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
這篇文章主要介紹了C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法,需要的朋友可以參考下2015-09-09
WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法
這篇文章主要介紹了WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法,涉及針對窗口的操作與設(shè)置技巧,具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11

