图形功能问题 [英] Graphics functionality question
问题描述
我如何进行这项工作.我有两种方法,
How do I make this work. I have two methods,
public void Calculate(Graphics g)
{
---code---
}
private void btnPrint_Click(object sender, EventArgs e)
{
---code---
Calculate(Graphics g);
}
我想在btnPrint_Click方法中调用Calculate方法,但无法执行.感谢您的帮助.
I want to call the Calculate method inside the btnPrint_Click method but I cannot do it. Any help is appreciated.
推荐答案
简单.如果您想多想一点,便会轻松找到解决方案.
Easy. You would easily find a solution if you thought just a bit more.
public partial class FormMain {
void Print() {
using (PrintDocument pd = new PrintDocument()) {
if (PrinterSettings != null)
PrintDialog.PrinterSettings = PrinterSettings;
pd.PrintPage += (sender, eventArgs) => { this.Calculate(eventArgs.Graphics); };
DialogResult result = PrintDialog.ShowDialog();
if (result != DialogResult.OK) return;
PrinterSettings = PrintDialog.PrinterSettings;
PrintDialog.Document = pd;
pd.PrinterSettings = PrinterSettings;
// could be a lot more, like taking into account page setup, using page setup dialog...
pd.Print();
} //using (pd.Dispose is automatically called in this point)
} //Print
PrintDialog PrintDialog = new PrintDialog();
PrinterSettings PrinterSettings;
//...
} //class FormMain
您的Calculate(Graphics g)
实际上是一个好主意,但您可能低估了它.您从System.Drawing.Graphics
的具体实例中提取了一些渲染.这样,您只有一种图形渲染方法,可以从某些控件OnPaint
中调用它,还可以将其用于其他渲染目标:打印",打印到...",导出到位图",导出到...". (某些矢量图形)之类的东西-相当普遍.你有主意吗?
Your Calculate(Graphics g)
is actually a good idea, but you probably underestimate it. You abstract some rendering from concrete instance of System.Drawing.Graphics
. This way, you can have only one graphics rendering method and call it from some controls OnPaint
but also use it for other targets of rendering: "Print", "Print to…", "Export to bitmap", "Export to…" (some vector graphics) and the like — quite universal. Are you getting the idea?
完全破获,但我认为TRK3是正确的:
Totally rip off but I think TRK3 is right:
您是什么意思我不能做到"?您是说编译器抱怨是因为您应该调用:Calculate(g)而不是Calculate(Graphics g)吗?还是说您在调用异常时收到异常?还是?
What do you mean "I cannot do it"? Do you mean the compiler complains because you should be calling: Calculate(g) instead of Calculate(Graphics g) ? Or do you mean you get an exception when you call it? Or ?
这篇关于图形功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!