c# - 模拟时钟 - 在标签上绘制时钟臂

标签 c# .net winforms graphics gdi+

我正在使用 WinForms 创建时钟。问题是时钟指针位于我的面板/标签下方。我尝试将手画在面板/标签上,但没有成功。我还尝试将面板/标签移到后面,将手移到前面,但这也效果不佳。我还尝试做类似 panel_digital_Timer.Parent = pictureBox1 的事情,它制作了一个透明面板。如何将时钟指针移到面板/标签前面?

public partial class Form1 : Form
{
    private Bitmap bmp;
    private int angle = 0;
    private int counter_Time;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Paint += PictureBox1_Paint;
        bmp = Properties.Resources.Clock_Arm;

        //pictureBoxOverlay.BackColor = Color.Transparent;

        //// Change parent for overlay PictureBox...
        pictureBoxOverlay.Parent = pictureBox1;

        panel_digital_Timer.BackColor = Color.Transparent;
    }

    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
        var rbmp = rotateCenter(bmp, angle);
        e.Graphics.TranslateTransform((pictureBox1.Width - rbmp.Width) / 2,
            (pictureBox1.Height - rbmp.Height) / 2);
        e.Graphics.DrawImage(rbmp, 0, 0);
        e.Graphics.ResetTransform();
    }


    /// <summary>
    /// Rotates the input image by theta degrees around center.
    /// </summary>
    public static Bitmap rotateCenter(Bitmap bmpSrc, float theta)
    {
        Matrix mRotate = new Matrix();
        //mRotate.Translate(bmpSrc.Width / -2, bmpSrc.Height / -2, MatrixOrder.Append);
        mRotate.Translate(bmpSrc.Width / -2, -bmpSrc.Height, MatrixOrder.Append);
        mRotate.RotateAt(theta, new Point(0, 0), MatrixOrder.Append);
        using (GraphicsPath gp = new GraphicsPath())
        {  // transform image points by rotation matrix
            gp.AddPolygon(new Point[] { new Point(0, 0), new Point(bmpSrc.Width, 0), new Point(0, bmpSrc.Height) });
            gp.Transform(mRotate);
            PointF[] pts = gp.PathPoints;

            // create destination bitmap sized to contain rotated source image
            Rectangle bbox = boundingBox(bmpSrc, mRotate);
            Bitmap bmpDest = new Bitmap((int)(bbox.Width * 2), (int)(bbox.Height * 2));

            using (Graphics gDest = Graphics.FromImage(bmpDest))
            {  // draw source into dest
                Matrix mDest = new Matrix();
                //mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
                mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
                gDest.Transform = mDest;
                gDest.DrawImage(bmpSrc, pts);
                //drawAxes(gDest, Color.Red, 0, 0, 1, 100, "");
                return bmpDest;
            }
        }
    }

    private static Rectangle boundingBox(Image img, Matrix matrix)
    {
        GraphicsUnit gu = new GraphicsUnit();
        Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu));

        // Transform the four points of the image, to get the resized bounding box.
        Point topLeft = new Point(rImg.Left, rImg.Top);
        Point topRight = new Point(rImg.Right, rImg.Top);
        Point bottomRight = new Point(rImg.Right, rImg.Bottom);
        Point bottomLeft = new Point(rImg.Left, rImg.Bottom);
        Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft };
        GraphicsPath gp = new GraphicsPath(points,
        new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line });
        gp.Transform(matrix);
        return Rectangle.Round(gp.GetBounds());
    }

    private void Timer_StopClock_Tick(object sender, EventArgs e)
    {
        if (counter_Time == 360)
        {
            counter_Time = 0;
        }
        else
        {
            counter_Time += 15;
        }

        angle = counter_Time;
        //angle += counter_Time;

        Console.WriteLine(counter_Time);
        pictureBox1.Invalidate();
    }
}

下载项目:http://www.filedropper.com/clockprojectquestion

目标

enter image description here

问题

enter image description here

最佳答案

不要使用TextBox要显示文本,请自己绘制文本。

您在 Graphics 上绘制的图画Control 的对象将在控件的表面上绘制,并且不能在子控件或其他堆叠控件上绘制。因此,在上面的代码中,不要使用 TextBox要显示文本,您应该使用 TextRenderer.DrawText 绘制文本.

示例

句柄Tick带有 Interval 的计时器事件设置为 1000 并调用 pictureBox1.Invalidate();Tick事件处理程序。然后在句柄 Paint图片框的事件是这样的:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.Clear(Color.White);
    var r1 = this.pictureBox1.ClientRectangle;
    r1.Inflate(-3, -3);
    g.DrawEllipse(Pens.Black, r1);
    var r2 = r1;
    r2.Inflate(-5, -5);
    TextRenderer.DrawText(g, DateTime.Now.ToString("HH:mm:ss"), this.Font,
        new Rectangle(r1.Left, r1.Top + 2 * r1.Height / 3, r1.Width, r1.Height / 3),
        Color.Black);
    e.Graphics.TranslateTransform(r2.Left + r2.Width / 2, r2.Top + r2.Height / 2);
    var c = g.BeginContainer();
    g.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.RotateTransform(DateTime.Now.Hour * 30f + DateTime.Now.Minute / 2f);
    g.FillEllipse(Brushes.Black, -5, -5, 10, 10);
    using (var p = new Pen(Color.Black, 4))
        g.DrawLine(p, 0, 0, 0, -r2.Height / 2 + 30);
    g.EndContainer(c);
    c = g.BeginContainer();
    g.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.RotateTransform(DateTime.Now.Minute * 6);
    using (var p = new Pen(Color.Black, 2))
        g.DrawLine(p, 0, 0, 0, -r2.Height / 2 + 10);
    g.EndContainer(c);
    c = g.BeginContainer();
    g.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.RotateTransform(DateTime.Now.Second * 6);
    using (var p = new Pen(Color.Red, 2))
        g.DrawLine(p, 0, 10, 0, -r2.Height / 2 + 15);
    g.EndContainer(c);
}

那么结果会是这样的:

enter image description here

关于c# - 模拟时钟 - 在标签上绘制时钟臂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539030/

相关文章:

c# - RouteValues 与 QueryString MVC?

c# - Windows Azure 中的 APNS

c# - 如何从 DataGridView 中的一行获取 DataRow

c# - 有 Form.Invoke() 方法吗?

c# - 如何测试通过公共(public)方法修改的私有(private)字段

c# - 如何使用 LINQ 使用 Azure AD 图形 API 客户端库 2.0 查找具有特定许可证的 Azure AD 用户

c# - 闭包和 lambda 之间的区别?

.net - 在Visual Studio中转储本地窗口的文本

.Net & WCF - 如何创建 REST 而不是 SOAP?

c# - OleDB 选择语句