Pages - Menu

2016年5月18日 星期三

將chart 轉圖表列印 使用e.Graphics.DrawString

沒想到會有自己手動刻報表的時候,使用winform開發,
要列印報表。需加上chart,本來想使用rdlc作為列印的工作,
但他的chart太陽春了,很多功能做不到,只好拿原本的mschart轉成圖片後塞進去。
刻的過程,通常會寫在printDocument.printPage裡面
先寫幾個常用到的。
1.計算title字串的寬度有多少,把width改成height就是高度了。
e.Graphics.MeasureString(title,New Font("Arial", 24.0F, FontStyle.Bold), e.MarginBounds.Width).Width
2.使用字型的大小去算高度
dim ypos as integer = New Font("Arial", 16.0F, FontStyle.Underline).GetHeight
3.劃線:
先建立筆的顏色、寬度。再來是實線的長度、虛線的長度。
指定線條的種類為自訂,也可以設定實線、虛線
傳入自己設定的虛線陣列。
畫出
Dim LinePen As New Pen(Color.Black, 1)
Dim lineDash As Single() = {10, 2}  '實線長度、虛線長度(loop)
LinePen.DashStyle = Drawing2D.DashStyle.Custom
LinePen.DashPattern = lineDash
e.Graphics.DrawLine(LinePen, New Point(e.MarginBounds.Left, yPos), New PointF(e.MarginBounds.Right, yPos))

4.建立字串\t,拿來對齊使用,vb需使用vbtab,如果在c#可直接使用\t即可。
os:為了讓同事能夠維護只能乖乖寫VB,言下之意就是我遲早會勞跑XD
e.Graphics.DrawString(String.Format("Cpk製程能力總和指數:{0}\t{1}", txtCpk.Text, txtCpkLv.Text).Replace("\t", vbTab), New Font(dg.Font, FontStyle.Bold), Brushes.Black,e.MarginBounds.Left + 261, yPos)
string.format("{1}{0}{2}{0}",a,vbTab,2,vbTab)

5.畫datagrid標題列
For Each GridCol As DataGridViewColumn In dg.Columns
                            e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLeft(iCount)),
                                                    iTopMargin, CInt(arrColumnWidth(iCount)), iHeaderHeight))                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLeft(iCount)), iTopMargin,
                                                                               CInt(arrColumnWidth(iCount)), iHeaderHeight))

                            e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                  New SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                  New RectangleF(CInt(arrColumnLeft(iCount)), iTopMargin,
                                                                 CInt(arrColumnWidth(iCount)), iHeaderHeight), strFormat)
                            iCount += 1
                        Next


6.將chart轉成圖檔
'將chart轉成圖檔
            dataChart.DrawToBitmap(bmp, New Rectangle(0, 0, dataChart.Width, dataChart.Height))
            e.Graphics.DrawImage(DirectCast(dest_bmp, Image), e.PageBounds.Left + 20, yPos)

沒有留言:

張貼留言