all groups > vb.net upgrade > november 2003 >
You're in the

vb.net upgrade

group:

A question about size unit!


A question about size unit! Mynfred
11/19/2003 1:33:46 AM
vb.net upgrade:
A question about size unit!
Hi, all,

I want to draw a ruler on my form, and I want to display
it in mm unit, my code as following, but it doesn't work
well, can anyone point my fault or give me some advice.thx!

-----------------------------------------------------------
------------------------- Dim nWidth As Integer = Me.Width
Dim nHeight As Integer = Me.Height
Dim Sincr As Single, RScale As Integer
Dim g As System.Drawing.Graphics
g = MyBase.CreateGraphics()

'g.PageUnit = GraphicsUnit.Millimeter
Dim fDpiX As Single = g.DpiX
Dim fDpiY As Single = g.DpiY

'the width in mm unit
Dim fWidthMMUnit As Single = nWidth / fDpiX * 25.4
'the width in cm unit
Dim nSegmentsX As Integer = fWidthMMUnit / 10 + 1
'the width in mm unit
Dim fHeigtMMUnit As Single = nHeight / fDpiY * 25.4
'the width in cm unit
Dim nSegmentsY As Integer = fHeigtMMUnit / 10 + 1


'points in per millimeter
Dim fDpmmX As Integer = fDpiX / 25.4
Dim fDpmmY As Integer = fDpiY / 25.4

Dim penBlack As New Pen(Color.Black, 1)

Dim nCMIndex As Integer = 0
Dim nMMIndex As Integer = 0

Dim RulerWidth As Integer = Me.Height - 10
Dim x As Integer = 0
Dim y As Integer = 0
Dim nLineLength As Integer = 20 'ruler marker line length
Dim xText, yText As Single
Dim SF As New StringFormat
SF.FormatFlags = StringFormatFlags.DirectionVertical

Dim yLength As Integer = nLineLength
Dim xOffset As Integer = 0
RulerWidth = Me.Height

g.DrawLine(penBlack, StartPoint.X, RulerWidth - yLength,
StartPoint.X, RulerWidth)

xText = StartPoint.X + 1
yText = RulerWidth - nLineLength * 1.3
g.DrawString((nCMIndex).ToString, Me.Font, Brushes.Black,
xText, yText)

Do While nCMIndex < nSegmentsX
For nMMIndex = 1 To 10 '1cm = 10mm
x = StartPoint.X + nMMIndex * fDpmmX
xOffset = nCMIndex * 10 * fDpmmX
x += xOffset
If nMMIndex = 10 Then
yLength = nLineLength
xText = x + 1
yText = RulerWidth - nLineLength * 1.3
g.DrawString((nCMIndex + 1).ToString, Me.Font,
Brushes.Black, xText, yText)
Else
If nMMIndex = 5 Then
yLength = nLineLength * 0.75
Else
yLength = nLineLength * 0.5
End If
End If

g.DrawLine(penBlack, x, RulerWidth - yLength, x,
RulerWidth)
Next
nCMIndex += 1
Loop
-----------------------------

mynfred

Re: A question about size unit! Jay B. Harlow [MVP - Outlook]
11/19/2003 8:15:30 AM
Mynfred,
What doesn't work well about it? (Note I have not tried your code yet).

Rather then calling CreateGraphics you should put this code in the Paint
event of the form or Control you want to paint on, then you can us the
e.Graphics object that is part of the EventArgs object to the Paint Event.

If you do call CreateGraphics, be very certain to call Graphics.Dispose when
you are done with it.

[quoted text, click to view]
This should be all you need to do, you should not need to bother Dpi to
millimeters...

Charles Petzold's book "Programming Microsoft Windows With Microsoft Visual
Basic.NET" from MS Press has a sample of drawing a Ruler on the screen.

Hope this helps
Jay


[quoted text, click to view]

Re: A question about size unit! Jay B. Harlow [MVP - Outlook]
11/19/2003 9:45:21 AM
Mynfred,
Looking at your code briefly, you do have a number of issues, in addition to
my earlier CreateGraphics suggestion.

[quoted text, click to view]
You are getting the width & height of the outside of the form, not the
client area of the form. Use Me.ClientSize here instead, which has a Width &
Height property. The ClientSize is the space you are allowed to draw on.

[quoted text, click to view]
This line is commented out, if you expect to draw in Millimeters, then you
cannot have this line commented out.

I would suggest you site down and compare this with Petzold's example and
see where your differences lie.

Hope this helps
Jay

[quoted text, click to view]

Re: A question about size unit! mynfred
11/19/2003 7:19:10 PM
Jay,

Thank you for your advice first, But my problem now is
this book for me unavailable, what's more, if I set the
pageunit to milimeter, I cann't display the line for
unknown reason according my code, maybe because my
horizontal ruler only 2cm height, the unit is too larger
to draw it.
I think implement it by getting the DpiX and DpiY of
graphics first, then calculate the dots number of each
milimeter, and draw it according this number of dots, how
about this method? but this doesn't work well, maybe there
are bugs in my code. So, if possible, would you like post
the code about("Programming Microsoft Windows With
Microsoft Visual Basic.NET" from MS Press has a sample of
drawing a Ruler on the screen) fir me?
MoreOver, according msdn said, the DpiX and DpiY is the
resolution of graphics, but the value I got of DpiX and
DpiY are same, is it right? I try to draw a vertical ruler
like the code I post, to my confused, the spacing of two
closer lines of horizontal and vertical are diffrent. I
post the code as following:

