您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么使用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球”,在日常操作中,相信很多人在怎么使用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”怎么使用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
WPF的拖曳效果,基本配置一下,就可以了,但是自繪的話,就得自己控制,按鍵點(diǎn)擊,按鍵移動(dòng)和按鍵松開的事件,與其配合達(dá)到目的。
這個(gè)效果實(shí)現(xiàn)了,其實(shí)也變相的實(shí)現(xiàn)了WPF里的拖動(dòng)效果,這個(gè)效果用著還是很方便的。
但是代碼,確十分的簡單。
新建一個(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; } } });
鼠標(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); } }
到此,關(guān)于“怎么使用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。