Re: String drawing gives me headache... advise very welcome



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
.



Relevant Pages

  • Re: API Class Question
    ... .flags = .flags Or CC_FULLOPEN ... >> (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As ... >> Call EnumWindows(AddressOf GetWindowTitles, VarPtr(tParms)) ... >> Dim sTitleText As String ...
    (microsoft.public.excel.programming)
  • Re: API Class Question
    ... .flags = .flags Or CC_FULLOPEN ... Private Declare Function EnumWindows Lib "user32" (ByVal lEnumFunc As ... (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) ... Dim sTitleText As String ...
    (microsoft.public.excel.programming)
  • Re: Mixing authentication type flags & By design Bug from MS ?
    ... public static DirectoryEntry SecureConnectDC(string adsPath, string usr, ... AuthenticationTypes AuthTypes; // Authentication flags ... a user has the rights to call ChangePassword on ...
    (microsoft.public.dotnet.security)
  • Re: FTP trasnferred as binary even with FTP_TRANSFER_TYPE_ASCII specified
    ... You aren't showing the code where you are calling Put and setting the flags. ... int accessType, ... string proxyName, ... static extern bool InternetCloseHandle; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)