473,508 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get same value for GetDeviceCaps parameters

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
Nov 20 '05 #1
3 3751
* "Nancy" <np***@systek.com> scripsit:

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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
"Nancy" <np***@systek.com> schrieb
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

Nov 20 '05 #3
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2i************@uni-berlin.de...
* "Nancy" <np***@systek.com> scripsit:

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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3155
by: Joey | last post by:
Hi There, I am trying to get the selected value of a listbox when I click a button, everything works ok and I can bind the list and when I have a basic page and click a button to invoke a sub it...
6
2254
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
2
1995
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to...
10
1841
by: ryan.mclean | last post by:
Hi all, I am new to using sql server and parameterized sql. I am hoping to be returned the value of a column that has been inserted. Here is my statement strSqlInsetrtTrack = _ "INSERT INTO...
3
2338
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
1
2323
by: Phil Mc | last post by:
Trying to call a stored proc but some times don't want to have values inserted in some fields. Hi I am rewriting a VBS script which called a stored proc in a SQL server db. The proc takes a...
2
2609
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
2
2312
by: Phill W. | last post by:
From what I've been reading, the code below /should/ pop up the Users chosen Font Size (yes; I'm having to cross sword with "Large Fonts" some ... person .. decided to recommend it to our users). ...
0
4566
by: AxleWacl | last post by:
Hi, The below error is what I am receiving. The code im using is below the error, for the life of me, I can not see where any parameter is missing..... Server Error in '/FleetcubeNews'...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.