c# - Analog Clock - Draw clocks arm over a Label -
i'm using winforms create clock. problem clocks hand going under panel/label. tried paint hand on top of panel/label wasn't successful. tried moving panel/label , hand front , didn't work either. tried doing panel_digital_timer.parent = picturebox1 made transparent panel. how can move clocks hand in front of panel/label?
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 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 input image 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 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 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 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 4 points of image, 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(); } } download project: http://www.filedropper.com/clockprojectquestion
goal
problem
don't use textbox show text, draw text yourself.
the drawings draw on graphics object of control drawn on surface of control , can not drawn on child controls or other stacked controls. in above code instead of using textbox show text, should draw text using textrenderer.drawtext.
for example:
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); } 


Comments
Post a Comment