更改 MSChart 背景进行打印

发布于 2024-10-31 08:27:28 字数 271 浏览 2 评论 0原文

我已输入代码来在打印时更改 MSCharting 区域颜色。

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

我的问题是,在用户选择“打印”或“关闭”打印预览对话框后,或者单击对话框“X”后,如何处理颜色变回原始颜色。

事实上,如果我使用 PrintDialog,一旦打印完成或取消,如何将背景设置回正常状态?

I have put in code to change the MSCharting area colour when printing.

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

My quesiton is, how can I handle the color to change back to its oringinal color, eitehr after user has selected Print, or Close form the printpreview dialog,, or if the click on the dialogs "X".

In fact if I use the PrintDialog instead, how could I set background back to normal once printing has been completed or canceled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2024-11-07 08:27:28

有点晚了,但我希望它能帮助别人。

对于 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;
  }

little late but I hope it helps someone.

To MsChart print I'am using PrintDocument events. BeginPrint event for setting colors for printing, PrintPage event for print itself and EndPrint event for setting colors back before print.

Sample code:

 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;
  }
一梦等七年七年为一梦 2024-11-07 08:27:28

不幸的是,没有简单的方法,因为 PrintPreview 不提供任何回调。

您可以制作仅用于打印的图表副本,并将默认图表区域替换为自定义区域(具有自定义背景)。

另一种方法是更改​​ BG 颜色,使用 PrintPaint 将图表打印到内存中的图像,恢复 BG 颜色并为刚刚渲染的图像手动显示打印对话框。

还有更多方法,例如挂钩新创建的窗口,但它们变得越来越复杂和肮脏。

祝你好运

Unfortunatly there is no easy way since PrintPreview doesnt provide any callbacks.

You can make a copy of the chart used solely for printing with the default chart area replaced by your custom area (with custom background).

Another way is to change the BG Color, print the chart to a in-memory image using PrintPaint, restore the BG Color and show a print dialog manually for the image you just rendered.

There are more ways like hooking the newly created window but they are getting more complex and more dirty.

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文