WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球
拖曳小球
WPF的拖曳效果,基本配置一下,就可以了,但是自繪的話,就得自己控制,按鍵點(diǎn)擊,按鍵移動(dòng)和按鍵松開的事件,與其配合達(dá)到目的。
這個(gè)效果實(shí)現(xiàn)了,其實(shí)也變相的實(shí)現(xiàn)了WPF里的拖動(dòng)效果,這個(gè)效果用著還是很方便的。
但是代碼,確十分的簡單。
Wpf 和 SkiaSharp
新建一個(gè)WPF項(xiàng)目,然后,Nuget包即可
要添加Nuget包
Install-Package SkiaSharp.Views.WPF -Version 2.88.0
其中核心邏輯是這部分,會(huì)以我設(shè)置的60FPS來刷新當(dāng)前的畫板。
skContainer.PaintSurface += SkContainer_PaintSurface; _ = Task.Run(() => { while (true) { try { Dispatcher.Invoke(() => { skContainer.InvalidateVisual(); }); _ = SpinWait.SpinUntil(() => false, 1000 / 60);//每秒60幀 } catch { break; } } });
實(shí)現(xiàn)代碼
鼠標(biāo)按下,移動(dòng),鼠標(biāo)松開
先對(duì)SkiaSharp對(duì)象,增加相關(guān)事件
skContainer.MouseDown += SkContainer_MouseDown; skContainer.MouseUp += SkContainer_MouseUp; skContainer.MouseMove += SkContainer_MouseMove;
然后增加相關(guān)事件處理代碼,我這邊都統(tǒng)一處理了.
private void SkContainer_MouseDown(object sender, MouseButtonEventArgs e) { var cur = e.GetPosition(sender as IInputElement); drawClock.MouseDown(new SKPoint((float)cur.X, (float)cur.Y), true); } private void SkContainer_MouseUp(object sender, MouseEventArgs e) { var cur = e.GetPosition(sender as IInputElement); drawClock.MouseDown(new SKPoint((float)cur.X, (float)cur.Y), false); } private void SkContainer_MouseMove(object sender, MouseEventArgs e) { var cur = e.GetPosition(sender as IInputElement); drawClock.MouseMove(new SKPoint((float)cur.X, (float)cur.Y)); }
拖曳核心類
/// <summary> /// 拖曳 /// </summary> public class Drag { public SKPoint centerPoint; public int Radius = 0; private bool Pressed = false; private bool CirclePressend = false; private SKPoint sKPoint = SKPoint.Empty; private SKPoint CirclePoint = SKPoint.Empty; private SKCanvas canvas; private float dx = 0; private float dy = 0; /// <summary> /// 渲染 /// </summary> public void Render(SKCanvas canvas, SKTypeface Font, int Width, int Height) { this.canvas = canvas; centerPoint = new SKPoint(Width / 2, Height / 2); this.Radius = 40; canvas.Clear(SKColors.White); if (CirclePoint.IsEmpty) { CirclePoint = new SKPoint(centerPoint.X, centerPoint.Y); } if (CirclePressend) { CirclePoint = new SKPoint(sKPoint.X - dx, sKPoint.Y - dy); DrawCircle(this.canvas, CirclePoint); } else { DrawCircle(this.canvas, CirclePoint); } using var paint = new SKPaint { Color = SKColors.Black, IsAntialias = true, Typeface = Font, TextSize = 24 }; var msg = $"X:{ sKPoint.X} Y:{sKPoint.Y} Pressed:{Pressed} CirclePressend:{CirclePressend}"; canvas.DrawText(msg, 0, 30, paint); } public void MouseMove(SKPoint sKPoint) { this.sKPoint = sKPoint; if (CirclePressend)//按下,就開始拖動(dòng) { CirclePoint = sKPoint; } } public void MouseDown(SKPoint sKPoint, bool Pressed) { this.sKPoint = sKPoint; this.Pressed = Pressed; if (this.Pressed) { this.CirclePressend = CheckPoint(sKPoint, CirclePoint); if (this.CirclePressend) { dx = sKPoint.X - CirclePoint.X; dy = sKPoint.Y - CirclePoint.Y; } } else { this.CirclePressend = false; } } public bool CheckPoint(SKPoint sKPoint, SKPoint CirclePoint) { var d = Math.Sqrt(Math.Pow(sKPoint.X - CirclePoint.X, 2) + Math.Pow(sKPoint.Y - CirclePoint.Y, 2)); return this.Radius >= d; } /// <summary> /// 畫一個(gè)圓 /// </summary> public void DrawCircle(SKCanvas canvas, SKPoint sKPoint) { using var paint = new SKPaint { Color = SKColors.Blue, Style = SKPaintStyle.Fill, IsAntialias = true, StrokeWidth = 2 }; canvas.DrawCircle(sKPoint.X, sKPoint.Y, Radius, paint); } }
效果如下
我可以點(diǎn)的球的邊邊哦,這也是一個(gè)小技巧,點(diǎn)到球哪里,停止的時(shí)候,鼠標(biāo)還在那個(gè)位置,是不是有點(diǎn)像拖動(dòng)窗體的感覺了。
到此這篇關(guān)于WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球的文章就介紹到這了,更多相關(guān)WPF SkiaSharp拖曳小球內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#連接SQL Server數(shù)據(jù)庫的實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于C#連接SQL Server數(shù)據(jù)庫的實(shí)例內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。2020-01-01C#創(chuàng)建一個(gè)小型Web Server(Socket實(shí)現(xiàn))
這篇文章主要介紹了關(guān)于C#利用Socket實(shí)現(xiàn)創(chuàng)建一個(gè)小型Web Server的相關(guān)資料,文中通過示例代碼介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02C# 調(diào)用API函數(shù)彈出映射網(wǎng)絡(luò)驅(qū)動(dòng)器對(duì)話框問題
C#中的.net的常用對(duì)話框中沒有映射網(wǎng)絡(luò)驅(qū)動(dòng)映射對(duì)話框,所以需要用windows的API函數(shù)去實(shí)現(xiàn)彈出映射網(wǎng)絡(luò)驅(qū)動(dòng)器對(duì)話框2014-01-01C# Winform實(shí)現(xiàn)表格復(fù)制粘貼效果
這篇文章主要為大家學(xué)習(xí)介紹了如何通過C# Winform實(shí)現(xiàn)表格復(fù)制粘貼效果,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下2023-07-07c#定時(shí)運(yùn)行程序分享(定時(shí)程序)
寫了一個(gè)小的定時(shí)程序,定時(shí)觸發(fā)后可以按要求運(yùn)行其它代碼,分享一下操作過程,希望能對(duì)大家有幫助2014-01-01