BobFX 0 Light Poster

Hi,

I'm using a C++/CLI form, with a printDialog item:

  {
  public:
    Form1(void)
    {
      InitializeComponent();


      this->printDialog1 = (gcnew System::Windows::Forms::PrintDialog());    

I call the dialog with:

    printFont = gcnew System::Drawing::Font( "Courier New",10 );
    PrintDocument^ pd = gcnew PrintDocument;
    pd->PrintPage += gcnew PrintPageEventHandler( this, &Form1::PrintInstPrintPage);
    printDialog1->ShowHelp = false;
    printDialog1->Document = pd;
    System::Windows::Forms::DialogResult result = printDialog1->ShowDialog();

    if (result == ::DialogResult::OK)
     { first_call = true;
       pd->Print();
     }

I'm trying to get the number of availabler lines and columns with the PrintInstPrintPage() callback with:

void Form1::PrintInstPrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
{
      System::Drawing::SizeF sz = ev->Graphics->MeasureString("X", printFont, 100, StringFormat::GenericTypographic);
      colsPerPage =  ev->PageBounds.Width /  sz.Height;

      PageHeight = ev->MarginBounds.Height - 2*printFont->GetHeight( ev->Graphics );
      linesPerPage = PageHeight / printFont->GetHeight( ev->Graphics );

Unfortunately, ev->Graphics->MeasureString() appears to return wrong values, giving for example on 43 columns while 85 can fit in the page.

I tested ev->Graphics->MeasureString() with these 2 variants of the program found at http://msdn.microsoft.com/en-us/library/system.drawing.stringformatflags.aspx

Variant one:

      String^ measureString = "Measure String";

      // Set maximum width of string.
      int stringWidth = 100;

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      SizeF stringSize = ev->Graphics->MeasureString( measureString, printFont, stringWidth, newStringFormat );

      // Draw rectangle representing size of string.
      ev->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      ev->Graphics->DrawString( measureString, printFont, Brushes::Black, PointF(0,0), newStringFormat );

Variant two:

      String^ measureString = "Measure String";

      // Set maximum width of string.
      int stringWidth = 100;

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      //newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      SizeF stringSize = ev->Graphics->MeasureString( measureString, printFont, stringWidth, newStringFormat);

      // Draw rectangle representing size of string.
      ev->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      ev->Graphics->DrawString( measureString, printFont, Brushes::Black, PointF(0,0), newStringFormat );

When the drawing is vertical (newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical) MeasureString returns correct values, the box is indeed drawed just around the displayed string.

When the drawing is horizontal, the box does not bound the string, showing that MeasureString() retun is spurious.

Any idea how to get correct width values?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.