C#開發漂亮的數字時鐘

來源:互聯網
上載者:User

標籤:blog   http   os   使用   ar   for   strong   檔案   sp   

  今天用C#做了一個漂亮的數字時鐘。介面如下。

 

 

實現技術:主要是通過Graphics類的DrawImage方法來繪製數字時鐘中所有的數字,這些數字是從網上找的一些圖片檔案。時鐘使用DateTime中Now屬性來獲得不同的,時,分,秒,最後通過定時器來實現時鐘的運行狀態。

 

 

Main Code:

 

 

[c-sharp] view plaincopy
  1. //將0~9數字圖片儲存在Image數組中  
  2. private Image[] image = new Bitmap[10];  
  3.         public Form1()  
  4.         {  
  5.             InitializeComponent();  
  6.             for (int i = 0; i < 10;i++ )  
  7.             {  
  8.                 image[i] = new Bitmap(@"D:/編程/C#/數字時鐘/數字時鐘/Resources/"+i.ToString()+".jpg");  
  9.             }  
  10.         }  

    

 

[c-sharp] view plaincopy
  1. private void Form1_Paint(object sender, PaintEventArgs e)  
  2. {  
  3.     Graphics g = e.Graphics;  
  4.   
  5.     int hh = DateTime.Now.Hour;                       //取得小時數字  
  6.     int hh1 = hh / 10;  
  7.     int hh2 = hh % 10;  
  8.     g.DrawImage(image[hh1], 20, 20, 80, 180);  
  9.     g.DrawImage(image[hh2], 100, 20, 80, 180);  
  10.   
  11.     int mm = DateTime.Now.Minute;                      //取得分鐘數字  
  12.     int mm1 = mm / 10;  
  13.     int mm2 = mm % 10;  
  14.     g.DrawImage(image[mm1], 260, 20, 80, 180);  
  15.     g.DrawImage(image[mm2], 340, 20, 80, 180);  
  16.   
  17.     int ss = DateTime.Now.Second;                       //取得秒數字  
  18.     int ss1 = ss / 10;  
  19.     int ss2 = ss % 10;  
  20.     g.DrawImage(image[ss1], 500, 20, 80, 180);  
  21.     g.DrawImage(image[ss2], 580, 20, 80, 180);  
  22. }  
  23.   
  24. private void timer1_Tick(object sender, EventArgs e)  //對表單進行重繪  
  25. {  
  26.     this.Invalidate();  
  27. }  

 

另外,需要將Timer的Interval屬性設為1000mm,Enable設定為True!

C#開發漂亮的數字時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.