设置TextFieldRect.Width的大小

时间:2017-08-29 13:33:14

标签: c# printing rectangles drawrectangle

美好的一天,我们的.Net开发人员飞过了鸡舍,我试图破译所使用的语法。我发现问题是TextSize.Width = 104.7736和TextFieldRect.Width = 100所以当它遇到if()方法中的DrawLeftTextField语句时会抛出错误。我的问题是,如何将TextFieldRect.Width更改为超过100的帐户?以下是语法,如果需要进一步说明,请告诉我,我会尽力进一步解释。

编辑---我在这里的目的是在打印页面的顶部写入文本和文本框的值。

protected override void OnPrintPage(PrintPageEventArgs e)
{
    // Run base code
    base.OnPrintPage(e);

    //Declare local variables needed
    int printHeight = 0;
    int printWidth = 0;
    int rightMargin = 0;

    PaperSize ps = default(PaperSize);
    for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++)
    {
        if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4)
        {
            ps = PrinterSettings.PaperSizes[ix];
            DefaultPageSettings.PaperSize = ps;
            break;
        }
    }

    DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN;
    DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN;
    DefaultPageSettings.Landscape = true;

    var CurrentPageSettings = base.DefaultPageSettings;
    printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom;
    printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right;
    m_leftMargin = CurrentPageSettings.Margins.Left; //X
    rightMargin = CurrentPageSettings.Margins.Top; //Y

    //Create a rectangle printing are for our document
    m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight);

    // Get Normal Row Height
    int charactersFitted = 0;
    int linesFilled = 0;
    SizeF TextSize = new SizeF();
    StringFormat textFormat = new StringFormat();
    //Tell it to Alignment Text in its rectangle 
    textFormat.Alignment = StringAlignment.Near;
    textFormat.FormatFlags = StringFormatFlags.NoClip;
    TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
    m_NormalRowHeight = (int)TextSize.Height + 3;  

    DrawTextValuePair(e, "Employee Name: ", Control.maintenance.txtEmployee.Text, true, m_leftMargin);

    //More here
}

void DrawTextValuePair(PrintPageEventArgs e, string strText, string strValue, bool bDrawLine, int iLeftMargin)
{
    m_iCurrentLocationY += m_NormalRowHeight;
    Point LeftTextFieldLocation = new Point(iLeftMargin, m_iCurrentLocationY);
    DrawLeftTextField(e, strText, new Rectangle(LeftTextFieldLocation, m_TextValuePairSize), StringAlignment.Near);

    Point ValueFieldLocation = new Point(iLeftMargin + m_TextValuePairSize.Width, m_iCurrentLocationY);
    DrawValueField(e, strValue, new Rectangle(ValueFieldLocation, m_TextValuePairSize), StringAlignment.Far);

    if (bDrawLine)
    {
    int iRightEnd = iLeftMargin + (2 * m_TextValuePairSize.Width);
    int iYLocation = m_iCurrentLocationY + m_NormalRowHeight;
    e.Graphics.DrawLine(new Pen(Color.DarkBlue, 2), iLeftMargin, iYLocation, iRightEnd, iYLocation);
    }
}

void DrawLeftTextField(PrintPageEventArgs e, string strText, Rectangle TextFieldRect, StringAlignment TextAligment)
{
    int charactersFitted = 0;
    int linesFilled = 0;
    SizeF TextSize = new SizeF();
    StringFormat textFormat = new StringFormat();
    //Tell it to Alignment Text in its rectangle 
    textFormat.Alignment = TextAligment;
    textFormat.LineAlignment = StringAlignment.Center;
    textFormat.FormatFlags = StringFormatFlags.NoClip;
    TextSize = e.Graphics.MeasureString(strText, NORMAL_TEXT_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
    if ((TextSize.Width > TextFieldRect.Width) || (TextSize.Height > TextFieldRect.Height))
    {
    Debug.Assert(false);
    return;
    }

    e.Graphics.DrawString(strText, NORMAL_TEXT_FONT, new SolidBrush(Color.Black), TextFieldRect, textFormat);

}

修改
我已经硬编码了一个长度,现在不会抛出错误并使用下面的语法,但是我要显示的文本只是垃圾,请看截图

protected override void OnPrintPage(PrintPageEventArgs e)
{
// Run base code
base.OnPrintPage(e);

//Declare local variables needed
int printHeight = 0;
int printWidth = 0;
int rightMargin = 0;

PaperSize ps = default(PaperSize);
for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++)
{
    if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4)
    {
        ps = PrinterSettings.PaperSizes[ix];
        DefaultPageSettings.PaperSize = ps;
        break;
    }
}

DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN;
DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN;
DefaultPageSettings.Landscape = true;

var CurrentPageSettings = base.DefaultPageSettings;
printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom;
printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right;
m_leftMargin = CurrentPageSettings.Margins.Left; //X
rightMargin = CurrentPageSettings.Margins.Top; //Y

//Create a rectangle printing are for our document
m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight);

// Get Normal Row Height
int charactersFitted = 0;
int linesFilled = 0;
SizeF TextSize = new SizeF();
StringFormat textFormat = new StringFormat();
//Tell it to Alignment Text in its rectangle 
textFormat.Alignment = StringAlignment.Near;
textFormat.FormatFlags = StringFormatFlags.NoClip;
TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
m_NormalRowHeight = (int)TextSize.Height + 3; //Row is bigger than text 

// draw logo
Image logoImage = global::SoilProfile.Properties.Resources.SoilProfileIcon.ToBitmap();
// get the size of the image
Rectangle LogoRect = new Rectangle(m_leftMargin, m_leftMargin, (int)(logoImage.Width * 0.75), (int)(logoImage.Height * 0.8));
e.Graphics.DrawImage(logoImage, LogoRect);
//e.Graphics.DrawRectangle(Pens.LightBlue, LogoRect);

//Draw First Heading
Rectangle MainHeaderRect = new Rectangle();
DrawMainHeading(e, LogoRect, ref MainHeaderRect);

m_iCurrentLocationY = MainHeaderRect.Bottom + (m_NormalRowHeight);

// Document Name
textFormat.Alignment = StringAlignment.Near;
Rectangle TextRect = new Rectangle(m_leftMargin, m_iCurrentLocationY, m_SubHeaderTextFieldSize.Width, m_SubHeaderTextFieldSize.Height);
e.Graphics.DrawString("Document Name: " + m_PrintData.DocumentName, SUB_HEADING_FONT, new SolidBrush(Color.Black), TextRect, textFormat);

int LeftSubHeadingWidth = 200;
m_SubHeaderTextFieldSize = new Size(LeftSubHeadingWidth, m_NormalRowHeight);

m_TextValuePairSize = new Size(m_SubHeaderTextFieldSize.Width / 30, m_SubHeaderTextFieldSize.Height);
DrawTextValuePair(e, "Name: ", Control.maintenance.txtEmployee.Text, true, m_leftMargin);
DrawTextValuePair(e, "Phone #: ", Control.maintenance.txtPhone.Text, true, m_leftMargin);
int iCurrentLocationX = m_leftMargin;
int iAlphaStart_Y = m_iCurrentLocationY;
iCurrentLocationX += m_SubHeaderTextFieldSize.Width + 30;
DrawTextValuePair(e, "Hire Date: ", DControl.maintenance.dtpDate.Text, true, iCurrentLocationX);
DrawTextValuePair(e, "Manager: ", Control.maintenance.txtManager.Text, true, iCurrentLocationX);
DrawTextValuePair(e, "District: ", Control.maintenance.txtDistrict.Text, true, iCurrentLocationX);

//More code here
}

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,除了语法之外,我要说的第一个问题是弄清楚这段代码的“内容”和“原因”。

它正在做什么:

  • 它正在获取要打印的字符串
  • 它想要在
  • 打印一个矩形
  • 确保要打印的字符串符合要求 矩形

...好吧,为什么那里出现错误?

if ((TextSize.Width > TextFieldRect.Width) || (TextSize.Height > TextFieldRect.Height))
{
    Debug.Assert(false);
    return;
}

......他们正在检查以确保矩形足够大以适应字符串。

那你有什么选择?

  1. 更改代码,如果传入的内容太长,则代替 错误,它只是缩短了它试图打印的内容
  2. 找出用矩形调用函数的函数 适合文本,并更改​​它,使其具有更大的矩形或 较小的字符串
  3. 看看是否有减少矩形大小的东西 最近(例如打印机边距/尺寸变化。
  4. 希望有所帮助。