Newbie to VB.net

I'm trying to find a replacement for Vb Printform think I may have found it.....

this will not compile: could someone walk me through the setup of this?

I created new project with one form on it Form1 in the code area I put the following::

PublicClass Form1

Imports System.Drawing

' ...

PrivateConst SRCCOPYAsInteger = &HCC0020

PrivateDeclareFunction BitBlt _

Lib"gdi32.dll" ( _

ByVal hdcDestAs IntPtr, _

ByVal xAs Int32, _

ByVal yAs Int32, _

ByVal WidthAs Int32, _

ByVal HeightAs Int32, _

ByVal hdcSrcAs IntPtr, _

ByVal xSrcAs Int32, _

ByVal ySrcAs Int32, _

ByVal dwRopAs Int32 _

)AsBoolean

Private formImageAs Bitmap

PublicSub PrintForm(OptionalByVal fullWindowAsBoolean =False)

' Copyright ? 2004 by Mathias Schiffer. Leave this notice in place.

' Copies a screenshot of the form this code is being run in

' to the active printer. Place this code in a form module.

' Set fullWindow to True for including borders and titlebar.

' Remember to use an Imports reference to System.Drawing.

' Also, add a PrintDocument1 control to the form for printing.

' The disadvantage of this code is that it does only capture

' those parts of the form that are actually visible (screenshot),

' i.e. it must not be hidden and may not even be obscured.

' VB's PrintForm method does not require that, so this is not a

' perfect replacement. Keep that in mind.

WithMe' This can easily be replaced with a Form parameter

' Create a Graphics object for the form

Dim formGraphicsAs Graphics = .CreateGraphics

' Create a compatible bitmap and get its Graphics object

If fullWindowThen

formImage =New Bitmap(.Width, .Height, formGraphics)

Else

formImage =New Bitmap(.ClientRectangle.Width, _

.ClientRectangle.Height, _

formGraphics)

EndIf

Dim memGraphicsAs Graphics = Graphics.FromImage(formImage)

' Get the target and source device context handles (hDC)

Dim sourceDCAs IntPtr = formGraphics.GetHdc

Dim targetDCAs IntPtr = memGraphics.GetHdc

' Do the screenshot part of the job

If fullWindowThen

' Consider the border width and the titlebar height

Dim widthDeltaAsInteger = (.Width - _

.ClientRectangle.Width)

Dim heightDeltaAsInteger = (.Height - _

.ClientRectangle.Height)

' Copy the form including its titlebar and borders

BitBlt(targetDC, _

0, 0, _

.ClientRectangle.Width + widthDelta, _

.ClientRectangle.Height + heightDelta, _

sourceDC, _

0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _

SRCCOPY)

Else

' Copy the form's client area

BitBlt(targetDC, _

0, 0, .ClientRectangle.Width, .ClientRectangle.Height, _

sourceDC, _

.ClientRectangle.X, .ClientRectangle.Y, _

SRCCOPY)

EndIf

' Release DCs and dispose objects

formGraphics.ReleaseHdc(sourceDC)

formGraphics.Dispose()

memGraphics.ReleaseHdc(targetDC)

memGraphics.Dispose()

' formImage now has the form's image. Print it before disposing it.

PrintDocument1.Print()' Invokes PrintDocument1_PrintPage (below)

' Finally dispose the image

formGraphics.Dispose()

EndWith

EndSub

PrivateSub PrintDocument1_PrintPage( _

ByVal senderAs System.Object, _

ByVal eAs System.Drawing.Printing.PrintPageEventArgs _

)Handles PrintDocument1.PrintPage

' Print the screen shot in formImage. Use DrawImage's

' parameters to control the position of the printer output.

e.Graphics.DrawImage(formImage, 100, 200)' printing position x=100, y=200

EndSub

EndClass

I get three errors line 2 Imports must proceed declarations

line 90 PrintDocument1 not declared

line 102 Handle requires WithEvent.

I'm sure its something simple but with the Vb.Net I don;t know thanks

Mitch

[10603 byte] By [Mitch5713] at [2007-12-24]
# 1

I actually posted some code using this Win32API for someone....take a look at this and see if it helps you. You may also be noted that the Visual Basic powerpack tools apperently (but do not quote me) have a print feature that helps you do the thing you are after, printing out a form

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=747921&SiteID=1

infact, please do read the entire thread, as you should as it contains other information also, NoBugz also points out the PowerPack which supports printing forms

I hope this helps

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
the first error is because your first two lines should be.

Imports System.Drawing

Public Class Form1

not

Public Class Form1

Imports System.Drawing

your second and third error is becuase you need to add a Printdocument control to your form in design time as it says in your comments,

Andrew

BeanR at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
The VB team has just released the PrintForm Power Pack, it will take care of printing the form for you. Note: the link appears to be down just now, MSFT doesn't have a lot of luck keeping their web servers running. Try again later if necessary...
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

This link works for me...

Best regards,
Johan Stenberg

MSJohanStenberg at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
Wow thats sounds just great!! With the language improvements VB is in the Stone age when it comes to printing something Definatly needs to be done!!! However I cannot ever get to that site any other ideas where I may find this? Site is always down
Mitch5713 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Try this link instead, I just checked it.
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...