将PaintEventArgs传递给方法

时间:2015-04-17 13:11:06

标签: c# .net winforms

我在C#中有以下方法:

private string adjustColumnValueLength(String value, int maxLength, PaintEventArgs e)
{
    // Set up our string font
    System.Drawing.Font printFontText = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);            
    SizeF stringSize = new SizeF();
    string newValue = value;

    // Loop until the string fits the size we need
    for (int x = value.Length; x >= 0; x--)
    {
        // Measure the string
        newValue = value.Substring(0, x);
        stringSize = e.Graphics.MeasureString(newValue, printFontText);
        Size roundedSize = Size.Round(stringSize);
        if (Int32.Parse(roundedSize.ToString()) <= maxLength)
        {
            return newValue;
        }
    }
    return newValue;
}

我从另一个方法中调用它来获取字符串的长度以匹配我必须显示的像素宽度,但是我不知道我应该如何传递PaintEventArgs。我尝试过使用System.Windows.Forms.PaintEventArgs,但这不起作用。

如何实现这一目标?

更新

private void btnPrintTicket_Click(object sender, EventArgs e)
    {
        // Loop over items in the listScanData object and create a ticket for printing showing
        // the Job #, In/Out, Part #, Name, Description and Qty
        line = 0;
        page = 1;
        xCoord = 50;
        yCoord = 180;
        printTicket = new System.Drawing.Printing.PrintDocument();
        printTicket.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.ticketData);
        PrintDialog printDialog = new PrintDialog();
        printDialog.Document = printTicket;
        DialogResult result = printDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            printTicket.Print();
        }
    }

    private void ticketData(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        // Insert code here to render the content we want to print
        System.Drawing.Font printFontHeading = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);
        System.Drawing.Font printFontSubheading = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular);
        System.Drawing.Font printFontFooter = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Italic);
        System.Drawing.Font printFontLabels = new System.Drawing.Font("Arial", 14, System.Drawing.FontStyle.Underline);
        System.Drawing.Font printFontText = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);

        // Draw our heading and subheading
        string printHeader = "Header 1";            
        string printSubHeader = "Subheader";
        e.Graphics.DrawString(printHeader, printFontHeading, System.Drawing.Brushes.Black, 50, 50);
        e.Graphics.DrawString(printSubHeader, printFontSubheading, System.Drawing.Brushes.Black, 50, 80);
        Pen pen = new Pen(Color.Gray);
        e.Graphics.DrawLine(pen, new Point(50, 110), new Point(800, 110));            

        // Draw our table headers
        string thType = "Type:"; string thName = "Name:"; string thNo = "No:"; string thQty = "Qty:"; string thBackorderQty = "Backorder Qty:";
        e.Graphics.DrawString(thType, printFontLabels, System.Drawing.Brushes.Black, 50, 150);            
        e.Graphics.DrawString(thName, printFontLabels, System.Drawing.Brushes.Black, 200, 150);
        e.Graphics.DrawString(thNo, printFontLabels, System.Drawing.Brushes.Black, 400, 150);
        e.Graphics.DrawString(thQty, printFontLabels, System.Drawing.Brushes.Black, 550, 150);
        e.Graphics.DrawString(thBackorderQty, printFontLabels, System.Drawing.Brushes.Black, 650, 150);

        int counter = 0;

        // Loop over our data to print it - incrementing our yCoord var by 30 each iteration (so the text doesn't overlap)
        for (; line < myGlobals.summaryData.Count; line++ )
        {
            // If our line var is greater than the data count, we have no more pages to print
            if (counter > 26)
            {
                // Draw our footer on the page
                xCoord = 350;
                yCoord = 1010;                    
                e.Graphics.DrawString("Page " + page, printFontFooter, System.Drawing.Brushes.Black, xCoord, yCoord);
                page++;

                // Reset our counter and our x/y coord
                counter = 0;
                xCoord = 50;
                yCoord = 180;                    

                // Indicate we have more to print and return
                e.HasMorePages = true;
                return;    
            }

            // Assign local vars with the data we need for this iteration
            string tdScanType = myGlobals.summaryData[line].ScanType.ToString();
            string tdScanName = myGlobals.getProductName(myGlobals.summaryData[line].Barcode.ToString());
            //if (tdScanName.Length > 25) { tdScanName = tdScanName.Substring(0, 25); }
            tdScanName = adjustColumnValueLength(tdScanName, 200);
            string tdScanPartNo = myGlobals.getProductNumber(myGlobals.summaryData[line].Barcode.ToString());
            //if (tdScanPartNo.Length > 15) { tdScanPartNo = tdScanPartNo.Substring(0, 15); }
            tdScanPartNo = adjustColumnValueLength(tdScanPartNo, 150);
            string tdScanQty = myGlobals.summaryData[line].Qty.ToString();
            string tdScanBackorderQty = myGlobals.summaryData[line].BackorderQty.ToString();

            // Draw the scan type
            e.Graphics.DrawString(tdScanType, printFontText, System.Drawing.Brushes.Black, xCoord, yCoord);

            // Increment our xCoord
            xCoord += 150;

            // Draw the scan name
            e.Graphics.DrawString(tdScanName, printFontText, System.Drawing.Brushes.Black, xCoord, yCoord);

            // Increment our xCoord
            xCoord += 200;

            // Draw the scan part no
            e.Graphics.DrawString(tdScanPartNo, printFontText, System.Drawing.Brushes.Black, xCoord, yCoord);                

            // Increment our xCoord
            xCoord += 150;

            // Draw the scan qty
            e.Graphics.DrawString(tdScanQty, printFontText, System.Drawing.Brushes.Black, xCoord, yCoord);

            // Increment our xCoord
            xCoord += 100;

            // Draw the scan backorder qty
            e.Graphics.DrawString(tdScanBackorderQty, printFontText, System.Drawing.Brushes.Black, xCoord, yCoord);

            // Reset our xCoord and increment our yCoord
            xCoord = 50;
            yCoord += 30;               

            // Increment our counter
            counter++;
        }

        // Draw our footer on the page
        xCoord = 350;
        yCoord = 1010;
        e.Graphics.DrawString("Page " + page, printFontFooter, System.Drawing.Brushes.Black, xCoord, yCoord);
        e.HasMorePages = false;            
    }

    private string adjustColumnValueLength(String value, int maxLength)
    {
        // Set up our string font
        System.Drawing.Font printFontText = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);
        Graphics g = this.CreateGraphics();
        SizeF stringSize = new SizeF();
        string newValue = value;

        // Loop until the string fits the size we need
        for (int x = value.Length; x >= 0; x--)
        {
            // Measure the string
            newValue = value.Substring(0, x);
            //stringSize = TextRenderer.MeasureText(newValue, printFontText);                
            stringSize = g.MeasureString(newValue, printFontText);
            Size roundedSize = Size.Round(stringSize);
            if (Int32.Parse(roundedSize.ToString()) <= maxLength)
            {
                return newValue;
            }
        }
        return newValue;
    }

上面提供的完整代码,我试图从ticketData方法中调用adjustColumnValueLength,这样我就可以将正在打印的值子串到我可用于该列的宽度。

1 个答案:

答案 0 :(得分:2)

您应该只从Paint事件处理程序调用该方法。我想你的代码中有一些东西:

private void Control_Paint(object sender, PainEventArgs e) { ... }

你应该从那里调用你的方法(而不是其他地方!):

private void Control_Paint(object sender, PainEventArgs e)
{
    adjustColumnValueLength("value", 10, e);
}

如果您没有,请创建一个事件处理程序:

this.Control.Paint += Control_Paint;

另请阅读:Custom Control Painting and Rendering