-
Oct 11th, 2014, 02:49 PM
#1
Thread Starter
PowerPoster
[RESOLVED] make a forms width fit printout
The form is wider than a sheet of paper and not ver tall
I have form that i am trying to avoid making everthing smaller so it can be printed on one sheet of paper
way i see it their are 3 possible solutations
1. redraw all the controls to make them closer together thus making the form more narrow.
Trying to avoid this as many users are using high resolutation and am not sure it will still fit
2. Print the form in Landscape.
Have read where not all printers support Landscape and am not sure if printform can print Landscape
3. Create another form just for printing and copy all the data
Maybe their is another method i have not listed.
Which way would be the best ? and is there another way ?
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 02:55 PM
#2
Re: make a forms width fit printout
If the printer has an option to Scale To Page, you can opt for that
Another option is to use a hidden picturebox (sized same as your form) with AutoRedraw set to true. Print your form to the hidden picbox's hDC. Then you can scale the contents via PaintPicture back to the same picbox, then print the .Image property? Last time I used a method like that was well over 10 years ago (kinda like print preview ). So I know I didn't fill in all the "holes", but the logic should be sound.
Also may want to search this forum for keywords: print preview
-
Oct 11th, 2014, 03:17 PM
#3
Re: make a forms width fit printout
The best way to do that IMO is to capture screen to a picturebox and then Paintpicture it directly to the printer device as LaVolpe suggested. When you write routines that do such manipulations such as scaling or flipping, you can declare your sub routine using OBJECT instead of PictureBox. Doing this, a scaling routine using Object, Object.Scalewidth and Object.scaleHeight can be made device and Units independent. Normally, my pictureboxes are defined in twips or pixels while I prefer Inches for the printer.
You can also use typeof on the object inside a routine and print portrait or landcape, and skip that bit of code if the object is a picturebox.
Last edited by Navion; Oct 11th, 2014 at 03:25 PM.
Reason: a few mode details
-
Oct 11th, 2014, 03:22 PM
#4
Thread Starter
PowerPoster
Re: make a forms width fit printout
Thanks LaVolpe
No telling about the first option
The 2nd option seems like the best, but most is over my head and will take a lot of posts ti this forum
I will search print preview
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 06:17 PM
#5
Re: make a forms width fit printout
Every printer should allow landscape, if not then simply flip it landscape in the picturebox. What code were you using?
-
Oct 11th, 2014, 06:26 PM
#6
Re: make a forms width fit printout
I don't think I have ever came across a printer that did not support landscape printing unless it was just a label printer.
So it would seem that you should be printing in landscape mode and then you might want to scale it as well.
As for a method not mentioned the obvious would be to use a report rather than printing the form.
-
Oct 11th, 2014, 06:45 PM
#7
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by Max187Boucher
Every printer should allow landscape, if not then simply flip it landscape in the picturebox. What code were you using?
I did not try it I just read it somewhere'
I just did printform
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 06:47 PM
#8
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by DataMiser
I don't think I have ever came across a printer that did not support landscape printing unless it was just a label printer.
So it would seem that you should be printing in landscape mode and then you might want to scale it as well.
As for a method not mentioned the obvious would be to use a report rather than printing the form.
What do you mean by Use a report ?
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 10:36 PM
#9
Re: make a forms width fit printout
Ok I got two options for you.
1 - Use Landscape
Code:
Printer.Orientation = vbPRORLandscape
PrintForm
2 - Use this function (I put it in a Module .bas)
Code:
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBitmap As Long
hPal As Long
Reserved As Long
End Type
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Function FormToPrint(MyForm As Form) As Picture
Dim FormWidth As Long
Dim FormHeight As Long
Dim SourceDC As Long
Dim MemoryDC As Long
Dim hBitmap As Long
Dim hBitmapTemp As Long
With MyForm
FormWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbPixels)
FormHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbPixels)
End With
SourceDC = MyForm.hdc
MemoryDC = CreateCompatibleDC(SourceDC)
hBitmap = CreateCompatibleBitmap(SourceDC, FormWidth, FormHeight)
hBitmapTemp = SelectObject(MemoryDC, hBitmap)
BitBlt MemoryDC, 0, 0, FormWidth, FormHeight, SourceDC, 0, 0, vbSrcCopy
hBitmap = SelectObject(MemoryDC, hBitmapTemp)
DeleteDC MemoryDC
ReleaseDC MyForm.hWnd, SourceDC
Set FormToPrint = CreateBitmap(hBitmap)
End Function
Private Function CreateBitmap(ByVal hBitmap As Long) As Picture
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = vbPicTypeBitmap
.hBitmap = hBitmap
End With
OleCreatePictureIndirect Pic, IID_IDispatch, 1, IPic
Set CreateBitmap = IPic
End Function
Here is how you call it (also added a simple picture scaling option)
Code:
Dim Percentage As Double
Percentage = 50 'Set Image scale %
Percentage = Percentage / 100
Printer.ScaleMode = vbTwips
Printer.PaintPicture FormToPrint(Form1), 0, 0, Percentage * Form1.ScaleWidth, Percentage * Form1.ScaleHeight
Printer.EndDoc
Let me know if this works for you.
-
Oct 11th, 2014, 10:42 PM
#10
Re: make a forms width fit printout
Originally Posted by isnoend07
What do you mean by Use a report ?
I mean use a report, as in either the report tool that comes with VB or Crystal or the one I use Active Reports these are designed for displaying and printing data reports which is what you seem to be trying to do by printing your form.
I always use a report or direct code and never resort to printing a form.
IMO the forms border, title bar, control box, the close button, the borders around the text boxes and such make it look very unprofessional when printed that way.
-
Oct 11th, 2014, 11:24 PM
#11
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by Max187Boucher
Ok I got two options for you.
1 - Use Landscape
Code:
Printer.Orientation = vbPRORLandscape
PrintForm
2 - Use this function (I put it in a Module .bas)
Code:
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBitmap As Long
hPal As Long
Reserved As Long
End Type
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Function FormToPrint(MyForm As Form) As Picture
Dim FormWidth As Long
Dim FormHeight As Long
Dim SourceDC As Long
Dim MemoryDC As Long
Dim hBitmap As Long
Dim hBitmapTemp As Long
With MyForm
FormWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbPixels)
FormHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbPixels)
End With
SourceDC = MyForm.hdc
MemoryDC = CreateCompatibleDC(SourceDC)
hBitmap = CreateCompatibleBitmap(SourceDC, FormWidth, FormHeight)
hBitmapTemp = SelectObject(MemoryDC, hBitmap)
BitBlt MemoryDC, 0, 0, FormWidth, FormHeight, SourceDC, 0, 0, vbSrcCopy
hBitmap = SelectObject(MemoryDC, hBitmapTemp)
DeleteDC MemoryDC
ReleaseDC MyForm.hWnd, SourceDC
Set FormToPrint = CreateBitmap(hBitmap)
End Function
Private Function CreateBitmap(ByVal hBitmap As Long) As Picture
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = vbPicTypeBitmap
.hBitmap = hBitmap
End With
OleCreatePictureIndirect Pic, IID_IDispatch, 1, IPic
Set CreateBitmap = IPic
End Function
Here is how you call it (also added a simple picture scaling option)
Code:
Dim Percentage As Double
Percentage = 50 'Set Image scale %
Percentage = Percentage / 100
Printer.ScaleMode = vbTwips
Printer.PaintPicture FormToPrint(Form1), 0, 0, Percentage * Form1.ScaleWidth, Percentage * Form1.ScaleHeight
Printer.EndDoc
Let me know if this works for you.
Thanks for adding those options
Hope the 2nd option works
I will try now and be back
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 11:45 PM
#12
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by isnoend07
Thanks for adding those options
Hope the 2nd option works
I will try now and be back
Thanks that works good with some minor adjustments it will be perfect eg;
little bigger and center aligned.
I am glad you added that
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 11th, 2014, 11:47 PM
#13
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by Max187Boucher
Ok I got two options for you.
1 - Use Landscape
Code:
Printer.Orientation = vbPRORLandscape
PrintForm
2 - Use this function (I put it in a Module .bas)
Code:
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBitmap As Long
hPal As Long
Reserved As Long
End Type
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Function FormToPrint(MyForm As Form) As Picture
Dim FormWidth As Long
Dim FormHeight As Long
Dim SourceDC As Long
Dim MemoryDC As Long
Dim hBitmap As Long
Dim hBitmapTemp As Long
With MyForm
FormWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbPixels)
FormHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbPixels)
End With
SourceDC = MyForm.hdc
MemoryDC = CreateCompatibleDC(SourceDC)
hBitmap = CreateCompatibleBitmap(SourceDC, FormWidth, FormHeight)
hBitmapTemp = SelectObject(MemoryDC, hBitmap)
BitBlt MemoryDC, 0, 0, FormWidth, FormHeight, SourceDC, 0, 0, vbSrcCopy
hBitmap = SelectObject(MemoryDC, hBitmapTemp)
DeleteDC MemoryDC
ReleaseDC MyForm.hWnd, SourceDC
Set FormToPrint = CreateBitmap(hBitmap)
End Function
Private Function CreateBitmap(ByVal hBitmap As Long) As Picture
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = vbPicTypeBitmap
.hBitmap = hBitmap
End With
OleCreatePictureIndirect Pic, IID_IDispatch, 1, IPic
Set CreateBitmap = IPic
End Function
Here is how you call it (also added a simple picture scaling option)
Code:
Dim Percentage As Double
Percentage = 50 'Set Image scale %
Percentage = Percentage / 100
Printer.ScaleMode = vbTwips
Printer.PaintPicture FormToPrint(Form1), 0, 0, Percentage * Form1.ScaleWidth, Percentage * Form1.ScaleHeight
Printer.EndDoc
Let me know if this works for you.
Tried to give you reputation,but i have to spread some first
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 12th, 2014, 12:39 AM
#14
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by isnoend07
Tried to give you reputation,but i have to spread some first
How can the printout be centered.
Have changed the size: Percentage = 75 '50 'Set Image scale %
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 12th, 2014, 12:46 AM
#15
Re: [RESOLVED] make a forms width fit printout
Just so you know you can set more of the printer properties.
For example,
Orientation (vbPRORLandscape/vbPRORPortrait)
PrintQuality (vbPRPQDraft/vbPRPQHigh/vbPRPQLow/vbPRPQMedium)
ColorMode (vbPRCMMonochrome/vbPRCMColor) 'Black/White or Color
-
Oct 12th, 2014, 12:53 AM
#16
Re: make a forms width fit printout
Originally Posted by isnoend07
How can the printout be centered.
Have changed the size: Percentage = 75 '50 'Set Image scale %
Sorry I won't have time to test this to see if it works...
read this here
That link might have the solution you need or an idea on how to implement a "center picture" option.
-
Oct 12th, 2014, 12:02 PM
#17
Thread Starter
PowerPoster
Re: make a forms width fit printout
Originally Posted by Max187Boucher
Sorry I won't have time to test this to see if it works...
read this here
That link might have the solution you need or an idea on how to implement a "center picture" option.
Thanks Max187Boucher I could not get the VBWire code to work,so have decided to arrange the form
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 12th, 2014, 02:49 PM
#18
Re: [RESOLVED] make a forms width fit printout
Sorry to leave you stranded but you should be able to adjust the height (top) of your picture printout. Either by setting some printer property or adding blank lines in front before your picture. Only have a cheap iphone at the moment so can't try it out.
-
Oct 12th, 2014, 04:19 PM
#19
Thread Starter
PowerPoster
Re: [RESOLVED] make a forms width fit printout
Originally Posted by Max187Boucher
Sorry to leave you stranded but you should be able to adjust the height (top) of your picture printout. Either by setting some printer property or adding blank lines in front before your picture. Only have a cheap iphone at the moment so can't try it out.
No problem
I appreciate the help
setting the top margin is a simple matter of moving a frame before printing, then putting it back.
Also changed the forms color from brown to white
This change is briefly visible so i may have to copy the data to another form and print that form. Posted a question here:
http://www.vbforums.com/showthread.p...31#post4770631
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 18th, 2014, 10:04 AM
#20
Thread Starter
PowerPoster
Re: [RESOLVED] make a forms width fit printout
Originally Posted by Max187Boucher
Sorry to leave you stranded but you should be able to adjust the height (top) of your picture printout. Either by setting some printer property or adding blank lines in front before your picture. Only have a cheap iphone at the moment so can't try it out.
Had Decided to use printform along with Private Declare Function LockWindowUpdate so as to not briefly show arranging controls before
print. But now I have discovered Printforms print is blurry and am trying to go back to the code you posted, which i had a problem centering
print output, but the print was clear just not centered.
I am thinking a fix may be to put all the forms controls in a borderless picturebox and arranging that before print.
Would this work ?
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Oct 18th, 2014, 04:21 PM
#21
Re: [RESOLVED] make a forms width fit printout
I got you a center option now, but reducing an image size will always cause some sort of distortion, but it still looks good. I'm sure you could make it clearer with some function (like in photoshop) but thats a different story, you would need to fix the pixels that are distorted.
Ok anyways here is what I came up with...
Code:
Dim Percentage As Double
Dim PrintX As Integer
Dim PrintY As Integer
Dim PrintWidth As Integer
Dim PrintHeight As Integer
Percentage = 75
Percentage = Percentage / 100
Printer.ScaleMode = vbTwips
Printer.Orientation = vbPRORLandscape 'vbPRORLandscape, vbPRORPortrait
Printer.PrintQuality = vbPRPQHigh 'vbPRPQDraft, vbPRPQLow, vbPRPQMedium, vbPRPQHigh
PrintWidth = Percentage * ScaleX(Form1.ScaleWidth, Form1.ScaleMode, Printer.ScaleMode)
PrintHeight = Percentage * ScaleY(Form1.ScaleHeight, Form1.ScaleMode, Printer.ScaleMode)
PrintX = Printer.ScaleLeft + (Printer.ScaleWidth - PrintWidth) / 2
PrintY = Printer.ScaleTop + (Printer.ScaleHeight - PrintHeight) / 2
Printer.PaintPicture FormToPrint(Form1), PrintX, PrintY, PrintWidth, PrintHeight
Printer.EndDoc
I tested it with my pc in landscape and portrait and (for me) it worked out really good and centered!
-
Oct 18th, 2014, 07:17 PM
#22
Thread Starter
PowerPoster
Re: [RESOLVED] make a forms width fit printout
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|