我想打印带有计算数据,grafs,datagrids的表单...... 我在将用于打印的c#代码翻译成f#代码时遇到了问题:
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument1_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
我似乎无法正确链接PrintDocument,PrintDialog和PrintPageEventArgs。 任何人都可以指出我正确的方向吗?
由于
答案 0 :(得分:1)
一个稍微粗略但功能完备的代码片段(来自here的下载源代码),显示事件连线,用于在点击{{1}时打印包含Form
图表的FSharp.Charting
放在同一个表格上:
Button
为了构建它,请添加open System
open FSharp.Charting
open FSharp.Charting.ChartTypes
open System.Drawing
open System.Drawing.Printing
open System.Windows.Forms
[<STAThread; EntryPoint>]
let main args =
let captureScreen (form: Form) =
let myGraphics = form.CreateGraphics()
let size = form.Size
let memoryImage = new Bitmap(size.Width, size.Height, myGraphics)
let memoryGraphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, size)
memoryImage
let myChart = [for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)]
|> Chart.Line |> Chart.WithYAxis(Title="Test")
let chart = new ChartControl(myChart, Dock=DockStyle.Fill)
use printer = new System.Drawing.Printing.PrintDocument()
let printBtn = new Button(Text="Print", Dock=DockStyle.Bottom)
printBtn.Click.Add(fun prt -> printer.Print())
let form = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
printer.PrintPage.Add(fun prt ->
printBtn.Visible <- false
prt.Graphics.DrawImage(captureScreen(form), 0, 0)
printBtn.Visible <- true)
form.Controls.AddRange([|chart; printBtn |])
do Application.Run(form) |> ignore
0
FSharp.Charting
以及对nuget
和System.Drawing
的引用。
成分是:
System.Windows.Forms
函数返回整个表单的位图图像captureScreen
是System.Drawing.Printing.PrintDocument()
可重复使用的对象(不会添加到表单中)printer
方法连接到按钮的Button
事件printer.Print()
Click
事件处理程序使按钮不可见,将表单捕获到位图并绘制后者,然后恢复按钮可见性。我相信这对你来说是一个公平的起点。
答案 1 :(得分:0)
非常感谢!
我在下面的代码中添加了printdialog:
let captureScreen (form: Form) =
let myGraphics = form.CreateGraphics()
let size = form.Size
let memoryImage = new Bitmap(size.Width, size.Height, myGraphics)
let memoryGraphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, size)
memoryImage
let printdoc = new System.Drawing.Printing.PrintDocument()
let printdia = new PrintDialog(Document=printdoc)
let mutable Image = new Bitmap(main.Size.Width, main.Size.Height)
let mutable printok = new DialogResult()
print.Click.Add(fun prt -> Image <- captureScreen(main)
printok <- printdia.ShowDialog()
if printok = DialogResult.OK then printdoc.Print())
printdoc.PrintPage.Add(fun prt ->
print.Visible <- false
prt.Graphics.DrawImage(Image, 0, 0)
print.Visible <- true)