Print my windows

Good morning group.
I would like to print the WINDOWS I am viewing. But I can not.
There is a command like Window.print… I’ve read the documentation a bit but it always talks about textual prints and other examples instead create procedures for printing. Isn’t there a ready-made command, easy easy ? :slight_smile:

I use in a pressed button: [code]
Var ps As New PrinterSetup
Var g As Graphics
g = ps.ShowPrinterDialog()
If g <> Nil Then
Var p As New Picture( window1.Width, window1.Height, 32)
window1.DrawInto p.Graphics, 0, 0
Var gheight As Integer = g.Width/p.Width*p.Height
g.DrawPicture p, 0, 0, g.Width, gheight, 0, 0, p.Width, p.Height
End If

[/code]

Is this ok or is there something simpler, more functional?

Have a look at the documentation:

If that code works, use it. I do not see how it could be any simpler.

put this in a global module

function print( extends w as window)

Var ps As New PrinterSetup
Var g As Graphics
g = ps.ShowPrinterDialog()
If g <> Nil Then
  Var p As New Picture( w.Width, w.Height, 32)
  w.DrawInto p.Graphics, 0, 0
  Var gheight As Integer = g.Width/p.Width*p.Height
  g.DrawPicture p, 0, 0, g.Width, gheight, 0, 0, p.Width, p.Height
End If

end function

then you can use mywindow1.print

3 Likes

i’ve never used FUNCTION, how do you insert them?

Do I need to enter a Method ?

in this example, is missed a RETURN value ?

A function is just a method that returns a value.
(Although the example above does not, oddly)
In Xojo code, if you add a method, it ‘codes’ it as a SUBroutine
If you add a return value in the IDE, it ‘codes’ it as a Function

To exit these before the end, you need to use Exit Sub or Exit Function, but just ‘Exit’ works on both.

exact syntax is :

Public Sub print(extends w as Window)
  Var ps As New PrinterSetup
  Var g As Graphics
  g = ps.ShowPrinterDialog()
  If g <> Nil Then
    Var p As New Picture( w.Width, w.Height, 32)
    w.DrawInto p.Graphics, 0, 0
    Var gheight As Integer = g.Width/p.Width*p.Height
    g.DrawPicture p, 0, 0, g.Width, gheight, 0, 0, p.Width, p.Height
  End If
End Sub

and yes, create a module,
create a method with a scope set to global, inside with the above parameters

1 Like

I can’t figure out where I’m wrong … when on the button PUSH I write window1. I don’t see print.
Where am I wrong?

put the print method IN A MODULE

https://documentation.xojo.com/api/language/extends.html#extends

You can use Extends only for methods in a module.

I’ve read the guide, but I don’t know how to do it.
I put the method in a Module … but still can’t invoke the function on keypress window1. (print does not appear)

what is in the “pressed” event of the button ?

When I go to the pressed event of the button and write window1.print, the .print does not appear after window1. As if he didn’t see the function.

try to type “print” anyway and see if it runs ans compile. it should
sometimes when you just made a function the IDE does not autocomplete, you have to save and reopen the project.

Not work

my bad … desktop window is not a window !
print (extends w as desktopwindow) …

Ok Jean it’s work !! Thanks !!! so my code is neater :slight_smile:

I was curious to know where this ‘g’ came from:

image

but it must have been corrected by now, or it still wouldn’t compile

yes, I misspelled it. You don’t need g as an output. :slight_smile: