C#繪制鼠標(biāo)指針的示例代碼
前言
有用的沒(méi)用的,用的上的用不上的,能寫(xiě)的不能寫(xiě)的,反正想起來(lái)就寫(xiě)了,比如這篇,好像一般也沒(méi)什么用,emmm,或許,做錄制軟件的時(shí)候可以用一下。
顧名思義,本篇主要就是來(lái)實(shí)現(xiàn)將鼠標(biāo)的指針樣式給繪制成圖片,顯示或者保存下來(lái)。以下會(huì)通過(guò)兩種方式實(shí)現(xiàn),一種是C#自帶的Cursor,另一種就是用Windows Api;下面分別寫(xiě)下兩種方式的實(shí)現(xiàn)代碼以及優(yōu)勢(shì)和缺陷
開(kāi)發(fā)環(huán)境:.NET Framework版本:4.8
開(kāi)發(fā)工具:Visual Studio 2022
實(shí)現(xiàn)步驟
第一種使用C#自帶的Cursor,這種方式使用起來(lái)比較簡(jiǎn)單,但是沒(méi)辦法正確獲取到程序頁(yè)面以外的指針形狀
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = pictureBox1.CreateGraphics();
graphics.Clear(pictureBox1.BackColor);
int x=Cursor.Position.X;
int y=Cursor.Position.Y;
Cursor.Draw(graphics, new Rectangle(1,1,50,50));
//以拉伸格式繪制
// Cursor.DrawStretched(pictureBox1.CreateGraphics(), new Rectangle(1, 1, 50, 50));
label1.Text = $"坐標(biāo):{x},{y}";
}第二種使用Windows Api,這種方式就比較全面,可以彌補(bǔ)上面那種方式的缺點(diǎn)。
/// <summary>
/// 獲取鼠標(biāo)信息
/// </summary>
/// <param name="cInfo"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
private static extern bool GetCursorInfo(ref CURSORINFO cInfo);
/// <summary>
/// 將指定的圖標(biāo)從另一個(gè)模塊復(fù)制到當(dāng)前模塊。
/// </summary>
/// <param name="hIcon"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
private static extern IntPtr CopyIcon(IntPtr hIcon);
/// <summary>
/// 獲取有關(guān)指定圖標(biāo)或光標(biāo)的信息
/// </summary>
/// <param name="hIcon"></param>
/// <param name="iInfo"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO iInfo);private (int x, int y, Bitmap bmp) CaptureCursor()
{
CURSORINFO cURSORINFO = new CURSORINFO();
cURSORINFO.cbSize = Marshal.SizeOf(cURSORINFO);
if (GetCursorInfo(ref cURSORINFO))
{
if (cURSORINFO.flags == 0x00000001)
{
IntPtr icon = CopyIcon(cURSORINFO.hCursor);
ICONINFO iCONINFO;
if (GetIconInfo(icon, out iCONINFO))
{
int x = cURSORINFO.ptScreenPos.X - iCONINFO.xHotspot;
int y = cURSORINFO.ptScreenPos.Y - iCONINFO.yHotspot;
Bitmap bmp = Icon.FromHandle(icon).ToBitmap();
return (x, y, bmp);
}
}
}
return (0,0,null);
}private void button3_Click(object sender, EventArgs e)
{
var cursor = CaptureCursor();
pictureBox1.Image = cursor.bmp;
label1.Text = $"坐標(biāo):{cursor.x},{cursor.y}";
}下面看下實(shí)現(xiàn)效果,當(dāng)鼠標(biāo)在界面外時(shí),我們主要通過(guò)Tab和Enter來(lái)觸發(fā)按鈕
實(shí)現(xiàn)效果

到此這篇關(guān)于C#繪制鼠標(biāo)指針的示例代碼的文章就介紹到這了,更多相關(guān)C#鼠標(biāo)指針內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#單例模式(Singleton)的6種實(shí)現(xiàn)
這篇文章主要介紹了c#單例模式(Singleton)的6種實(shí)現(xiàn) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)
這篇文章主要介紹了BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)的相關(guān)資料,通過(guò)引入相關(guān)文件,實(shí)現(xiàn)此功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
C#實(shí)現(xiàn)狀態(tài)欄提示信息功能的示例
今天小編就為大家分享一篇C#實(shí)現(xiàn)狀態(tài)欄提示信息功能的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

