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

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

 

先看一個截圖:

【WIN10】WIN2D——基本圖形的繪製_第1张图片

 

繪製了一些基本形狀。

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;
        }

 

 

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

後面再一一補上。

你可能感兴趣的:(【WIN10】WIN2D——基本圖形的繪製)