1
我使用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
目標
問題
沒有看到你的設計師代碼,我不能肯定地說,但如果標籤在Z順序上位於最前面,它將在您的自定義繪畫之後被繪製。我要麼在Z順序的頂部創建一個完全透明的控件,在最後畫出手。另外,你的位圖是透明的嗎?如果沒有繪畫,最後可能會覆蓋您的標籤。 –
不要使用'TextBox'來顯示文本,只需使用'TextRenderer.DrawText'繪製文本。 –
@SeanK設計器代碼在我的可下載鏈接中粘貼在我的問題中。 – taji01