WPF 显示时间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;//引用

namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void label1_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);   //间隔1
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

        }

        void timer_Tick(object sender, EventArgs e)
        {
            label1.Content = DateTime.Now.ToString("yyyy年MM月dd日 HH时hh分ss秒");//yyyy年MM月dd日 HH:hh:ss 也可以
        }
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF 秒表 计时器 定时关机 到计时关机 public const uint WM_SYSCOMMAND = 0x0112; public const uint SC_MONITORPOWER = 0xF170; [DllImport("user32")] public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, uint wParam, int lParam); /// /// 关闭显示器 /// public void CloseScreen() { IntPtr windowHandle = Process.GetCurrentProcess().MainWindowHandle; SendMessage(windowHandle, WM_SYSCOMMAND, SC_MONITORPOWER, 2); // 2 为关闭显示器, -1则打开显示器 } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; using IniFiles; namespace StopWatch { /// /// shutdonwCtrl.xaml 的交互逻辑 /// public partial class shutdonwCtrl : UserControl { DispatcherTimer timer1; DispatcherTimer timer2; public shutdonwCtrl() { InitializeComponent(); timer1 = new DispatcherTimer(); timer1.Tick += new EventHandler(OnTimer1); timer1.Interval = new TimeSpan(0, 0, 1); timer2 = new DispatcherTimer(); timer2.Tick += new EventHandler(OnTimer2); timer2.Interval = new TimeSpan(0, 0, 1); btn_cancel.IsEnabled = false; cancel1.IsEnabled = false; } IniFile INI = new IniFile(IniFile.AppIniName); public void LoadIni() { cbo_hour.Text = INI.ReadString("定时关机", "时", "0"); cbo_Minute.Text = INI.ReadString("定时关机", "分", "0"); cbo_Second.Text = INI.ReadString("定时关机", "秒", "0"); cbo1.Text = INI.ReadString("到计时关机", "分", "0"); //combobox1.Text = INI.ReadString("到计时", "时", "0"); //combobox2.Text = INI.ReadString("到计时", "分", "0"); //combobox3.Text = INI.ReadString("到计时", "秒", "0"); } public void SaveIni() { INI.WriteString("定时关机", "时", cbo_hour.Text); INI.WriteString("定时关机", "分", cbo_Minute.Text); INI.WriteString("定时关机", "秒", cbo_Second.Text); INI.WriteString("到计时关机", "分", cbo1.Text); } private void ShutDown() { System.Diagnostics.Process.Start("shutdown.exe", "-s -t 1"); } private void OnTimer1(object sender,EventArgs e) { if (second > 0) second--; label1.Content = string.Format("Windows将在 {0} 关机", TimerClass.GetTimeString1(second)); if (second <= 0 && !cbo1.IsEnabled) { ShutDown(); } } int second = 0; private void Button_Click(object sender, RoutedEventArgs e) { second = Convert.ToInt32(cbo1.Text) * 60; if (second <= 0) { MessageBox.Show("数值必须大于0!"); return; } timer1.Start(); cbo1.IsEnabled = false; cancel1.IsEnabled = true; start1.IsEnabled = false; } private void Button_Click_1(object sender, RoutedEventArgs e) { label1.Content = " "; timer1.Stop(); second = 0; cbo1.IsEnabled = true; cancel1.IsEnabled = false; start1.IsEnabled = true; } private void OnTimer2(object sender, EventArgs e) { if ( DateTime.Now.Hour == Convert.ToInt32(cbo_hour.Text) && DateTime.Now.Minute == Convert.ToInt32(cbo_Minute.Text) && DateTime.Now.Second == Convert.ToInt32(cbo_Second.Text) ) { ShutDown(); } } private void Button_Click_2(object sender, RoutedEventArgs e) { int s = Convert.ToInt32(cbo_hour.Text) * 3600 + Convert.ToInt32(cbo_Minute.Text) * 60 + Convert.ToInt32(cbo_Second.Text); timer2.Start(); label2.Content = string.Format("Windows将在 {0} 关机", TimerClass.GetTimeString1(s)); btn_start.IsEnabled = false; btn_cancel.IsEnabled = true; cbo_hour.IsEnabled = false; cbo_Minute.IsEnabled = false; cbo_Second.IsEnabled = false; } private void btn_cancel_Click(object sender, RoutedEventArgs e) { label2.Content = ""; timer2.Stop(); btn_start.IsEnabled = true; btn_cancel.IsEnabled = false; cbo_hour.IsEnabled = true; cbo_Minute.IsEnabled = true; cbo_Second.IsEnabled = true; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Windows.Threading; using IniFiles; namespace StopWatch { /// /// TimerCtrl.xaml 的交互逻辑 /// public partial class TimerCtrl : UserControl { private DispatcherTimer timer1 = null; private DispatcherTimer timer2 = null; int SND_SYNC = 0x0000; /* play asynchronously */ //异步 int SND_ASYNC = 0x0001; int SND_LOOP = 8; [DllImport("winmm")] public static extern bool PlaySound(string szSound, IntPtr hMod, int flags); private void playSound3() { Task tsk = new Task(new Action(proc)); tsk.Start(); } private void proc() { PlaySound(@"Sound\2.wav", IntPtr.Zero, SND_SYNC); PlaySound(@"Sound\2.wav", IntPtr.Zero, SND_SYNC); PlaySound(@"Sound\2.wav", IntPtr.Zero, SND_SYNC); } public TimerCtrl() { InitializeComponent(); timer1 = new DispatcherTimer(); timer1.Tick += new EventHandler(OnTimer1); timer1.Interval = new TimeSpan(0, 0, 1); timer2 = new DispatcherTimer(); timer2.Tick += new EventHandler(OnTimer2); timer2.Interval = new TimeSpan(0, 0, 1); btn_reset1.IsEnabled = false; btn_pause1.IsEnabled = false; } int h = 0; int m = 0; int s = 0; int seconds = 0; private void OnTimer1(object sender, EventArgs e) { if (seconds < 1) { PlaySound(@"Sound\2.wav", IntPtr.Zero, SND_ASYNC); btn_start.IsEnabled = false; btn_reset1.IsEnabled = true; btn_pause1.IsEnabled = false; combobox1.IsEnabled = true; combobox2.IsEnabled = true; combobox3.IsEnabled = true; return; } seconds--; label1.Content = TimerClass.GetTimeString1(seconds); } private void btn_start_Click(object sender, RoutedEventArgs e) { h = Convert.ToInt32(combobox1.Text); m = Convert.ToInt32(combobox2.Text); s = Convert.ToInt32(combobox3.Text); seconds = h * 3600 + m * 60 + s; label1.Content = TimerClass.GetTimeString1(seconds); timer1.Start(); btn_start.IsEnabled = false; btn_reset1.IsEnabled = true; btn_pause1.IsEnabled = true; combobox1.IsEnabled = false; combobox2.IsEnabled = false; combobox3.IsEnabled = false; } private void btn_reset_Click(object sender, RoutedEventArgs e) { seconds = 0; label1.Content = TimerClass.GetTimeString1(seconds); timer1.Stop(); btn_start.IsEnabled = true; btn_reset1.IsEnabled = false; btn_pause1.IsEnabled = false; combobox1.IsEnabled = true; combobox2.IsEnabled = true; combobox3.IsEnabled = true; } private void pause1_Click(object sender, RoutedEventArgs e) { if (btn_pause1.Content.ToString() == "暂停") { timer1.Stop(); btn_pause1.Content = "继续"; } else { timer1.Start(); btn_pause1.Content = "暂停"; } } int SEC = 0; private void OnTimer2(object sender, EventArgs e) { if (SEC < 1) { timer2.Stop(); playSound3(); btn_quick.IsEnabled = true; btn_cancel2.IsEnabled = false; combobox4.IsEnabled = true; return; } SEC--; label4.Content = TimerClass.GetTimeString1(SEC); } private void Button_Click(object sender, RoutedEventArgs e) { SEC = Convert.ToInt32(combobox4.Text) * 60; timer2.Start(); combobox4.IsEnabled = false; btn_quick.IsEnabled = false; btn_cancel2.IsEnabled = true; } private void Button_Click_1(object sender, RoutedEventArgs e) { SEC = 2; label4.Content = "00:00:00"; timer2.Stop(); combobox4.IsEnabled = true; btn_quick.IsEnabled = true; } IniFile INI = new IniFile(IniFile.AppIniName); public void LoadIni() { combobox1.Text = INI.ReadString("到计时", "时", "0"); combobox2.Text = INI.ReadString("到计时", "分", "0"); combobox4.Text = INI.ReadString("快速计时", "分", "0"); } public void SaveIni() { INI.WriteString("到计时", "时", combobox1.Text); INI.WriteString("到计时", "分", combobox2.Text); INI.WriteString("到计时", "秒", combobox3.Text); INI.WriteString("快速计时", "分", combobox4.Text); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; using System.Windows.Threading; namespace StopWatch { /// /// WatchCtrl.xaml 的交互逻辑 /// public partial class WatchCtrl : UserControl { public WatchCtrl() { InitializeComponent(); timer1 = new DispatcherTimer(); timer1.Tick += new EventHandler(OnTimer1); timer1.Interval = new TimeSpan(0, 0, 1); timer1_1 = new DispatcherTimer(); timer1_1.Tick += new EventHandler(OnTimer1_1); timer1_1.Interval = new TimeSpan(0, 0, 0, 100); timer2 = new DispatcherTimer(); timer2.Tick += new EventHandler(OnTimer2); timer2.Interval = new TimeSpan(0, 0, 1); } private DispatcherTimer timer1 = null; private DispatcherTimer timer1_1 = null; private DispatcherTimer timer2 = null; private void OnTimer1(object sender, EventArgs e) { second++; label1.Content = TimerClass.GetTimeString1(second) ;//DateTime.Now.ToString("h:mm:ss"); } public int second = 0; private int second1 = 0; public int count = 0; private void OnTimer1_1(object sender, EventArgs e) { //label1.Content = TimerClass.GetTimeString1(second) + "." + count.ToString();//DateTime.Now.ToString("h:mm:ss"); //count++; //if (count > 9) // count = 0; } private void OnTimer2(object sender, EventArgs e) { second1++; label2.Content = TimerClass.GetTimeString1(second1) ;//.ToString("h:mm:ss"); } private void btn_start_Click(object sender, RoutedEventArgs e) { timer1.Start(); timer2.Start(); if (btn_start.Content.ToString() == "停止") { btn_start.Content = "开始"; btn_reset.Content = "复位"; timer1.Stop(); timer2.Stop(); } else { btn_start.Content = "停止"; btn_reset.Content = "计次"; } } private int counter = 0; private void btn_reset_Click(object sender, RoutedEventArgs e) { if (btn_reset.Content.ToString() == "复位") { second = 0; second1 = 0; label1.Content = "00:00:00"; label2.Content = "00:00:00"; timer1.Stop(); timer2.Stop(); } else { if (second1 > 0) { counter++; listbox1.Items.Add(string.Format(" {0}\t{1}", counter, label2.Content)); } second1 = 0; label2.Content = "00:00:00"; } if (btn_reset.Content.ToString() == "复位" && btn_start.Content.ToString()=="开始") { listbox1.Items.Clear(); } } } }
### 回答1: WPF是一种强大的UI框架,它提供了许多控件,其中包括时间选择器控件。但是,由于在特定的应用程序中,需要自定义时间选择器以满足一些个性化需求。在这种情况下,可以使用WPF自定义控件来创建自己的时间选择器控件。 首先,在WPF中创建时间选择器控件,需要使用Calendar控件和TimePicker控件。Calendar控件用于显示日期,而TimePicker控件用于选择时间时间选择器控件的主体是StackPanel控件。在StackPanel控件中添加了两个控件Calendar和TimePicker,以实现时间选择器的基本功能。 然后,需要在时间选择器控件中定义一些附加属性,例如:选定日期、选定时间等等,以实现一些高级功能。 最后,为时间选择器控件添加样式,并实现一些触发器和动画效果,以使其外观和功能与应用程序的主题相匹配。 实现WPF自定义时间选择器可能需要一些时间和经验,但对于需要一个不寻常的时间选择器的应用程序来说,是值得的。 这样的时间选择器是用户友好的,具有很好的设计和功能,并且以C#编写,可以很容易地与WPF应用程序集成。 ### 回答2: WPF自定义时间选择器是一种功能强大、灵活性高的工具,它可以根据需求自行设计不同的选择器,可以实现小时、分钟、秒数的选择等多种功能。 首先,我们需要使用WPF自带的DatePicker控件和TimePicker控件来实现时间选择器。接下来,我们可以自定义控件的样式和模板,使其更符合我们的设计需求。 在自定义控件的样式时,我们需要设置控件的各个属性,比如控件的边框、背景、字体等。同时,我们可以通过设置样式来调整控件的布局和显示效果。 在时间选择器的实现中,需要涉及到一些比较复杂的计算,比如计算时间的差值、时间的格式转换等。我们可以使用C#中的DateTime类和TimeSpan类来实现这些功能。 最后对于自定义时间选择器的控件事件,需要自定义一些控件事件,使其更加符合我们的设计需求。比如增加或减少系统时钟里的时间。 总而言之,实现WPF自定义时间选择器需要对WPF控件、样式、模板、计算和控件事件等各个方面有深入的了解。只有掌握了这些知识,才能够设计出优秀的时间选择器,满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值