动态获取当前日期和时间

实现效果:

关键知识:

实现代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Threading.Thread thread = //创建线程
                new System.Threading.Thread(
                () =>   //使用lambda表达式
                {
                    while (true) //无限循环
                    {
                        this.Invoke(    //操作窗体线程
                            (MethodInvoker)delegate() //使用匿名方法
                            {
                               this.Refresh();  //刷新窗体
                                Graphics g =    //创捷绘图对象
                                    CreateGraphics();
                                g.DrawString("系统时间:
"+ //在窗体中绘出系统时间
                                    DateTime.Now.ToString(
                                    "yyy年MM月dd日hh时mm分ss秒"),
                                    new Font("宋体",15),
                                    Brushes.Blue,
                                    new Point(10,10));
                            });
                        System.Threading.Thread.Sleep(1000);//线程挂起1秒
                    }
                });
            thread.IsBackground = true;//将线程设置为后台线程
            thread.Start();//启动线程
        }

  

原文地址:https://www.cnblogs.com/feiyucha/p/9911399.html