C# 簡單的loading提示控制項

来源:http://www.cnblogs.com/onegarden/archive/2017/09/03/7468730.html
-Advertisement-
Play Games

自己畫一個轉圈圈的控制項 ...


自己畫一個轉圈圈的控制項

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExerciseUIPrj.controls
{
    public partial class LoadControl : Control
    {
        Color beginColor = Color.Blue;
        Color endColor = Color.Red;
        int wid = 10;
        int curindex = 0;
        Timer timer;
        int instervel = 200;
        string loadStr = "loading....";

        public LoadControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer, true);
            this.MinimumSize = new Size(40, 80);
            if (!DesignMode)
            {
                Start();
            }
        }

        public void Start()
        {
            if (timer == null)
            {
                timer = new Timer();
                timer.Interval = instervel;
                timer.Tick += Timer_Tick;
            }
            timer.Enabled = true;
        }
        public void Stop()
        {
            if (timer != null)
            {
                timer.Enabled = false;
            }
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            curindex++;
            curindex = curindex >= wid ? 0 : curindex;
            Refresh();
        }
        //計算各種圈圈相關
        Point getPoint(double d, double r, Point center)
        {
            int x = (int)(r * Math.Cos(d * Math.PI / 180.0));
            int y = (int)(r * Math.Sin(d * Math.PI / 180.0));
            return new Point(center.X + x, center.Y - y);
        }
        GraphicsPath getPath(Point a, Point b)
        {
            Point c, d, e, f;
            int h = 2;
            Vertical(a, b, h, out c, out d);
            Vertical(b, a, h, out e, out f);
            GraphicsPath path = new GraphicsPath();
            path.AddPolygon(new Point[] { c, d, e, f });
            path.CloseAllFigures();
            return path;


        }
        bool Vertical(Point pointa, Point pointb, double R, out Point pointc, out Point pointd)
        {
            pointc = new Point();
            pointd = new Point();
            try
            {
                //(X-xa)^2+(Y-ya)^2=R*R    距離公式
                //(X-xa)*(xb-xa)+(Y-ya)*(yb-ya)=0   垂直
                //解方程得兩點即為所求點
                var cx = pointa.X - (pointb.Y - pointa.Y) * R / Distance(pointa, pointb);
                var cy = pointa.Y + (pointb.X - pointa.X) * R / Distance(pointa, pointb);

                var dx = pointa.X + (pointb.Y - pointa.Y) * R / Distance(pointa, pointb);
                var dy = pointa.Y - (pointb.X - pointa.X) * R / Distance(pointa, pointb);
                pointc = new Point((int)cx, (int)cy);
                pointd = new Point((int)dx, (int)dy);
                return true;
            }
            catch
            {
                //如果A,B兩點重合會報錯,那樣就返回false
                return false;
            }
        }
        double Distance(double xa, double ya, double xb, double yb)
        {
            double L;
            L = Math.Sqrt(Math.Pow(xa - xb, 2) + Math.Pow(ya - yb, 2));
            return L;
        }
        double Distance(Point pa, Point pb)
        {
            return Distance(pa.X, pa.Y, pb.X, pb.Y);
        }
        GraphicsPath getPath(double d, double r, Point c)
        {
            var p1 = getPoint(d, r / 2.0, c);
            var p2 = getPoint(d, r, c);
            return getPath(p1, p2);
        }
        //算漸變色
        Color[] getColors()
        {
            int dr = (int)((endColor.R - beginColor.R) / (double)wid);
            int dg = (int)((endColor.G - beginColor.G) / (double)wid);
            int db = (int)((endColor.B - beginColor.B) / (double)wid);
            List<Color> colors = new List<Color>();
            for (int i = 0; i < wid; i++)
            {
                colors.Add(Color.FromArgb(beginColor.R + dr * i, beginColor.G + dg * i, beginColor.B + db * i));
            }
            return colors.ToArray();

        }

        //畫圈圈
        void drawRect(Graphics g)
        {

            int r = (int)(Size.Height / 2.0);
            Point center = new Point(r, r);
            var colors = getColors();
            int findex = curindex;
            for (int i = 0; i < wid; i++)
            {
                double d = (360.0 / wid) * i;
                var p = getPath(d, r, center);
                int cindex = findex + i;
                cindex = cindex >= wid ? cindex - wid : cindex;
                g.FillPath(new SolidBrush(colors[cindex]), p);
                
            }
        }
        //畫字元串
        void drawString(Graphics g)
        {
            if (Size.Height >= Size.Width) return;
            Rectangle rect = new Rectangle(new Point(Size.Height, 0), new Size(Size.Width - Size.Height, Size.Height));
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString(loadStr, Font, Brushes.Black, rect,sf);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            drawRect(g);
            drawString(g);
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (Size.Height > Size.Width)
            {
                Size = new Size(Size.Height, Size.Height);
            }
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在伺服器端 Web 應用程式框架中,其中非常重要的設計是開發人員如何將URL與伺服器上的資源進行匹配,以便正確的處理請求。最簡單的方法是將 URL 映射到磁碟上的物理文件,在 Razor 頁面框架中,ASP.NET團隊就是這樣實現的。 關於 Razor 頁面框架如何將 URL 與文件相匹配,有一些規 ...
  • 上篇有朋友提及到如果nginx做集群後應該還會有下一篇文章主講session控制,一般來說就是登陸;本篇分享的內容不是關於分散式session內容,而是netcore自帶的授權Authorize,Authorize粗略的用法,希望能對大家有好的幫助; web網站session和cookie關係 在N ...
  • 微軟在千禧年推出 .NET戰略,併在兩年後推出第一個版本的.NET Framework和IDE(Visual Studio.NET 2002,後來改名為Visual Studio),如果你是一個資深的.NET程式員,相信傳統的.NET應用的開發方式已經深深地烙印在你的腦子裡面。.NET Core打來... ...
  • 以下是我根據自身的情況來總結的ASP.NET 知識體系 ...
  • Response.Write 、RegisterClientScriptBlock和RegisterStartupScript總結 Page.ClientScript.RegisterStartupScript用法小結 原文鏈接:http://blog.csdn.net/qiujialongjjj/ ...
  • 套用mui官方文檔的一句話:“開發者只需關心業務邏輯,實現載入更多數據即可”。真的是不錯的框架。 想更多的瞭解這個框架:http://dev.dcloud.net.cn/mui/ 那麼如何實現下拉刷新,上拉載入的功能呢? 首先需要一個容器: 然後進行初始化操作,通過mui.init方法中pullRe ...
  • 一、WCF技術我該如何學習? 阿笨的回答是:作為初學者的我們,那麼請跟著阿笨一起玩WCF吧,阿笨將帶領大家如何以正確的姿勢去掌握WCF技術。由於WCF技術知識點太多了,就純基礎概念性知識都可以單獨出一本書來講解,本次分享課程《C#面向服務編程技術WCF從入門到實戰演練》開課之前,阿笨還是希望從沒瞭解 ...
  • 開篇先來說一下我和來畫的故事,以及寫這篇文章的初衷。 今年年初時,我還在北京,在 Face++,做著人臉識別技術的 Windows 和 Android 端,做著人工智慧終將實現世間所有美好的夢。這時的我已經離開 UWP,甚至 C# 很久了,寫著 C++ 和 Java,當時真的沒想過會再次回到 UWP ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...