WPF 顯示當前系統時間
東西很簡單,不過以前沒見過的話,如果讓人直接去實現,還得查查資料,用的東西很少
建一個WPF工程,當然silverlight也行,放置一個 TextBlock 在面板上
代碼:
<
Grid
x:Name
="LayoutRoot"
>
<
Grid.Background
>
<
LinearGradientBrush
EndPoint
="0.5,1"
StartPoint
="0.5,0"
>
<
GradientStop
Color
="Black"
Offset
="1"
/>
<
GradientStop
Color
="#FF00D1FF"
/>
</
LinearGradientBrush
>
</
Grid.Background
>
<
TextBlock
x:Name
="Tt"
FontSize
="30"
Margin
="8,43,8,68"
TextWrapping
="Wrap"
>
<
TextBlock.Foreground
>
<
LinearGradientBrush
EndPoint
="0.5,1"
StartPoint
="0.5,0"
>
<
GradientStop
Color
="Black"
Offset
="0"
/>
<
GradientStop
Color
="#FFEE580F"
Offset
="1"
/>
</
LinearGradientBrush
>
</
TextBlock.Foreground
>
</
TextBlock
>
</
Grid
>
下面就是后台C#里的東西啦,也沒什么東西,首先要為顯示當前系統時間起一個Timer。直接代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
namespace
顯示當前系統時間
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public
partial
class
MainWindow : Window
{
private
DispatcherTimer ShowTimer;
public
MainWindow()
{
this
.InitializeComponent();
// 在此點下面插入創建對象所需的代碼。
//show timer by_songgp
ShowTimer =
new
System.Windows.Threading.DispatcherTimer();
ShowTimer.Tick +=
new
EventHandler(ShowCurTimer);
//起個Timer一直獲取當前時間
ShowTimer.Interval =
new
TimeSpan(0, 0, 0, 1, 0);
ShowTimer.Start();
}
//show timer by_songgp
public
void
ShowCurTimer(
object
sender, EventArgs e)
{
//"星期"+DateTime.Now.DayOfWeek.ToString(("d"))
//獲得星期幾
this
.Tt.Text = DateTime.Now.ToString(
"dddd"
,
new
System.Globalization.CultureInfo(
"zh-cn"
));
this
.Tt.Text +=
" "
;
//獲得年月日
this
.Tt.Text += DateTime.Now.ToString(
"yyyy年MM月dd日"
);
//yyyy年MM月dd日
this
.Tt.Text +=
" "
;
//獲得時分秒
this
.Tt.Text += DateTime.Now.ToString(
"HH:mm:ss:ms"
);
//System.Diagnostics.Debug.Print("this.ShowCurrentTime {0}", this.ShowCurrentTime);
}
}
}
|
這里記得要加一個頭文件:using System.Windows.Threading;