locked
Measuring Sizes? RRS feed

  • Question

  • Hello,

    I was wondering how you create the measurement toolbar at the top of many text editing packages, where it displays it in CM/ Inches and you can configure it etc.

    Regards,

    Mitch.
    Friday, August 22, 2008 3:13 AM

Answers

  • Graphics are measured in "pixels" (sometimes called "dots").  To set up a ruler you will need to know how many pixels there are in an inch (or centimeter).  Typically this will be "96", but you should always let the operating system give you the correct value.  In VB the statement would be:

    Dim iDPIX As Integer = CType(Me.CreateGraphics.DpiX, Int32)

     

    Once you have this value you can go ahead and build your ruler.  To put marks at one inch intervals, just draw verticle lines at "iDPIX' intervals.  For eighth inch intervals use "iDPIX \ 8".  Beware of propagating a rounding error.  For example, to calculate 7/8 of an inch use "7 * iDPIX \ 8" instead of "iDPIX \ 8 * 7".

     

    To switch from inches to centimeters you would just convert "iDPIX" to "iDPIC" and then redraw your ruler.

    Dim iDPIC = CType(iDPIX / 2.54, Int32)

    Friday, August 22, 2008 1:17 PM

All replies

  • Graphics are measured in "pixels" (sometimes called "dots").  To set up a ruler you will need to know how many pixels there are in an inch (or centimeter).  Typically this will be "96", but you should always let the operating system give you the correct value.  In VB the statement would be:

    Dim iDPIX As Integer = CType(Me.CreateGraphics.DpiX, Int32)

     

    Once you have this value you can go ahead and build your ruler.  To put marks at one inch intervals, just draw verticle lines at "iDPIX' intervals.  For eighth inch intervals use "iDPIX \ 8".  Beware of propagating a rounding error.  For example, to calculate 7/8 of an inch use "7 * iDPIX \ 8" instead of "iDPIX \ 8 * 7".

     

    To switch from inches to centimeters you would just convert "iDPIX" to "iDPIC" and then redraw your ruler.

    Dim iDPIC = CType(iDPIX / 2.54, Int32)

    Friday, August 22, 2008 1:17 PM
  • Nice, thank you.

    Now all i have to do is find some code on drawing lines etc - great answer thanks again.

    Regards,

    Mitch.
    Friday, August 22, 2008 6:40 PM