zl程序教程

您现在的位置是:首页 >  其他

当前栏目

动态获取当前日期和时间

日期 获取 时间 动态 当前
2023-09-14 09:00:21 时间

实现效果:

关键知识:

实现代码:

        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("系统时间:\n"+ //在窗体中绘出系统时间
                                    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();//启动线程
        }