Determining Ruler's Central Position On Different Screen Widths RRS feed

  • Question

  • Hello.

    I have this question posted as a response to another thread of mine but wanted to post it as a new question since it's really a separate issue that I have.
    The original post is here

    My rulers are created dynamically depending on the width of the screen. They are twice the width of the screen with inch number markings ranging from for e.g. -17 through 0 through +17 (if the screen width is 1600). So the ruler has -17   -16   -15 ....0.... 15    16    17 marking the position of each inch.

    I have a window with a horizontal ruler across the top of the window and a vertical ruler along the left side of the window. The window that the rulers are on has a Canvas (DesignCanvas) that has a ScrollViewer (MyScrollViewer). Above MyScrollViewer the horizontal ruler is placed whereby the 0 marking on the ruler is above the ScrollViewer aligned with it's top left side.
    The vertical ruler is to the left of the ScrollViewer with the 0 marking on the ruler also aligned with it's top left side. I can position my horizontal and vertical rulers correctly above the ScrollViewer with the center of the rulers (0) positioned at the top left.

    The problem is that when it's run on a screen with a different width the ruler center position is off a bit (5/16") for both horizontal and vertical rulers.

    Here's the code I'm using to determine the offset positioning of the rulers.

    _rulerCenterXOffset = SystemParameters.FullPrimaryScreenWidth - MyScrollViewer.HorizontalOffset - DesignCanvas.Margin.Left  
    _rulerCenterYOffset = SystemParameters.FullPrimaryScreenWidth - MyScrollViewer.VerticalOffset - DesignCanvas.Margin.Top 

    I thought that by using the SystemParameters.FullPrimaryScreenWidth it would allow for different screen widths.


    Wednesday, March 18, 2009 10:22 PM


  • So, I finally got this figured out!
    I knew that I was off 5/16" and that my (SystemParameters.FullPrimaryScreenWidth / 96) (to get the width in inches) resulted in a double value with decimals.
    I determined the difference between the rounded screen width and the true screen width (with decimals).
    I then multiplied that by the number of screen width inches (rounded) to get the total difference.
    I finally multipled that by 6 since there are 6 pixels per 1/16".

    Here's my code that determined how much was lost in the screen width rounding calculation.

       Private Sub SetRulerPositionOffset()  
            Dim screenWidthRounding As Double = ScreenWidthRounding()  
            _rulerCenterXOffset = SystemParameters.FullPrimaryScreenWidth - ScrollViewer.HorizontalOffset - DesignCanvas.Margin.Left + screenWidthRounding  
            _rulerCenterYOffset = SystemParameters.FullPrimaryScreenWidth - ScrollViewer.VerticalOffset - DesignCanvas.Margin.Top - screenWidthRounding  
        End Sub  
        Private Function ScreenWidthRounding() As Double  
            Dim actualWidthInInches As Double = SystemParameters.FullPrimaryScreenWidth / 96  
            Dim roundedWidthInInches As Double = Math.Round(SystemParameters.FullPrimaryScreenWidth / 96)  
            Dim widthDifference As Double = roundedWidthInInches - actualWidthInInches  
            Dim widthRounding As Double = widthDifference * roundedWidthInInches * 6                  ' 6 pixels per 1/16 of an inch  
            Return widthRounding  
        End Function 

    This doesn't seem a very eloquent solution but it works for me.
    If there is another way to tackle this issue I'd sure like to hear about it.



    • Marked as answer by ritagreen Thursday, March 19, 2009 6:18 PM
    Thursday, March 19, 2009 6:17 PM