-----------------------------------------
Public Sub DrawRuler()
Dim nWidth As Integer = Me.ClientSize .Width
Dim nHeight As Integer = Me.ClientSize .Height
Dim Sincr As Single, RScale As Integer
Dim g As System.Drawing.Graphics
g = MyBase.CreateGraphics()

'g.PageUnit = GraphicsUnit.Millimeter
Dim fDpiX As Single = g.DpiX
Dim fDpiY As Single = g.DpiY

'the width in mm unit
Dim fWidthMMUnit As Single = nWidth / fDpiX * 25.4
'the width in cm unit
Dim nSegmentsX As Integer = fWidthMMUnit / 10 + 1
'the width in mm unit
Dim fHeigtMMUnit As Single = nHeight / fDpiY * 25.4
'the width in cm unit
Dim nSegmentsY As Integer = fHeigtMMUnit / 10 + 1


'points in per millimeter
Dim fDpmmX As Integer = fDpiX / 25.4
Dim fDpmmY As Integer = fDpiY / 25.4
Dim penBlack As New Pen(Color.Black, 1)

g.PageUnit = GraphicsUnit.Pixel

Dim nCMIndex As Integer = 0
Dim nMMIndex As Integer = 0

Dim RulerWidth As Integer = Me.Height - 10
Dim x As Integer = 0
Dim y As Integer = 0
Dim nLineLength As Integer = 20 'ruler marker
line length
Dim xText, yText As Single
Dim SF As New StringFormat
SF.FormatFlags =
StringFormatFlags.DirectionVertical

If Me.m_nRulerType = RulerType.HORIZONTALRULER Then
Dim yLength As Integer = nLineLength
Dim xOffset As Integer = 0
RulerWidth = Me.Height

g.DrawLine(penBlack, StartPoint.X, RulerWidth -
yLength, StartPoint.X, RulerWidth)

xText = StartPoint.X + 1
yText = RulerWidth - nLineLength * 1.3
g.DrawString((nCMIndex).ToString, Me.Font,
Brushes.Black, xText, yText)

Do While nCMIndex < nSegmentsX
For nMMIndex = 1 To 10 '1cm = 10mm
x = StartPoint.X + nMMIndex * fDpmmX
xOffset = nCMIndex * 10 * fDpmmX
x += xOffset
If nMMIndex = 10 Then
yLength = nLineLength
xText = x + 1
yText = RulerWidth - nLineLength *
1.3
g.DrawString((nCMIndex +
1).ToString, Me.Font, Brushes.Black, xText, yText)
Else
If nMMIndex = 5 Then
yLength = nLineLength * 0.75
Else
yLength = nLineLength * 0.5
End If
End If

g.DrawLine(penBlack, x, RulerWidth -
yLength, x, RulerWidth)
Next
nCMIndex += 1
Loop
Else
Dim xLength As Integer = nLineLength
Dim yOffset As Integer = 0
RulerWidth = Me.Width
g.DrawLine(penBlack, RulerWidth - xLength,
StartPoint.Y, RulerWidth, StartPoint.Y)

xText = RulerWidth - nLineLength * 1.5
yText = StartPoint.Y
g.DrawString((nCMIndex).ToString, Me.Font,
Brushes.Black, xText, yText, SF)

Do While nCMIndex < nSegmentsY
For nMMIndex = 1 To 10 '1cm = 10mm
y = StartPoint.Y + nMMIndex * fDpmmY
yOffset = nCMIndex * 10 * fDpmmY
y += yOffset
If nMMIndex = 10 Then
xLength = nLineLength
xText = RulerWidth - nLineLength *
1.5
yText = y + 1
g.DrawString((nCMIndex +
1).ToString, Me.Font, Brushes.Black, xText, yText, SF)
Else
If nMMIndex = 5 Then
xLength = nLineLength * 0.75
Else
xLength = nLineLength * 0.5
End If
End If

g.DrawLine(penBlack, RulerWidth -
xLength, y, RulerWidth, y)
Next
nCMIndex += 1
Loop
End If

penBlack.Dispose()
g.Dispose()
End Sub
__________________________________________________________

thanks!

mynfred


[quoted text, click to view]
Re: A question about size unit! Jay B. Harlow [MVP - Outlook]
11/21/2003 2:47:00 PM
mynfred,
I'm not sure what else to offer, I find a good book on GDI+ to be invaluable
when trying to do graphics programming in .NET. Petzold's book does a good
job of introducing both GDI+ graphics & Windows Forms. There are a number of
equally valuable books that cover either just GDI+ or just Windows forms.

If you have not, you may consider asking this "down the hall" in the
microsoft.public.dotnet.framework.drawing newsgroup.

Hope this helps
Jay

[quoted text, click to view]
<<snip>>

AddThis Social Bookmark Button