コード例 #1
0
        /// <summary>
        /// Print single line or multi lines TextBox.
        /// </summary>
        public void PrintTextBox(System.Windows.Forms.Control c,
            ParentControlPrinting typePrint,
            MultiPageManagement mp,
            Single x, Single y,
            ref Single extendedHeight, out bool ScanForChildControls)
        {
            ScanForChildControls = false;

            TextBox tb = (System.Windows.Forms.TextBox) c;
            Single h = mp.FontHeight(new Font(tb.Font.Name, tb.Font.Size));
            if (!tb.Multiline)
            {
                extendedHeight = mp.BeginPrintUnit(y, h);
                PrintText(c, mp, x, y, TextBoxBoxed, tb.Font.Bold, TextBoxBoxed, tb.TextAlign);
                mp.EndPrintUnit();
            }
            else  //multiline
            {
                // cut text into lines that fit in printed textBox width
                // Define an area of one line height and call MeasureString on it
                // This method return charactersFitted for the line
                ArrayList lines = new ArrayList();
                System.Drawing.Graphics g = mp.Graphics();
                SizeF areaForOneLineOfText = new SizeF();
                areaForOneLineOfText.Height = h;
                areaForOneLineOfText.Width = tb.Width;
                int charactersFitted;
                int linesFilled;
                int separatorCharPos;
                Font printFont = new Font(tb.Font.Name, tb.Font.Size);

                int pos = 0;
                do
                {
                    g.MeasureString(tb.Text.Substring(pos),
                        printFont,
                        areaForOneLineOfText,
                        new StringFormat(),
                        out charactersFitted,
                        out	linesFilled);
                    // this method with one line cut the last word....
                    // So, I have to go bak in the string to find a separator
                    // Happyly, MeasureString count separator character in charactersFitted
                    // For example, return "this is the first line " with space at the end
                    // even if there is not room for last space
                    separatorCharPos = charactersFitted;
                    if (charactersFitted < tb.Text.Substring(pos).Length) //le restant n'entre pas au complet sur la ligne
                    {
                        do
                        { separatorCharPos --; }
                        while ((separatorCharPos > pos) && !System.Char.IsSeparator(tb.Text, pos + separatorCharPos));
                        if (separatorCharPos == pos)   // no separator
                            separatorCharPos = charactersFitted;
                        else
                            separatorCharPos ++;
                    }
                    lines.Add(tb.Text.Substring(pos, separatorCharPos));
                    pos += separatorCharPos;
                }
                while ((pos < tb.Text.Length) && (charactersFitted > 0));

                // Print lines one by one
                Single yItem = y;
                Single extraHeight;
                Single extraHeightFirstLine = 0;	//Remenber if first line is pull down
                for (int i=0; i < lines.Count; i++)
                {
                    extraHeight = mp.BeginPrintUnit(yItem, h);   //Space required for tab page caption
                    if (i == 0)
                        extraHeightFirstLine = extraHeight;
                    mp.DrawString((string) lines[i], printFont, _Brush, x, yItem, tb.Width, h);
                    mp.EndPrintUnit();
                    yItem += h + extraHeight;
                }

                if ((yItem - y) > tb.Height)
                    extendedHeight = (yItem - y) - tb.Height;

                if (TextBoxBoxed)
                {
                    _Pen = new Pen(Color.Gray);
                    //Draw a rectangle arround list box. Start downer if first line pulled down
                    mp.DrawFrame(_Pen, x, y + extraHeightFirstLine, tb.Width, tb.Height + extendedHeight - extraHeightFirstLine);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Print a single line text for many controls. Do some formatting
        /// </summary>
        /// <param name="c">Control to print (must have these properties :font, text, width and height)</param>
        /// <param name="mp">Graphics object (printed page)</param>
        /// <param name="x">X position</param>
        /// <param name="y">Y position</param>
        /// <param name="tbBoxed">Draw a box arround text</param>
        /// <param name="inBold">use bold font</param>
        /// <param name="verticalCentering">Adjust vertical to obtain precise alignment from different type of control</param>
        /// <param name="hAlignment">Horizontal alignment of text in control print area</param>
        public void PrintText(System.Windows.Forms.Control c, 
            MultiPageManagement mp,
            Single x, Single y,
            bool tbBoxed, bool inBold, bool verticalCentering,
            HorizontalAlignment hAlignment)
        {
            Single yAdjusted = y;
            //Box
            if (tbBoxed)
                mp.DrawRectangle(_Pen, x, y, c.Width, c.Height);

            //Text
            Font printFont;
            if (inBold)
                printFont = new Font(c.Font.Name, c.Font.Size, FontStyle.Bold);
            else
                printFont = new Font(c.Font.Name, c.Font.Size);

            if (verticalCentering)
            {
                Single fontHeight = printFont.GetHeight(mp.Graphics());
                Single deltaHeight = (c.Height - fontHeight) / 2;
                yAdjusted += deltaHeight;
            }
            else
                yAdjusted += 2;

            mp.DrawString(c.Text, printFont, _Brush, x, yAdjusted, c.Width, c.Height, BuildFormat(hAlignment));
        }