/// <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)); }
/// <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); } } }
public void PrintReportTitle(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = false; Font printFont = new Font(c.Font.Name, (Single) (c.Font.Size * 1.3), FontStyle.Bold); float fontHeight = mp.FontHeight(printFont); Pen pen = new Pen(Color.Black, 2); extendedHeight = fontHeight + 3 + pen.Width + 1; mp.BeginPrintUnit(y, extendedHeight); mp.DrawString(c.Tag.ToString(), printFont, Brushes.Black, x, y, c.Width, fontHeight); y += fontHeight + 3; mp.DrawLines(pen, x, y, x + c.Size.Width, y); mp.EndPrintUnit(); }
public void PrintTabControl(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = true; System.Windows.Forms.TabControl tc = (TabControl) c; _Pen = new Pen(Color.Gray); switch (typePrint) { case ParentControlPrinting.BeforeChilds : //Nom du TabPage Single extraHeight = mp.BeginPrintUnit(y, tc.ItemSize.Height); //Space required for tab page caption System.Windows.Forms.TabPage tp = tc.SelectedTab; Font printFont = new Font(tp.Font.Name, tp.Font.Size, FontStyle.Bold); Single h = mp.FontHeight(printFont); if (h > tc.ItemSize.Height) h = tc.ItemSize.Height; mp.DrawString(tp.Text, printFont, Brushes.Black, x, y + (tc.ItemSize.Height - h) / 2, tp.Width, h); mp.DrawLines(_Pen, x, y + tc.ItemSize.Height, x + tc.Width, y + tc.ItemSize.Height); mp.EndPrintUnit(); extendedHeight += extraHeight; break; case ParentControlPrinting.AfterChilds : if (TabControlBoxed) mp.DrawFrame(_Pen, x, y, c.Width, c.Height + extendedHeight); break; } }
public void PrintListBox(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = false; ListBox lb = (ListBox) c; Single yItem = y; Single extraHeight; Single extraHeightFirstLine = 0; //Remenber if first line is pull down Font printFont = new Font(lb.Font.Name, lb.Font.Size, FontStyle.Bold); int oldPos = lb.SelectedIndex; //save position for (int i = 0; i < lb.Items.Count; i++) { extraHeight = mp.BeginPrintUnit(yItem, lb.ItemHeight); //Space required for tab page caption if (i == 0) extraHeightFirstLine = extraHeight; lb.SelectedIndex = i; // set position to obtain Text of current item mp.DrawString(lb.Text, printFont, _Brush, x, yItem, lb.Width, lb.ItemHeight); mp.EndPrintUnit(); yItem += lb.ItemHeight + extraHeight; } lb.SelectedIndex = oldPos; //restore position if ((yItem - y) > lb.Height) extendedHeight = (yItem - y) - lb.Height; _Pen = new Pen(Color.Gray); //Draw a rectangle arround list box. Start downer if first line pulled down mp.DrawFrame(_Pen, x, y + extraHeightFirstLine, c.Width, c.Height + extendedHeight - extraHeightFirstLine); }
/*public void PrintPanel(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = true; if (typePrint == ParentControlPrinting.AfterChilds) if ( ((System.Windows.Forms.Panel) c).BorderStyle != BorderStyle.None) { if (((System.Windows.Forms.Panel) c).BorderStyle == BorderStyle.Fixed3D) _Pen = new Pen(Color.Silver); // if height less than 10, just print an horizontal line if ((c.Height < 10) && (c.Controls.Count == 0)) { extendedHeight += mp.BeginPrintUnit(y, 1); mp.DrawLines(_Pen, x, y, x + c.Width, y); mp.EndPrintUnit(); } else mp.DrawFrame(_Pen, x, y, c.Width, c.Height + extendedHeight); } }*/ public void PrintGroupBox(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = true; Font printFont = new Font(c.Font.Name, c.Font.Size); Single h = mp.FontHeight(printFont); _Pen = new Pen(Color.Silver); switch (typePrint) { case ParentControlPrinting.BeforeChilds : // if height less than 10, just print an horizontal line if ((c.Height < 10) && (c.Controls.Count == 0)) { extendedHeight += mp.BeginPrintUnit(y, 1); mp.DrawLines(_Pen, x, y, x + c.Width, y); mp.EndPrintUnit(); } else { Single extraHeight = mp.BeginPrintUnit(y, h); //Space required for group caption //PrintText(c, mp, x + h, y, false, true, false, HorizontalAlignment.Left); mp.DrawString(c.Text, printFont, Brushes.Black, x + h, y, c.Width - h - h, h); mp.EndPrintUnit(); extendedHeight += extraHeight; }; break; case ParentControlPrinting.AfterChilds : mp.DrawFrame(_Pen, x, y, c.Width, c.Height + extendedHeight); break; } }
public void PrintDataGrid(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = false; DataGrid dg = (DataGrid) c; Single extraHeight = 0; Single extraHeightHeaderLine = 0; Font printFont = new Font(dg.Font.Name, dg.Font.Size, FontStyle.Bold); Single h = mp.FontHeight(printFont); // Column header DataGridTableStyle myGridTableStyle; if (dg.TableStyles.Count == 0) { myGridTableStyle = new DataGridTableStyle(); dg.TableStyles.Add(myGridTableStyle); } // Header of each column Single xPos = x; Single yPos = y; Single w; extraHeightHeaderLine = mp.BeginPrintUnit(yPos, h+1); //Space required for header text for (int i = 0; i < dg.TableStyles[0].GridColumnStyles.Count; i++) { string caption = dg.TableStyles[0].GridColumnStyles[i].HeaderText; w = dg.TableStyles[0].GridColumnStyles[i].Width; if (xPos+w > x + dg.Width) w = x + dg.Width - xPos; if (xPos < x + dg.Width) mp.DrawString(caption, printFont, _Brush, xPos, yPos, w, h); if (i == 0) // Draw horizontal line below header mp.DrawLines(_Pen, x, yPos+h, x+dg.Width, yPos+h); xPos += w; } mp.EndPrintUnit(); yPos += h + 1 + extraHeightHeaderLine; // Get dataTable displayed in DataGrid // This function only support DataGrid with DataTable as DataSource // This is the only case I code to obtain data in DataGrid DataTable dt = null; if (dg.DataSource is System.Data.DataTable) dt = (DataTable) dg.DataSource; else if ((dg.DataSource is System.Data.DataSet) && (dg.DataMember != null)) { DataSet ds = (DataSet) dg.DataSource; if (ds.Tables.Contains(dg.DataMember)) dt = ds.Tables[dg.DataMember]; } // Loop on DataRow, with embed loop on columns if (dt != null) foreach (DataRow dr in dt.Rows) { extraHeight = mp.BeginPrintUnit(yPos, h); //Space required for header text xPos = x; for (int i = 0; i < dg.TableStyles[0].GridColumnStyles.Count; i++) { string caption = dr[i].ToString(); w = dg.TableStyles[0].GridColumnStyles[i].Width; if (xPos+w > x + dg.Width) w = x + dg.Width - xPos; if (xPos < x + dg.Width) mp.DrawString(caption, printFont, _Brush, xPos, yPos, w, h); xPos += w; }; mp.EndPrintUnit(); yPos += h + extraHeight; } // Draw horizontal line at the bottom of DataGrid if (yPos < y + dg.Height + extraHeightHeaderLine) yPos = y + dg.Height + extraHeightHeaderLine; mp.BeginPrintUnit(yPos, 1); mp.DrawLines(_Pen, x, yPos, x+dg.Width, yPos); mp.EndPrintUnit(); // Finally, Compute extendedHeight if ((yPos - y) > dg.Height) extendedHeight = (yPos - y) - dg.Height; }
public void PrintReportTitle(System.Windows.Forms.Control c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = false; Font printFont = new Font(c.Font.Name, (Single)(c.Font.Size * 1.3), FontStyle.Bold); float fontHeight = mp.FontHeight(printFont); Pen pen = new Pen(Color.Black, 2); extendedHeight = fontHeight + 3 + pen.Width + 1; BrickGraphics test = ps.Graph; test.Font = printFont; SizeF size = test.MeasureString(c.Tag.ToString()); mp.BeginPrintUnit(y, extendedHeight); int xy = (int)ps.PageSettings.UsablePageSize.Width / 2; xy = xy - Convert.ToInt32(size.Width / 2); mp.DrawString(c.Tag.ToString(), printFont, Color.Black, xy, y, c.Width, fontHeight + 4); y += fontHeight + 10; mp.DrawLines(pen,0, y, ps.PageSettings.UsablePageRect.Width - 30, y); mp.EndPrintUnit(); }
public void PrintGridView(DevExpress.XtraGrid.GridControl c, ParentControlPrinting typePrint, MultiPageManagement mp, Single x, Single y, ref Single extendedHeight, out bool ScanForChildControls) { ScanForChildControls = false; //DataTable dtbl = (DataTable) c.DataSource; DevExpress.XtraGrid.Views.Grid.GridView gv = (DevExpress.XtraGrid.Views.Grid.GridView)c.DefaultView; //DataGrid dg = (DataGrid) c; Single extraHeight = 0; Single extraHeightHeaderLine = 0; Font printFont = new Font(c.Font.Name, c.Font.Size, FontStyle.Bold); Single h = mp.FontHeight(printFont); // Column header //DataGridTableStyle myGridTableStyle; //if (dg.TableStyles.Count == 0) //{ // myGridTableStyle = new DataGridTableStyle(); // dg.TableStyles.Add(myGridTableStyle); //} // Header of each column Single xPos = x; Single yPos = y; Single w; extraHeightHeaderLine = mp.BeginPrintUnit(yPos, h+1); //Space required for header text for (int i = 0; i < gv.Columns.Count; i++) { string caption = gv.Columns[i].Caption; w = gv.Columns[i].Width; if (xPos+w > x + c.Width) w = x + c.Width - xPos; if (xPos < x + c.Width) mp.DrawString(caption, printFont, _Brush, xPos, yPos, w, h); if (i == 0) // Draw horizontal line below header mp.DrawLines(_Pen, x, yPos+h, x+c.Width, yPos+h); xPos += w; } mp.EndPrintUnit(); yPos += h + 1 + extraHeightHeaderLine; // Get dataTable displayed in DataGrid // This function only support DataGrid with DataTable as DataSource // This is the only case I code to obtain data in DataGrid //DataTable dt = null; //if (dg.DataSource is System.Data.DataTable) // dt = (DataTable) dg.DataSource; //else // if ((dg.DataSource is System.Data.DataSet) && (dg.DataMember != null)) //{ // DataSet ds = (DataSet) dg.DataSource; // if (ds.Tables.Contains(dg.DataMember)) // dt = ds.Tables[dg.DataMember]; //} // Loop on DataRow, with embed loop on columns //if (dt != null) for(int j=0; j<gv.RowCount;j++) { extraHeight = mp.BeginPrintUnit(yPos, h); //Space required for header text xPos = x; for (int i = 0; i < gv.Columns.Count; i++) { if(gv.Columns[i].VisibleIndex<0) continue; string caption = gv.GetRowCellValue(j, gv.Columns[i]).ToString(); w = gv.Columns[i].Width; if (xPos+w > x + c.Width) w = x + c.Width - xPos; if (xPos < x + c.Width) mp.DrawString(caption, printFont, _Brush, xPos, yPos, w, h); xPos += w; }; mp.EndPrintUnit(); yPos += h + extraHeight; } // Draw horizontal line at the bottom of DataGrid if (yPos < y + c.Height + extraHeightHeaderLine) yPos = y + c.Height + extraHeightHeaderLine; mp.BeginPrintUnit(yPos, 1); mp.DrawLines(_Pen, x, yPos, x+c.Width, yPos); mp.EndPrintUnit(); // Finally, Compute extendedHeight if ((yPos - y) > c.Height) extendedHeight = (yPos - y) - c.Height; }