Converting a Screen Coordinate to a Time ! Yes Really :)

I need to draw controls against a visual timeline where the duration of the timeline can vary, as can the size of the window in which it is drawn.  So I use the paint event to measure the form width, subtract margins and draw out the duration of the timeline so that it fills the window.

Public Class Viewer
    Dim mFont As New Font("Ariel", 12)
    Dim mMargin As Integer = 40
    Dim mDayStartHour As Integer = 6 'start of the timeline
    Dim mDayEndHour As Integer = 23 'end of the timeline
    Dim mHourStep As Integer 
  
   
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim mDayLine As Integer = Me.Width - (mMargin * 2)
        mHourStep = mDayLine / (mEndHour - mDayStartHour) 'how long is an hour on screen
        Dim mHourDrawPosition As Integer = mMargin
        Dim mOffset As Integer
        Dim mGraphics As Graphics = e.Graphics
        mCurRow = mMargin
        mGraphics.DrawLine(Pens.Black, New Point(mMargin, mMargin), New Point(mMargin + mDayLine, mMargin))
        For i = mDayStartHour To mEndHour
            mOffset = mGraphics.MeasureString(i, mFont).Width / 2
            mGraphics.DrawString(i, mFont, New SolidBrush(Color.Black), New Point(mHourDrawPosition - mOffset, mMargin))
            mHourDrawPosition += mHourStep
        Next

Open in new window


How can I translate the coordinates of a MouseDown into a time that corresponds to the timeline ?

Headfry !!
LVL 1
DodsworthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Add this in and see what happens:
    Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim mDayLine As Integer = Me.Width - (mMargin * 2)
        mHourStep = mDayLine / (mEndHour - mDayStartHour) 'how long is an hour on screen
        Dim hour As Integer = Math.Min(Math.Max(mDayStartHour, mDayStartHour + (e.X - mMargin + mHourStep / 2) \ mHourStep), mEndHour)
        Me.Text = hour
    End Sub

Open in new window

0
DodsworthAuthor Commented:
Yes that gives me the nearest hour, but I need the full hours:minutes:seconds any further thoughts ?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha...here's another approach:
Public Class Form1

    Dim mFont As New Font("Ariel", 12)
    Dim mMargin As Integer = 40
    Dim mDayStartHour As Integer = 6 'start of the timeline
    Dim mDayEndHour As Integer = 23 'end of the timeline
    Dim mHourStep As Integer

    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        Me.Refresh()
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim mDayLine As Integer = Me.Width - (mMargin * 2)
        mHourStep = mDayLine / (mDayEndHour - mDayStartHour) 'how long is an hour on screen
        Dim mHourDrawPosition As Integer = mMargin
        Dim mOffset As Integer
        Dim mGraphics As Graphics = e.Graphics
        mGraphics.DrawLine(Pens.Black, New Point(mMargin, mMargin), New Point(mMargin + mDayLine, mMargin))
        For i = mDayStartHour To mDayEndHour
            mOffset = mGraphics.MeasureString(i, mFont).Width / 2
            mGraphics.DrawString(i, mFont, New SolidBrush(Color.Black), New Point(mHourDrawPosition - mOffset, mMargin))
            mHourDrawPosition += mHourStep
        Next
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim mDayLine As Integer = Me.Width - (mMargin * 2)
        Dim percentage As Double = CDbl(Math.Max(0, Math.Min(e.X - mMargin, mDayLine))) / CDbl(mDayLine)
        Dim TS As New TimeSpan(mDayStartHour, 0, 0)
        TS = TS.Add(TimeSpan.FromSeconds(percentage * TimeSpan.FromHours(mDayEndHour - mDayStartHour).TotalSeconds))
        Me.Text = String.Format("{0}:{1}:{2}", TS.Hours, TS.Minutes.ToString("00"), TS.Seconds.ToString("00"))
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.