CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Width of a tab sequence

    The size of a string in a certain font and font size can, for instance, be measured with an event handler like this little hack:

    Code:
        System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
        {
          Graphics ^gr = Graphics::FromHwnd(textBox1->Handle);
          gr->PageUnit = GraphicsUnit::Point;
          lblSize->Text = String::Concat("Text size: ", (gr->MeasureString(textBox1->Text, textBox1->Font)).ToString());
        }
    Well, "little" doesn't really mean "quick" here, as I had to scratch together the objects involved from all corners of the CTS. The Handle property of the text box, for instance, is not found in the docs on the TextBox class itself. It is rather inherited indirectly from the IWin32Window implementation in System::Windows::Forms::Control. Note that the Graphics object here is not used to actually draw something, but merely for the size calculation.

    If you are interested in more elaborate information about this, feel free to ask. I was already thinking of writing a little text size calculator anyway and might post it here if you're interested. This would take some time to write, though.

    The text size specification in the attached image has been generated by the code posted above, for instance. Note that the height specification is not equal to the font size because it accounts the entire line spacing rather than just the height of a norm character.

    The width of a single digit in MS Sans Serif 8.25, BTW, is 7.47522 pt, measured using the same method. But the width measured for 8 digits is 40.15576 pt, which obviously is not 8 times the value for the single digit. Maybe I should investigate some more about how that measurement actually works...
    Attached Images Attached Images
    Last edited by Eri523; October 18th, 2010 at 09:40 PM. Reason: Corrected where the text box' Handle property actually is derived from
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured