Home>

We are currently developing a cash register system in C #/VisualStudio.
I would like to print a receipt, but if I print the current status, the store's logo (image file) and the product/money information will overlap.
Ideally
Logo
Product information (accounting)
I want to display in the order of.
The code looks like this

private void pd_Print_img (object sender, PrintPageEventArgs e)
        {
            // Load image
            Image img = Properties.Resources.logo_receipt;
            // Draw an image
            e.Graphics.DrawImage (img, 0, 0, 270, 90);
            // Notify that there is no next page
            e.HasMorePages = false;
            // Clean up
            img.Dispose ();
            printingPosition = 0;
        }
private void pd_Print_Info (object sender, PrintPageEventArgs e)
        {
            string pText = "";
            if (printingPosition == 0)
            {
                // Unify line breaks to '\ n'
                pText = HeadText.Replace ("\ r \ n", "\ n");
                pText = HeadText.Replace ("\ r", "\ n");
            }
            int bwidth = e.MarginBounds.Width;
            // Loop until it doesn't fit on one page or there are no more characters to print
            while (e.MarginBounds.Bottom>y + printFont.Height&&
                printingPosition<pText.Length)
            {
                string line = "";
                for (;;)
                {
                    // If there are no more characters to print,
                    // Print out of the loop at line break
                    if (printingPosition>= pText.Length ||
                        pText [printingPosition] == '\ n')
                    {
                        if (e.Graphics.MeasureString (line, printFont) .Width<width)
                        {
                            int l = 0;
                            double p = (width-e.Graphics.MeasureString (line, printFont) .Width)/2;
                            int xp = (int) (Math.Floor (p)/e.Graphics.MeasureString ("", printFont) .Width);
                            while (l<xp)
                            {
                                line = line.Insert (0, "");
                                l ++;
                            }
                        }
                        printingPosition ++;
                        break;
                    }
                    // Add one character and check if it exceeds the print width
                    line + = pText [printingPosition];
                    if (e.Graphics.MeasureString (line, printFont) .Width
                        >width)
                    {
                        // wrapping over the print width
                        line = line.Substring (0, line.Length-1);
                        break;
                    }
                    // Next print character position
                    printingPosition ++;
                }
                // write a line
                e.Graphics.DrawString (line, printFont, Brushes.Black, x, y);
                // Calculate the print position of the next line
                y + = (int) printFont.GetHeight (e.Graphics);
            }
            e.HasMorePages = true;
            printingPosition = 0;
            y + = (int) printFont.GetHeight (e.Graphics);
        }
  • Answer # 1

    You may be interested in

      

    pText = HeadText.Replace ("\ r \ n", "\ n");
      pText = HeadText.Replace ("\ r", "\ n");

    The previous line is meaningless. Is this all right?

  • Answer # 2

    Isn't it because y was not carried down by pd_Print_img?

  • Answer # 3

    What is the execution order of pd_print_info and pd_print_img? Both seem to be event handlers, but they are executed in the order in which they are added.
    The printingPosition is set to 0 at the end of pd_print_img, but this is at least the height of the image, maybe.

    A program is a command that instructs the computer to operate. If you are not expecting it, first make sure that you are giving instructions as expected.