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

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

 

先看一個截圖:

 

繪製了一些基本形狀。

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

 

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

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

 

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

        private void clock_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            float radius = (float)sender.ActualWidth / 2 - 4;
            Vector2 center = new Vector2((float)sender.ActualWidth / 2, (float)sender.ActualWidth / 2);

            for (int i = 0; i < 60; ++i)
            {
                int borderSize = 1;
                Vector2 begin = new Vector2(radius  + center.X - 3, center.Y);
                Vector2 end = new Vector2(radius + center.X, center.Y);

                if (i % 15 == 0)
                {
                    borderSize = 4;
                    begin = new Vector2(center.X + radius - 15, center.Y);                    
                }
                else if (i % 5 == 0)
                {
                    borderSize = 2;
                    begin = new Vector2(radius + center.X - 10, center.Y);
                }

                args.DrawingSession.Transform = Matrix3x2.CreateRotation(TimeValue2Radion(i, 60), center);
                args.DrawingSession.DrawLine(begin, end, Color.FromArgb(255, 255, 255, 255), borderSize);
            }

            args.DrawingSession.DrawCircle(center, radius, Color.FromArgb(255, 255, 255, 255), 2);

            // 結點處是圓,指向處為三角
            CanvasStrokeStyle lineStyle = new CanvasStrokeStyle();
            lineStyle.StartCap = CanvasCapStyle.Round;
            lineStyle.EndCap = CanvasCapStyle.Triangle;

            // 時針
            float hours = DateTime.Now.Hour % 12 + DateTime.Now.Minute / 60.0f + DateTime.Now.Second / 60.0f / 24.0f; // 12小時制
            float intervalHours = hours - 3.0f; // 3點是0度
            float hourRadian = TimeValue2Radion(intervalHours, 12);
            args.DrawingSession.Transform = Matrix3x2.CreateRotation(hourRadian, center);
            args.DrawingSession.DrawLine(center, new Vector2(center.X + 80, center.Y), Color.FromArgb(255, 255, 255, 255), 5, lineStyle);

            // 分針
            float minutes = DateTime.Now.Minute+ DateTime.Now.Second / 60.0f;
            float intervalMinutes = minutes - 15; // 15分钟是0度
            float minuteRadian = TimeValue2Radion(intervalMinutes, 60);
            args.DrawingSession.Transform = Matrix3x2.CreateRotation(minuteRadian, center);
            args.DrawingSession.DrawLine(center, new Vector2(center.X + 100, center.Y), Color.FromArgb(255, 255, 255, 255), 2, lineStyle);

            // 秒針
            float seconds = DateTime.Now.Second;
            float intervalSeconds = seconds - 15; // 15秒是0度
            float secondRadian = TimeValue2Radion(intervalSeconds, 60);
            args.DrawingSession.Transform = Matrix3x2.CreateRotation(secondRadian, center);
            args.DrawingSession.DrawLine(center, new Vector2(center.X + 120, center.Y), Color.FromArgb(255, 255, 255, 255));
        }

        private float TimeValue2Radion(float intervalTime, int total)
        {
            return intervalTime / total * 360 * (float)Math.PI / 180;
        }

 

 

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

後面再一一補上。

转载于:https://www.cnblogs.com/lin277541/p/5059489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值