01 //-------------------------------------------------------------------------------------
02 // Einfache HTML Demo-Uhr
03 // (C) Oscar Kohler
04 //-------------------------------------------------------------------------------------
05 // Form:
06 //    WebBrowser Objekt das auf eine gültige URI zeigt,
07 //    Timer Objekt 
08 //-------------------------------------------------------------------------------------
09 
10 using System;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Drawing;
15 using System.Linq;
16 using System.Text;
17 using System.Windows.Forms;
18 using System.IO;
19 
20 //-------------------------------------------------------------------------------------
21 
22 namespace clock {
23     public partial class Form1 : Form {
24         string _template;
25 
26         public Form1() {
27             InitializeComponent();
28 
29             // Html Template laden
30 
31             try {
32                 StreamReader streamReader = new StreamReader("clock.html", true);
33                 _template = streamReader.ReadToEnd();
34                 streamReader.Close();
35             }
36             catch (Exception ex) {
37                 MessageBox.Show("Lade-fehler: " + ex.Message, "Error !",
38                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
39                 return;
40             }
41 
42             // Timerobjekt auf eine Sekunde setzen und starten
43 
44             timer.Interval = 1000;
45             timer.Enabled = true;
46             timer.Start();
47         }
48 
49         // Refresh jede Sekunde
50 
51         private void timer_Tick(object sender, EventArgs e) {
52             try {
53                 // Neues Dokument (webbrowser muss auf eine gültige uri zeigen !)
54 
55                 HtmlDocument htmlDoc = webBrowser.Document.OpenNew(true);
56                 string page = _template;
57 
58                 // Zeit ersetzen
59 
60                 page = page.Replace("%HH%", DateTime.Now.Hour.ToString());
61                 page = page.Replace("%MM%", DateTime.Now.Minute.ToString("d2"));
62                 page = page.Replace("%SS%", DateTime.Now.Second.ToString("d2"));
63 
64                 // Datum ersetzen
65 
66                 page = page.Replace("%WD%", DateTime.Now.DayOfWeek.ToString());
67                 page = page.Replace("%D%", DateTime.Now.Day.ToString());
68                 page = page.Replace("%M%", DateTime.Now.Month.ToString());
69                 page = page.Replace("%Y%", DateTime.Now.Year.ToString());
70 
71                 // Doc schreiben
72 
73                 htmlDoc.Write(page);
74             }
75             catch {
76                 // ...
77             }
78         }
79     }
80 }
81 
82 //-------------------------------------------------------------------------------------