【WIN10】WIN2D——基本圖形的繪製

时间:2022-03-25 04:55:23

DEMO下載地址:http://yunpan.cn/c3iNuHFFAcr8h (提取码:8e48)

先看一個截圖:

【WIN10】WIN2D——基本圖形的繪製

繪製了一些基本形狀。

DEMO的繪製代碼都非常簡單,不想在博客裡細說了,看代碼更為清晰些。

可能繪製扇形的代碼有些麻煩些。

微軟是使用鐘錶的轉動方向(順時針)作為弧度運轉方向的,所以角度30度,是會在x座標下的,而不是通常的在x座標上面。

帖一下畫鐘錶的代碼,是非常簡單的:

        private void clock_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
float radius = (float)sender.ActualWidth / - ;
Vector2 center = new Vector2((float)sender.ActualWidth / , (float)sender.ActualWidth / ); for (int i = ; i < ; ++i)
{
int borderSize = ;
Vector2 begin = new Vector2(radius + center.X - , center.Y);
Vector2 end = new Vector2(radius + center.X, center.Y); if (i % == )
{
borderSize = ;
begin = new Vector2(center.X + radius - , center.Y);
}
else if (i % == )
{
borderSize = ;
begin = new Vector2(radius + center.X - , center.Y);
} args.DrawingSession.Transform = Matrix3x2.CreateRotation(TimeValue2Radion(i, ), center);
args.DrawingSession.DrawLine(begin, end, Color.FromArgb(, , , ), borderSize);
} args.DrawingSession.DrawCircle(center, radius, Color.FromArgb(, , , ), ); // 結點處是圓,指向處為三角
CanvasStrokeStyle lineStyle = new CanvasStrokeStyle();
lineStyle.StartCap = CanvasCapStyle.Round;
lineStyle.EndCap = CanvasCapStyle.Triangle; // 時針
float hours = DateTime.Now.Hour % + DateTime.Now.Minute / 60.0f + DateTime.Now.Second / 60.0f / 24.0f; // 12小時制
float intervalHours = hours - 3.0f; // 3點是0度
float hourRadian = TimeValue2Radion(intervalHours, );
args.DrawingSession.Transform = Matrix3x2.CreateRotation(hourRadian, center);
args.DrawingSession.DrawLine(center, new Vector2(center.X + , center.Y), Color.FromArgb(, , , ), , lineStyle); // 分針
float minutes = DateTime.Now.Minute+ DateTime.Now.Second / 60.0f;
float intervalMinutes = minutes - ; // 15分钟是0度
float minuteRadian = TimeValue2Radion(intervalMinutes, );
args.DrawingSession.Transform = Matrix3x2.CreateRotation(minuteRadian, center);
args.DrawingSession.DrawLine(center, new Vector2(center.X + , center.Y), Color.FromArgb(, , , ), , lineStyle); // 秒針
float seconds = DateTime.Now.Second;
float intervalSeconds = seconds - ; // 15秒是0度
float secondRadian = TimeValue2Radion(intervalSeconds, );
args.DrawingSession.Transform = Matrix3x2.CreateRotation(secondRadian, center);
args.DrawingSession.DrawLine(center, new Vector2(center.X + , center.Y), Color.FromArgb(, , , ));
} private float TimeValue2Radion(float intervalTime, int total)
{
return intervalTime / total * * (float)Math.PI / ;
}

因為今天只寫了這麼一個例子,就先發一個了。

後面再一一補上。