Sprache: C#
Die untenstehende Methode zeichnet einfach einen Text-Kasten.
[u]Parameter:[/u]
[b]sText[/b]: Der Text
[b]fText[/b]: Die Schriftart
[b]cText[/b]: Die Textfarbe
[b]cBox[/b]: Die Hintergrundfarbe der Box
[b]cBorder[/b]: Die Farbe des Randes
[b]iOuterBorderWidth[/b]: Die Breite des Randes in Pixel
[b]iInnerBorderWidth[/b]: Der Abstand zwischen Text und äußerem Rand in Pixel
[b]pLocation[/b] Der Punkt, an dem die Box gezeichnet werden soll (obere linke Ecke)
[b]g[/b]: Die Graphics, auf die die Box gezeichnet wird.
private void DrawStringBox(string sText, Font fText, Color cText, Color cBox, Color cBorder, int iOuterBorderWidth, int iInnerBorderWidth, Point pLocation, Graphics g)
{
Point pBorder = pLocation;
Point pBox = pLocation;
Point pText = pLocation;
pBox.Offset(iOuterBorderWidth, iOuterBorderWidth);
pText.Offset(iOuterBorderWidth + iInnerBorderWidth, iOuterBorderWidth + iInnerBorderWidth);
Size szText = TextRenderer.MeasureText(sText, fText);
Size szBox = szText;
Size szBorder = szText;
szBox.Width += (2 * iInnerBorderWidth);
szBox.Height += (2 * iInnerBorderWidth);
szBorder.Width += (2 * iInnerBorderWidth) + (2 * iOuterBorderWidth);
szBorder.Height += (2 * iInnerBorderWidth) + (2 * iOuterBorderWidth);
g.FillRectangle(new SolidBrush(cBorder), new Rectangle(pBorder, szBorder));
g.FillRectangle(new SolidBrush(cBox), new Rectangle(pBox, szBox));
g.DrawString(sText, fText, new SolidBrush(cText), pText);
}
private void DrawStringBox(string sText, Font fText, Color cText, Color cBox, Color cBorder, int iOuterBorderWidth, int iInnerBorderWidth, Point pLocation, Graphics g)
{
Point pBorder = pLocation;
Point pBox = pLocation;
Point pText = pLocation;
pBox.Offset(iOuterBorderWidth, iOuterBorderWidth);
pText.Offset(iOuterBorderWidth + iInnerBorderWidth, iOuterBorderWidth + iInnerBorderWidth);
Size szText = TextRenderer.MeasureText(sText, fText);
Size szBox = szText;
Size szBorder = szText;
szBox.Width += (2 * iInnerBorderWidth);
szBox.Height += (2 * iInnerBorderWidth);
szBorder.Width += (2 * iInnerBorderWidth) + (2 * iOuterBorderWidth);
szBorder.Height += (2 * iInnerBorderWidth) + (2 * iOuterBorderWidth);
g.FillRectangle(new SolidBrush(cBorder), new Rectangle(pBorder, szBorder));
g.FillRectangle(new SolidBrush(cBox), new Rectangle(pBox, szBox));
g.DrawString(sText, fText, new SolidBrush(cText), pText);
}
Alte URL:
/snippet/einen-text-kasten-zeichnen/3834