Thanks,
Eric
"Eric" <pae...@mcsteen.com> schrieb:
> Does anyone have an example (in vb.net code, not c#) of using
> GetDeviceCaps to determine the hard margins of a particular printer?
(untested, sample based on a C# sample by Ron Allen)
<cite>
Get the hDc from the printer Graphics object during the print call. The
reason I suggest this is that the printer driver for the selected
printer may have a margin that is different from the PrintPreviewDialog
margin.
</cite>
\\\
Public Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hDc As IntPtr, _
ByVal funct As Int32 _
) As Int32
Private Const PHYSICALOFFSETX As Int32 = 112
Private Const PHYSICALOFFSETY As Int32 = 113
Private Const HORZRES As Int32 = 8
Private Const VERTRES As Int32 = 10
Private Const HORZSIZE As Int32 = 4
Private Const VERTSIZE As Int32 = 6
Public Sub GetHardMargins( _
ByVal hDC As IntPtr, _
ByRef Left As Single, _
ByRef Top As Single, _
ByRef Right As Single, _
ByRef Bottom As Single _
)
Dim offx As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETX))
Dim offy As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETY))
Dim resx As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, HORZRES))
Dim resy As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, VERTRES))
Dim hsz As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, HORZSIZE)) / 25.4F ' Screen
width in inches.
Dim vsz As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, VERTSIZE)) / 25.4F ' Screen
height in inches.
Dim ppix As Single = resx / hsz
Dim ppiy As Single = resy / vsz
Left = (offx / ppix) * 100.0F
Top = (offy / ppix) * 100.0F
Bottom = Top + (vsz * 100.0F)
Right = Left + (hsz * 100.0F)
End Sub
///
Regards,
Herfried K. Wagner
One issue regarding the right and bottom margins.
When I manually set the minimum margins on a printer through print
preview to all zero, the printer returns what I think are it's hard
margins.
When I subtract right and bottom margins returned from the
GetHardMargins(), from the paper width and height respectively, they
are off slightly.
For example, my Laser's apparent hard margins are .166, .166, .166,
.166 for left, right, top and bottom, when I set them to all zeroes
through the Print Preview dialog, for an 8.5 by 11 inch print page.
The left and top values exactly match what is returned from
GetHardMargins for the device handle for the Laser.
The bottom from GetHardMargins returns 1083.5 which when subtracted
from 1100.0 yields .165 which is very close close.
The right from GetHardMargins returns 831.6 which when subtracted from
850.0 returns .184, which is off slightly. It baffles me.
Anyways, it's close enough for my purposes. Thanks again for your
help.
Eric
"Herfried K. Wagner" <hirf...@m.activevb.de> wrote in message news:<OQwPH6b6...@TK2MSFTNGP12.phx.gbl>...