C# 设置TextFieldRect.Width的大小
您好,我们的.Net开发人员已经离任了,我正在尝试破译使用的语法。我发现问题在于C# 设置TextFieldRect.Width的大小,c#,printing,rectangles,drawrectangle,C#,Printing,Rectangles,Drawrectangle,您好,我们的.Net开发人员已经离任了,我正在尝试破译使用的语法。我发现问题在于TextSize.Width=104.7736和TextFieldRect.Width=100,因此当它在DrawLeftTextField方法中点击if()语句时,会抛出错误。我的问题是,如何将textfieldcrect.Width更改为大于100?下面是语法,如果需要进一步解释,请让我知道,我会尽我所能进一步解释 编辑---我在这里要做的是在打印页面顶部写下文本和文本框的值 protected override
TextSize.Width
=104.7736和TextFieldRect.Width
=100,因此当它在DrawLeftTextField
方法中点击if()
语句时,会抛出错误。我的问题是,如何将textfieldcrect.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);
}
PrintPage上受保护的覆盖无效(PrintPageEventArgs e)
{
//运行基本代码
基于打印页(e);
//声明所需的局部变量
int printHeight=0;
int printWidth=0;
int rightMargin=0;
PaperSize ps=默认值(PaperSize);
对于(int ix=0;ix textfieldcrect.Width)| |(TextSize.Height>textfieldcrect.Height))
{
Assert(false);
返回;
}
e、 Graphics.DrawString(strText、普通字体、新SolidBrush(颜色为黑色)、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
}
PrintPage上受保护的覆盖无效(PrintPageEventArgs e)
{
//运行基本代码
基于打印页(e);
//声明所需的局部变量
int printHeight=0;
int printWidth=0;
int rightMargin=0;
PaperSize ps=默认值(PaperSize);
对于(intix=0;ix好的,除了语法之外,我想说的第一个问题是找出这段代码的“什么”和“为什么”
它在做什么:
- 它得到了一个要打印的字符串
- 它得到了一个想要打印的矩形
- 它要确保要打印的字符串适合该字符串
长方形
…好吧,那为什么会有这样的错误呢
if ((TextSize.Width > TextFieldRect.Width) || (TextSize.Height > TextFieldRect.Height))
{
Debug.Assert(false);
return;
}
…这就是他们检查的地方,以确保矩形足够大,适合字符串
那么你有什么选择
如果传入的内容太长,请更改代码,而不是
错误,它只是缩短了它试图打印的内容
找出矩形变小时调用函数的原因
调整文本,并对其进行更改,使其具有更大的矩形或矩形
细绳
看看是否有什么东西缩小了矩形的大小
最近(如打印机页边距/大小更改)
希望这能有所帮助。我正在将数据从用户表单上的文本框传递到矩形,这样大小可能会有所不同。我是否会更好,忽略所有矩形,只是将数据堆叠在打印页面的顶部?不。我肯定不会盲目走这条路。以你的角度来看,我不会这样做hr通过代码找出是什么'strText'产生了错误。请记住,只有当有人试图以太小的矩形打印某个内容时才会发生错误(如果我读对了,这只是页面的全宽)打印的行可能太长了,错误是合法的:它不能放在页面中。一旦你知道是什么字符串/行文本产生了错误,你就可以知道你想如何处理这个问题。我一直在逐步检查代码,这就是我如何隔离实际生成错误的行的方法错误。我不确定如何扩展矩形的宽度,但将尝试继续单步执行并跟踪,以隔离该宽度的设置位置。