C# Analoge Uhr auf Hintergrund
Posted: 20 May 2025, 15:19
Ich muss lediglich die Uhr auf einem Panel mit HintergrundImage anzeigen, erscheint aber immer noch mit schwarzem Hintergrund. Ich habe das Innercolor und die äußere Verschreibung der analogen Uhr auf transparentes Lesen gesetzt. Hilfe bitte. < /P>
Dies ist mein Panel -Code: < /p>
Dies ist mein Panel -Code: < /p>
Code: Select all
public partial class FrMain : Form
{
private readonly ClokStyle _ClokStyle = ClokStyle.Standart;
readonly HandStyle _handStyle = HandStyle.Uniform;
private readonly Ticks _ticks = Ticks.All;
private readonly Numbers _numbers = Numbers.All;
private readonly Color _innerColor = Color.Transparent;
private readonly Color _outerColor = Color.Transparent;
private readonly Color _handColor = Color.White;
private readonly Color _secHandColor = Color.Red;
private readonly Color _tickColor = Color.White;
private readonly Color _textColor = Color.White;
public object Standard { get; private set; }
public FrMain()
{
InitializeComponent();
Cursor.Hide();
TmrDateTime.Start();
}
private void FrMain_Load(object sender, EventArgs e)
{
}
private void Panel_Paint(object sender, PaintEventArgs e)
{
var gOne = e.Graphics;
BufferedGraphicsContext CurrentContext = BufferedGraphicsManager.Current;
BufferedGraphics g = CurrentContext.Allocate(gOne, ClientRectangle);
g.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
g.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Graphics.Clear(panel.BackColor);
StringFormat stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
var Ox = panel.Width / 2;
var Oy = panel.Height / 2;
if (panel.Width > panel.Height)
{
_ = panel.Height;
}
else
{
_ = panel.Width;
}
#region DrawEllipse
GraphicsPath path = new GraphicsPath();
path.AddEllipse(new Rectangle(panel.Width / 100, panel.Height / 30, panel.Width - (panel.Width / 30) * 2, panel.Height - (panel.Height / 30) * 2));
PathGradientBrush pthGrBrush = new PathGradientBrush(path)
{
CenterColor = _innerColor
};
Color[] colors = { _outerColor };
pthGrBrush.SurroundColors = colors;
g.Graphics.FillEllipse(pthGrBrush, new Rectangle(panel.Width / 30, panel.Height / 30, panel.Width - (panel.Width / 30) * 2, panel.Height - (panel.Height / 30) * 2));
#endregion
#region DrawTicks
g.Graphics.DrawEllipse(new Pen(_tickColor), new Rectangle(panel.Width / 100, panel.Height / 100, panel.Width - (panel.Width / 100) * 2, panel.Height - (panel.Height / 100) * 2));
g.Graphics.TranslateTransform(Ox, Oy);
switch (_ticks)
{
case Ticks.All:
for (int i = 0; i < 4; i++)
{
g.Graphics.DrawLine(new Pen(_tickColor, 6), new Point((panel.Width / 2 - panel.Width / 20) * -1, 0), new Point((panel.Width / 2 - panel.Width / 20) * -1 + 13, 0));
for (int j = 0; j < 15; j++)
{
g.Graphics.DrawLine(new Pen(_tickColor, 1), new Point((panel.Width / 2 - panel.Width / 20) * -1, 0), new Point((panel.Width / 2 - panel.Width / 20) * -1 + 9, 0));
if (j % 5 == 0)
g.Graphics.DrawLine(new Pen(_tickColor, 4), new Point((panel.Width / 2 - panel.Width / 20) * -1, 0), new Point((panel.Width / 2 - panel.Width / 20) * -1 + 13, 0));
g.Graphics.RotateTransform(6);
}
}
break;
}
#endregion
#region DrawNumbers
g.Graphics.ResetTransform();
g.Graphics.TranslateTransform(Ox, Oy);
double radius = ((panel.Width - panel.Width / 20.0 * 2) / 2 - 40);
float x0 = 0;
float y0 = -(float)radius;
switch (_numbers)
{
case Numbers.All:
for (int i = 1; i < 13; i++)
{
if (_ClokStyle == ClokStyle.Classic && i == 6)
{
continue;
}
if (i == 10 || i == 11)
{
y0 = -(float)(radius - 3);
}
var rx = x0 - 0;
var ry = y0 - 0;
var c = Math.Cos((30 * i) * Math.PI / 180);
var s = Math.Sin((30 * i) * Math.PI / 180);
float x1 = (float)(0 + rx * c - ry * s);
float y1 = (float)(0 + rx * s + ry * c);
g.Graphics.DrawString(i.ToString(), new Font("Times New Roman", 40, FontStyle.Bold), new SolidBrush(_textColor), x1, y1, stringFormat);
}
break;
}
#endregion
#region DrawSecHand
var angleSec = DateTime.Now.Second * 6;
switch (_ClokStyle)
{
case ClokStyle.Standart:
g.Graphics.ResetTransform();
g.Graphics.TranslateTransform(Ox, Oy);
g.Graphics.RotateTransform(angleSec);
g.Graphics.DrawLine(new Pen(_secHandColor), 0, 30, 0, -150);
g.Graphics.FillPolygon(new SolidBrush(_secHandColor), new Point[] { new Point(0, 0), new Point(3, -40), new Point(0, -150), new Point(-3, -40) });
break;
}
#endregion
#region DrawBigHands
g.Graphics.ResetTransform();
g.Graphics.TranslateTransform(Ox, Oy);
var angleMin = DateTime.Now.Minute * 6;
var angleHour = DateTime.Now.Hour * 30 + 30.0 / 100.0 * (DateTime.Now.Minute / (60.0 / 100.0));
switch (_handStyle)
{
case HandStyle.Sharp:
g.Graphics.FillEllipse(new SolidBrush(_handColor), -7, -7, 14, 14);
g.Graphics.RotateTransform(angleMin);
g.Graphics.FillPolygon(new SolidBrush(_handColor), new Point[] { new Point(0, 0), new Point(9, -30), new Point(0, -170), new Point(-9, -30) });
g.Graphics.ResetTransform();
g.Graphics.TranslateTransform(Ox, Oy);
g.Graphics.RotateTransform((float)angleHour);
g.Graphics.FillPolygon(new SolidBrush(_handColor), new Point[] { new Point(0, 0), new Point(9, -24), new Point(0, -130), new Point(-9, -24) });
break;
case HandStyle.Uniform:
g.Graphics.FillEllipse(new SolidBrush(_handColor), -7, -7, 14, 14);
g.Graphics.RotateTransform(angleMin);
g.Graphics.DrawLine(new Pen(_handColor, 4), 0, -120, 0, 15);
g.Graphics.ResetTransform();
g.Graphics.TranslateTransform(Ox, Oy);
g.Graphics.RotateTransform((float)angleHour);
g.Graphics.DrawLine(new Pen(_handColor, 8), 0, -100, 0, 15);
break;
}
g.Graphics.FillEllipse(new SolidBrush(_secHandColor), -4, -4, 8, 8);
#endregion
g.Render();
g.Dispose();
}
}