将PaintEventArgs传递给方法 [英] Pass PaintEventArgs into method

查看:96
本文介绍了将PaintEventArgs传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中具有以下方法:

I have the following method in 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 ,但这不起作用。

I am calling this from within another method to get the length of a string to match the width in pixels I have to display it, however I have NO idea how I am supposed to pass in the PaintEventArgs. I've tried using System.Windows.Forms.PaintEventArgs but that does not work.

这怎么可能

更新

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;
    }

上面提供的完整代码,我试图从内部调用adjustColumnValueLength ticketData方法,这样我就可以将要打印的值子字符串化为仅该列可用的宽度。

Complete code provided above, I'm trying to call the adjustColumnValueLength from within the ticketData method so that I can substring the value that is being printed to only the width I have available for that column.

推荐答案

您应该只从 Paint 事件处理程序调用该方法。我想您的代码中有一些内容,例如:

You should only call that method from a Paint event handler. I guess you have something in your code like:

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

您应该从那里调用方法(无处可!):

You should call your method from there (and nowhere else!):

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

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

If you don't have that, create a event handler:

this.Control.Paint += Control_Paint;

也请阅读:自定义控件绘画和渲染

这篇关于将PaintEventArgs传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