Welcome to Dream.In.Code
Become a C# Expert!

Join 150,399 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 960 people online right now. Registration is fast and FREE... Join Now!




Drawing string problem

 
Reply to this topicStart new topic

Drawing string problem

cygnusX
4 Mar, 2008 - 08:36 AM
Post #1

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
I'm trying to write small text editor.The problem is that when i draw certain symbols like "i" for example,the space between "i" and the next symbol is larger than the needed space(which is virtually nothing,i mean there is no space between symbols at all).There must be some mechanism to measure the width of a symbol and prevent the problem that i've already mentioned.Can someone help me with that?

CODE

int pos = 0;

private void page_KeyUp(object sender, KeyEventArgs e)
{
           Graphics g = page.CreateGraphics();

            switch (e.KeyCode)
            {
               case Keys.A:
                   pos.X += 8;
                    break;
                case Keys.B:
                    g.DrawString("b", f, s, pos);
                    pos.X += 8;
                    break;
                    ///and so on
            }
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Drawing String Problem
4 Mar, 2008 - 09:01 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You can use Graphics.MeasureString() to measure the width of the character. Create a new StringFormat, passing it the then use DrawString, passing it the String.Format.GenericTypographic Property. You then pass DrawString the Font, ,Brush,Location and StringFormat, something like:


csharp

//This is our string to measure
string str = "Some String";
//create a new font
Font font = new Font("Arial", 12);
PointF location = new PointF(10, 10);
//create a starting point
PointF origin = new PointF(0, 0);
//set up our StringFormat Method
StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic);
//measure our string
SizeF textArea = Graphics.MeasureString(str, font, origin, sFormat);
//draw our string
Graphics.DrawString(str, font, System.Drawing.Brushes.Black, location,sFormat);



That should get you started down the right path smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month