get same value for GetDeviceCaps parameters

N

Nancy

BlankFollowing is the code I'm using in an attempt to determine the hard
margins of a printer and adjust the print margins accordingly. I'm only
showing the retrieval of info (not the manipulating) for which I'm getting
the same output value, 8.641474E+12, for the width, height, x, and y
parameters. I have no idea what is wrong. I've been searching for code
examples but haven't found a close example for what I'm trying to do - i.e.
determine these values within the PrintPage routine using VB.Net. Is the
handle creation wrong? Can a IntPtr be converted to Long? Thanks for any
help you can offer!
Nancy

Private Const PHYSICALWIDTH As Long = 110
Private Const PHYSICALHEIGHT As Long = 111
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Dim hdcPtr As IntPtr = e.Graphics.GetHdc
Dim hdcLong As Long = hdcPtr.ToInt64

'All numbers in dpi
Dim XRes As Single = e.PageSettings.PrinterResolution.X
Dim YRes As Single = e.PageSettings.PrinterResolution.Y
Dim hardWT As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALWIDTH))
Dim hardHT As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALHEIGHT))
Dim hardX As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALOFFSETX))
Dim hardY As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALOFFSETY))
'Dim hardXRes As Single = GetDeviceCaps(hdcLong, HORZRES)

e.Graphics.ReleaseHdc(hdcPtr)

Debug.WriteLine("*** Printer Hard Margins: ***" & Chr(13) & _
"Width = " & hardWT.ToString & " Height = " & hardHT.ToString &
Chr(13) & _
"X = " & hardX.ToString & " Y = " & hardY.ToString & Chr(13) & _
"X Res = " & XRes.ToString & " Y Res = " & YRes.ToString)

End Sub
 
H

Herfried K. Wagner [MVP]

* "Nancy said:
Private Const PHYSICALWIDTH As Long = 110
Private Const PHYSICALHEIGHT As Long = 111
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

All the 'As Long' in the code above should be 'As Int32'.
Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Dim hdcPtr As IntPtr = e.Graphics.GetHdc
Dim hdcLong As Long = hdcPtr.ToInt64

'hdcLong' should be 'IntPtr', or in other words, you don't need this variable.
'All numbers in dpi
Dim XRes As Single = e.PageSettings.PrinterResolution.X
Dim YRes As Single = e.PageSettings.PrinterResolution.Y
Dim hardWT As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALWIDTH))
Dim hardHT As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALHEIGHT))
Dim hardX As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALOFFSETX))
Dim hardY As Single = ToSingle(GetDeviceCaps(hdcLong, PHYSICALOFFSETY))
'Dim hardXRes As Single = GetDeviceCaps(hdcLong, HORZRES)

Post your declare of 'GetDeviceCaps'.

- or -

Have a look at my FAQ ;-):

Sample is based on a C# sample written by Ron Allen.

The method 'GetHardMargins' expects a hDC to the printer's 'Graphics'
object during the print call. The printer may have a margin that is
different from the PrintPreViewDialog's margin. Values are returned in
the procedure's parameters:

\\\
Public Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex 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
///
 
A

Armin Zingler

Nancy said:
BlankFollowing is the code I'm using in an attempt to determine the
hard margins of a printer and adjust the print margins accordingly.
I'm only showing the retrieval of info (not the manipulating) for
which I'm getting the same output value, 8.641474E+12, for the width,
height, x, and y parameters. I have no idea what is wrong. I've been
searching for code examples but haven't found a close example for
what I'm trying to do - i.e. determine these values within the
PrintPage routine using VB.Net. Is the handle creation wrong? Can a
IntPtr be converted to Long?

No, but to an Integer. Your declarations are probably for the wrong
language, not for VB.Net. Please post the declaration of GetDeviceCaps.
Private Const PHYSICALWIDTH As Long = 110
Private Const PHYSICALHEIGHT As Long = 111
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

Replace Long by Integer.
Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Dim hdcPtr As IntPtr = e.Graphics.GetHdc
Dim hdcLong As Long = hdcPtr.ToInt64

Change it to
Dim hdcLong As Integer = hdcPtr.ToInt32
but you probably don't need it at all if you declare the first argument of
GetDeviceCaps As IntPtr.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
N

Nancy

Thanks for your help Herfried and Armin!! I made your suggested changes
(changed Long to Int32 and used hdc as IntPtr) and it works! Used the same
function declaration as below.

Best Regards,
Nancy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top