Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:14:17+00:00 2026-06-13T05:14:17+00:00

I am trying to use the SetTextJustification function, but it does not work as

  • 0

I am trying to use the SetTextJustification function, but it does not work as expected.

If I set the argument value of nBreakExtra to 40 or to 10 the output is the same, why is this?

Here is my code:

      private void button1_Click(object sender, EventArgs e)
      {
          IntPtr hdc = richTextBox1.CreateGraphics().GetHdc();
          string str = "aaa bbb ccc ddd eee fff";
          SetTextJustification(hdc, 40, 5);
          TextOut(hdc, 20, 20, str, str.Length);
          SetTextJustification(hdc, 10, 5);
          TextOut(hdc, 20, 50, str, str.Length);
      }

      [DllImport("gdi32.dll")]
      static extern bool SetTextJustification(IntPtr hdc, int nBreakExtra, int nBreakCount);

      [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
      static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart, string lpString, int cbString);

the output is displayed as:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:14:18+00:00Added an answer on June 13, 2026 at 5:14 am

    Your code seems to work ok. Here is a screen shot of my test:

    Test form showing the results of the code

    I am not sure why your results are different.

    Perhaps I could offer this as an alternative solution:
    I have writen my own justification method:

    public void PaintTextJustification(Graphics g, string text, Font font, PointF location, int lineWidth, bool applyToLastLine)
    {
      string[] words = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
      int wordCount = 0;
      float locY = location.Y;
    
      while (wordCount < words.Length)
      {
        StringBuilder rawLine = new StringBuilder();
        List<string> lineParts = new List<string>();
    
        while ((wordCount < words.Length) && (g.MeasureString(rawLine.ToString() + words[wordCount], font).Width < (float)lineWidth))
        {
          rawLine.Append(words[wordCount] + " ");
          lineParts.Add(words[wordCount] + " ");
          wordCount++;
        }
        string rawLineStr = rawLine.ToString().Trim();
    
        float padding = 0;
        if ((wordCount < words.Length) || (applyToLastLine))
        {
          // Only apply padding if not the last line.
          padding = ((float)lineWidth - g.MeasureString(rawLineStr, font).Width) / (lineParts.Count - 1);
        }
    
        float locX = location.X;
        foreach (string word in lineParts)
        {
          g.DrawString(word, font, Brushes.Black, new PointF(locX, locY));
          locX += g.MeasureString(word, font).Width + padding;
        }
    
        locY += g.MeasureString(rawLineStr, font).Height;
      }
    }
    

    Using this new method lets you choose the font and specify the total length of the line, it also allows you greater flexibility and customization (e.g. incoporating a flag that indicates if the last line should be justified or not). You could also customize the method further by including the font color as a method argument.

    This method can now be used as shown in the button event method below (note the first bit of this event handler method includes the code for testing the origonal solution):

    private void EditButton_Click(object sender, EventArgs e)
    {
      IntPtr hdc = richTextBox1.CreateGraphics().GetHdc();
      string str = "aaa bbb ccc ddd eee fff";
    
      SetTextJustification(hdc, 40, 5);
      TextOut(hdc, 20, 20, str, str.Length);
    
      SetTextJustification(hdc, 10, 5);
      TextOut(hdc, 20, 40, str, str.Length);
    
      // Another Approach:
      Graphics g = richTextBox1.CreateGraphics();
      PaintTextJustification(g, str, richTextBox1.Font, new PointF(20f, 90f), 220, true);
    
      System.Drawing.Font newFont = new Font("Arial", 12f, FontStyle.Bold);
      string longStr = "This is a very long string which will need to be split across several lines when it is justified.";
      PaintTextJustification(g, longStr, newFont, new PointF(20f, 110f), 220, false);
    }
    

    Here is a screen shot of the results that show both approaches:

    enter image description here

    Anyway I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use cursor for pagination. Forwarding pagination work fine, but I can
I'm trying use double type in openCL, but doesn't work anyway, i want use
Trying to use Java's regexp I want to match /app, /app/.* , but not
I was trying use a set of filter functions to run the appropriate routine,
I have been trying use the edit_post_link() function to contain an image. All of
I am trying use filehelpers class builder but I am kinda confused on what
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I was trying use svn to find a checkin using svn log, but it
I am trying use std::copy to copy from two different iterator. But during course

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.