2011-04-11 108 views
0

我已經在代碼中打印時更改了MSCharting區域的顏色。更改MSChart打印背景

chart.ChartAreas[o].BackColor = System.Drawing.Color.White; 
chart.Printing.PrintPreview(); 

我quesiton是,我怎麼能處理的顏色改回其oringinal顏色,eitehr後,用戶已經選擇打印或關閉形式打印預覽對話框,,或者如果在對話框中的「X」點擊。

事實上,如果我改用PrintDialog,一旦打印完成或取消後,如何將背景設置恢復正​​常?

回答

0

不幸的是沒有簡單的方法,因爲PrintPreview不提供任何回調。

您可以製作僅用於打印的圖表副本,默認圖表區域由您的自定義區域(具有自定義背景)取代。

另一種方法是更改​​BG顏色,使用PrintPaint將圖表打印到內存中的圖像,恢復BG顏色並手動顯示剛剛呈現的圖像的打印對話框。

有更多的方法像鉤住新創建的窗口,但他們變得越來越複雜,越來越骯髒。

祝你好運

1

有點遲,但我希望它可以幫助別人。

給MsChart打印我使用PrintDocument事件。 BeginPrint用於設置打印顏色的事件,PrintPage用於打印的事件以及EndPrint用於在打印之前設置顏色的事件。

示例代碼:

public GraphFrm() 
    { 
    InitializeComponent(); 

    //new PrintDocument object to reset default one 
    chart.Printing.PrintDocument = new System.Drawing.Printing.PrintDocument(); 
    //set up events 
    chart.Printing.PrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDocument_PrintPage); 
    chart.Printing.PrintDocument.BeginPrint +=new System.Drawing.Printing.PrintEventHandler(PrintDocument_BeginPrint); 
    chart.Printing.PrintDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(PrintDocument_EndPrint); 
    //default print setting like margins and landscape 
    chart.Printing.PrintDocument.DefaultPageSettings.Margins.Bottom = 50; 
    chart.Printing.PrintDocument.DefaultPageSettings.Margins.Top = 50; 
    chart.Printing.PrintDocument.DefaultPageSettings.Margins.Left = 50; 
    chart.Printing.PrintDocument.DefaultPageSettings.Margins.Right = 50; 
    chart.Printing.PrintDocument.DefaultPageSettings.Landscape = true; 
    chart.Printing.PrintDocument.DefaultPageSettings.Color = true; 

    ... 
    } 

    public void Print() 
    { 
    //print method with show print dialog 
    chart.Printing.Print(true); 
    } 

    void PrintDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
    { 
    //set print color 
    PrintChartColorSet(); 
    } 

    void PrintDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
    { 
    //restore colors 
    PrintChartColorRestoreDefault(); 
    } 

    void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
    //print chart into rectangle defined by margins 
    Rectangle chartPosition = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height); 

    chart.Printing.PrintPaint(e.Graphics, chartPosition); 
    } 

    Color BackColor, BorderlineColor, CaBackColor, CaBorderColor, AxColor, LeBackColor, LeForeColor; 

    void PrintChartColorSet() 
    { 
    BackColor = chart.BackColor; 
    chart.BackColor = Color.White; 

    BorderlineColor = chart.BorderlineColor; 
    chart.BorderlineColor = Color.White; 

    CaBackColor = chart.ChartAreas[0].BackColor; 
    chart.ChartAreas[0].BackColor = Color.White; 

    CaBorderColor = chart.ChartAreas[0].BorderColor; 
    chart.ChartAreas[0].BorderColor = Color.Black; 

    AxColor = chart.ChartAreas[0].Axes[0].LineColor; 

    foreach(Axis a in chart.ChartAreas[0].Axes) 
    { 
     a.LineColor = Color.Black; 
     a.TitleForeColor = Color.Black; 
     a.MajorGrid.LineColor = Color.Black; 
     a.MajorTickMark.LineColor = Color.Black; 
     a.MinorGrid.LineColor = Color.Black; 
     a.MinorTickMark.LineColor = Color.Black; 
     a.LabelStyle.ForeColor = Color.Black; 
    } 

    LeBackColor = chart.Legends[0].BackColor; 
    chart.Legends[0].BackColor = Color.White; 

    LeForeColor = chart.Legends[0].ForeColor; 
    chart.Legends[0].ForeColor = Color.Black; 
    } 

    void PrintChartColorRestoreDefault() 
    { 
    chart.BackColor = BackColor; 
    chart.BorderlineColor = BorderlineColor; 
    chart.ChartAreas[0].BackColor = CaBackColor; 
    chart.ChartAreas[0].BorderColor = CaBorderColor; 

    foreach(Axis a in chart.ChartAreas[0].Axes) 
    { 
     a.LineColor = AxColor; 
     a.TitleForeColor = AxColor; 
     a.MajorGrid.LineColor = AxColor; 
     a.MajorTickMark.LineColor = AxColor; 
     a.MinorGrid.LineColor = AxColor; 
     a.MinorTickMark.LineColor = AxColor; 
     a.LabelStyle.ForeColor = AxColor; 
    } 

    chart.Legends[0].BackColor = LeBackColor; 
    chart.Legends[0].ForeColor = LeForeColor; 
    }