C#實(shí)現(xiàn)自定義光標(biāo)并動態(tài)切換
系統(tǒng)有很多光標(biāo)類型 :Cursors 類 (System.Windows.Input) | Microsoft Docs
本章介紹如何自定義光標(biāo)、并動態(tài)切換光標(biāo)類型。
動態(tài)切換光標(biāo)類型
以白板書寫為例:鼠標(biāo)操作時(shí),Cursor為紅點(diǎn);觸摸時(shí),Cursor為空;
public MainWindow()
{
InitializeComponent();
MouseEnter += (s, e) =>
{
ShowMouseCursor(e);
};
MouseMove += (s, e) =>
{
ShowMouseCursor(e);
};
StylusMove += (s, e) =>
{
ShowNoneCursor();
};
}設(shè)置光標(biāo)顯示:
private void ShowNoneCursor()
{
if (Cursor == Cursors.None)
{
return;
}
Cursor = Cursors.None;
Mouse.UpdateCursor();
}
private void ShowMouseCursor(MouseEventArgs e)
{
if (e.StylusDevice != null && e.StylusDevice.Id > -1)
{
return;
}
if (Cursor == GetFillCursor())
{
return;
}
Cursor = GetFillCursor();
Mouse.UpdateCursor();
}
private Cursor _fillCursor = null;
private Cursor GetFillCursor()
{
return _fillCursor ?? (_fillCursor = CursorHelper.CreateFillCursor());
}觸摸書寫時(shí),會有個(gè)默認(rèn)光標(biāo),所以此處把觸摸時(shí)的光標(biāo)置空Cursors.None。
Mouse.UpdateCursor()能強(qiáng)制更新光標(biāo)。當(dāng)然,不調(diào)用這個(gè)更新方法肉眼其實(shí)也看不出啥。。。

光標(biāo)切換效果如上,前面一段是用鼠標(biāo)書寫,后面是觸摸書寫,光標(biāo)類型有切換。紅點(diǎn)光標(biāo)自定義方案見下方。
自定義光標(biāo)
自定義一個(gè)純色的圓形光標(biāo):
public static Cursor CreateFillCursor(int size = 24, Brush fillBrush = null)
{
int unitSize = size / 4;
var bmp = new Bitmap(size, size);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clip = new Region(new Rectangle(0, 0, size, size));
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
using (var pen = new Pen(fillBrush ?? Brushes.Red, unitSize))
{
g.DrawEllipse(pen, new Rectangle(unitSize, unitSize, unitSize, unitSize));
}
}
return BitmapCursor.CreateBmpCursor(bmp);
}也可以通過圖片資源BitmapSource來生成光標(biāo):
public static Cursor CreateFromBitmapSource(BitmapSource source)
{
var bitmap = BitmapSourceToBitmap(source);
return BitmapCursor.CreateBmpCursor(bitmap);
}
private static Bitmap BitmapSourceToBitmap(BitmapSource source)
{
using (var stream = new MemoryStream())
{
var e = new BmpBitmapEncoder();
e.Frames.Add(BitmapFrame.Create(source));
e.Save(stream);
var bmp = new Bitmap(stream);
return bmp;
}
}BitmapCursor:
internal class BitmapCursor : SafeHandle
{
public override bool IsInvalid => handle == (IntPtr)(-1);
public static Cursor CreateBmpCursor(Bitmap cursorBitmap)
{
var c = new BitmapCursor(cursorBitmap);
return CursorInteropHelper.Create(c);
}
protected BitmapCursor(Bitmap cursorBitmap)
: base((IntPtr)(-1), true)
{
handle = cursorBitmap.GetHicon();
}
protected override bool ReleaseHandle()
{
bool result = DestroyIcon(handle);
handle = (IntPtr)(-1);
return result;
}
[DllImport("user32")]
private static extern bool DestroyIcon(IntPtr hIcon);
}到此這篇關(guān)于C#實(shí)現(xiàn)自定義光標(biāo)并動態(tài)切換的文章就介紹到這了,更多相關(guān)C#光標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中面向?qū)ο缶幊虣C(jī)制之繼承學(xué)習(xí)筆記
這篇文章主要介紹了C#中面向?qū)ο缶幊虣C(jī)制之繼承學(xué)習(xí)筆記,本文給出一個(gè)簡單子實(shí)例講解C#中的繼承,并講解了一些C#繼承的知識技巧,需要的朋友可以參考下2015-01-01
DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法,對C#程序設(shè)計(jì)人員來說是一個(gè)很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
C#調(diào)用HTTP POST請求上傳圖片的示例代碼
現(xiàn)在很多B/S系統(tǒng)的開發(fā)都是通過API方式來進(jìn)行的,一般服務(wù)端會開放一個(gè)API接口,客戶端調(diào)用API接口來實(shí)現(xiàn)圖片或文件上傳的功能,感興趣的可以了解一下2021-05-05
C#操作Byte數(shù)組和十六進(jìn)制進(jìn)行互轉(zhuǎn)
這篇文章介紹了C#操作Byte數(shù)組和十六進(jìn)制進(jìn)行互轉(zhuǎn)的的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Unity調(diào)用手機(jī)攝像機(jī)識別二維碼
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識別二維碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Win10下C# DateTime出現(xiàn)星期幾問題的解決方法
這篇文章主要介紹了Win10下C# DateTime出現(xiàn)星期幾問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

