I would like to print a form with calculated data, grafs, datagrids... I'm having problems translating the c# code for printing into f# code:
我想打印一个包含计算过的数据、格拉芙、datagrid…我在把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);
}
I can't seem to link PrintDocument, PrintDialog and PrintPageEventArgs correctly. Can anybody point me in the right direction?
我似乎不能正确地链接PrintDocument, PrintDialog和PrintPageEventArgs。谁能给我指出正确的方向吗?
Thanks
谢谢
2 个解决方案
#1
1
A somewhat sketchy, nevertheless fully functional snippet (download source code from here) showing event wiring for printing of the Form
containing an FSharp.Charting
chart upon a click of the Button
placed on the same form:
一个略显粗糙但功能完整的代码片段(从这里下载源代码),显示了用于打印包含FSharp的表单的事件连接。在同一表格上按下按钮,图表显示:
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
In order to build it add FSharp.Charting
with nuget
as well as references to System.Drawing
and System.Windows.Forms
.
为了构建它,添加了FSharp。用nuget绘制图表以及对系统的引用。画画和System.Windows.Forms。
The constituents are:
成分是:
-
captureScreen
function returning a Bitmap image of the whole form - captureScreen函数返回整个表单的位图图像
-
System.Drawing.Printing.PrintDocument()
isprinter
reusable object (not to be added to the form) - system . draw . print . printdocument()是打印机可重用的对象(不添加到表单中)
-
Button
havingprinter.Print()
method wired to button'sClick
event - 将print . print()方法连接到按钮的单击事件的按钮
-
printer.PrintPage
event handler that makes the button invisible, captures the form into bitmap and draws the latter, then restores button visibility. - 打印机。使按钮不可见的PrintPage事件处理程序,将表单捕获到位图并绘制后者,然后恢复按钮可见性。
I believe this can be a fair enough starting point for you to move forward.
我相信这是一个足够公平的起点,让你继续前进。
#2
0
Thanks a lot!
谢谢!
I've added the printdialog in the code below:
我在下面的代码中添加了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)
#1
1
A somewhat sketchy, nevertheless fully functional snippet (download source code from here) showing event wiring for printing of the Form
containing an FSharp.Charting
chart upon a click of the Button
placed on the same form:
一个略显粗糙但功能完整的代码片段(从这里下载源代码),显示了用于打印包含FSharp的表单的事件连接。在同一表格上按下按钮,图表显示:
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
In order to build it add FSharp.Charting
with nuget
as well as references to System.Drawing
and System.Windows.Forms
.
为了构建它,添加了FSharp。用nuget绘制图表以及对系统的引用。画画和System.Windows.Forms。
The constituents are:
成分是:
-
captureScreen
function returning a Bitmap image of the whole form - captureScreen函数返回整个表单的位图图像
-
System.Drawing.Printing.PrintDocument()
isprinter
reusable object (not to be added to the form) - system . draw . print . printdocument()是打印机可重用的对象(不添加到表单中)
-
Button
havingprinter.Print()
method wired to button'sClick
event - 将print . print()方法连接到按钮的单击事件的按钮
-
printer.PrintPage
event handler that makes the button invisible, captures the form into bitmap and draws the latter, then restores button visibility. - 打印机。使按钮不可见的PrintPage事件处理程序,将表单捕获到位图并绘制后者,然后恢复按钮可见性。
I believe this can be a fair enough starting point for you to move forward.
我相信这是一个足够公平的起点,让你继续前进。
#2
0
Thanks a lot!
谢谢!
I've added the printdialog in the code below:
我在下面的代码中添加了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)