C#代码更改所有数字的字体

时间:2017-06-13 00:31:35

标签: c# fonts

我正在创建一个时钟程序,我正在程序中实现一个允许用户更改所有时钟数字字体的选项

问题是,我有一大堆代码,其中包含所有时钟编号,并且所有代码都是单独编写的;包括字体。 (见下文)

    private void t_Tick(object sender, EventArgs e)
    {
        //create graphics
        g = Graphics.FromImage(bmp);

        //get time
        int ss = DateTime.Now.Second;
        int mm = DateTime.Now.Minute;
        int hh = DateTime.Now.Hour;


        int[] handCoord = new int[2];

        //clear
        g.Clear(Color.White);

        //draw circle
        g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, WIDTH, HEIGHT);

        //draw figure
        g.DrawString("1", new Font("Times New Roman", 12), Brushes.Black, new PointF(210, 20));
        g.DrawString("2", new Font("Times New Roman", 12), Brushes.Black, new PointF(259, 72));
        g.DrawString("3", new Font("Times New Roman", 12), Brushes.Black, new PointF(280, 140));
        g.DrawString("4", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("5", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("6", new Font("Times New Roman", 12), Brushes.Black, new PointF(142, 282));
        g.DrawString("7", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("8", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("9", new Font("Times New Roman", 12), Brushes.Black, new PointF(0, 140));
        g.DrawString("10", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("11", new Font("Times New Roman", 12), Brushes.Black, new PointF(286, 140));
        g.DrawString("12", new Font("Times New Roman", 12), Brushes.Black, new PointF(140, 2));

        //second hand
        handCoord = msCoord(ss, secHAND);
        g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

        //minute hand
        handCoord = msCoord(mm, minHAND);
        g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

        //hour hand
        handCoord = hrCoord(hh % 12, mm, hrHAND);
        g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

        //load bmp in picturebox1
        clockpic.Image = bmp;

        //disp time
        this.Text = "Clock -  " + hh + ":" + mm + ":" + ss;

        //dispose
        g.Dispose();
    }

有人可以帮我找出一个更好的编写代码的方法,也许是一行代码可以让我改变所有数字的字体。

1 个答案:

答案 0 :(得分:0)

您的时钟程序是GUI还是控制台?如果它是一个GUI,那么我可以帮助你。 将整个代码块复制并粘贴到您的程序中。这是你的字体对话框。 (在使用此代码之前,不要忘记从工具箱中手动设置)

System.DBNull.Value

然后从代码中更改此行...

    private void FontsMenuItem_Click(object sender, EventArgs e)
    {
        // Show the dialog.
        DialogResult result = fontDialog1.ShowDialog();
        // See if OK was pressed.
        if (result == DialogResult.OK)
        {
            // Get Font.
            Font = fontDialog1.Font;
        }
    }

到这一行......

g.DrawString("1", new Font("Times New Roman", 12), Brushes.Black, new PointF(210, 20));

重复所有号码(1到12)