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] >-----Original Message-----
>Mynfred,
>Looking at your code briefly, you do have a number of
issues, in addition to
>my earlier CreateGraphics suggestion.
>
>> Dim nWidth As Integer = Me.Width
>> Dim nHeight As Integer = Me.Height
>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.
>
>> 'g.PageUnit = GraphicsUnit.Millimeter
>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
>
>"Mynfred" <anonymous@discussions.microsoft.com> wrote in
message
>news:07a101c3ae80$3ae1a860$a101280a@phx.gbl...
>> 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 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
>>