Re: String drawing gives me headache... advise very welcome
- From: "Bob Powell [MVP]" <bob@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 06 Mar 2008 18:34:45 +0100
Just a tip.. You may want to clone a format such as GenericTypographic rather than create a new one. Otherwise I can't see what is "wrong" with your output.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Fre wrote:
Hi all,.
I'm working on this for some time now, but it keeps giving me
headaches. I'm drawing strings in a label and in a
DataGridViewTextBoxCell. Why? Subparts of the string need other
colors. It's working, but not as should be. I'm not able to fully
understand how drawing works with TextFormatFlags and StringFormat.
I've the feeling I tried all possible combinations of flag settings,
but it's not working as should be (which is well formatted strings and
backgrounds, well formed spacing...). This is a complete example that
draws a string in a label (to test, create a project with a Form
(Form1) and a Label (label1). As you can see after testing the code,
the result is buggy. How does MS draws the strings?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestDrawing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new Size(600, 80);
label1.Location = new Point(5, 5);
label1.Size = new Size(585, 21);
}
private void label1_Paint(object sender, PaintEventArgs e)
{
Rectangle rec = e.ClipRectangle;
Graphics g = e.Graphics;
Font f = new Font("Courier New", 10, FontStyle.Regular);
string[] writeThis = { "This drawing ",
"gives",
" me ",
"a",
" serious ",
"headache",
"! Why",
" isn't it ",
"w",
"orkin",
"g?"
};
// text format flags
TextFormatFlags flags = TextFormatFlags.Left;
flags |= TextFormatFlags.NoPadding;
flags |= TextFormatFlags.PreserveGraphicsClipping;
flags |= TextFormatFlags.NoPrefix;
flags |= TextFormatFlags.GlyphOverhangPadding;
// more text layout
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.None;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
sf.FormatFlags |= StringFormatFlags.NoWrap;
sf.FormatFlags |= StringFormatFlags.NoFontFallback;
bool b = false;
Size propsize = new Size(int.MaxValue, int.MaxValue);
foreach (string s in writeThis)
{
Size size = TextRenderer.MeasureText(g, s, f, propsize,
flags);
rec.Width = size.Width;
// fill rectangle and draw string (toggle colors)
b = !b;
g.FillRectangle(b ? Brushes.Indigo : Brushes.LightGreen, rec);
g.DrawString(s, f, b ? Brushes.Wheat : Brushes.Black, rec,
sf);
// update the start position of our rectangle
rec.X = rec.X + size.Width;
}
}
}
}
Thanks in advance for your feedback.
Frederik
- Prev by Date: Re: Graphics.TextRenderingHint throw exception?
- Next by Date: Re: Graphics.TextRenderingHint throw exception?
- Previous by thread: Re: Loss of precision when drawing gridlines and blocks on top.....
- Next by thread: on-screen overlay connections : looking for the medium-priced solution
- Index(es):
Relevant Pages
|