1 Replies - 493 Views - Last Post: 30 August 2013 - 08:13 AM Rate Topic: -----

#1 ybadragon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,534
  • Joined: 11-May 12

Pause Timer when Minimize

Posted 30 August 2013 - 08:04 AM

I have a program that has a timer and every tick checks the CMYK values of a color underneath the mouse cursor anywhere on the screen, the issue is it takes up quite a bit of CPU when it's running. I set it up now so that the program doesn't try to check the CMYK values when it is minimized, however the timer is still running in the background, and I was wondering how I can pause it. I know how to STOP the timer, but if I do that I don't know how to check if the WindowState changes so when the window is un-minimized, the timer will restart.

this is my timer_tick code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Not Me.WindowState = FormWindowState.Minimized Then
            Dim mouseRange As Rectangle = New Rectangle(MousePosition, New Size(50, 50))
            If MousePosition = lastPos And mouseRange.Contains(lastPos) And Not lastPos = lastReadPos Then
                lastReadPos = lastPos
                Dim myBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
                Dim g As Graphics = Graphics.FromImage(myBmp)
                g.CopyFromScreen(Point.Empty, Point.Empty, myBmp.Size)
                g.Dispose()
                col = Color.FromArgb(myBmp.GetPixel(MousePosition.X, MousePosition.Y).ToArgb())
                If Not prevCol = Color.FromArgb(col.ToArgb()) Then
                    prevCol = Color.FromArgb(col.ToArgb())
                    myBmp.Dispose()
                    R1 = col.R
                    R2 = Format(R1 / 255, "0.0000")
                    G1 = col.G
                    G2 = Format(G1 / 255, "0.0000")
                    B1 = col.B
                    B2 = Format(B1 / 255, "0.0000")
                    K = Format(1 - FindMax(New Double() {R2, G2, B2}), "0.0000")
                    C = Format((1 - R2 - K) / (1 - K), "0.0000")
                    M = Format((1 - G2 - K) / (1 - K), "0.0000")
                    Y = Format((1 - B2 - K) / (1 - K), "0.0000")
                    lblC.Text = C.ToString
                    lblCCol.BackColor = Color.FromArgb(col.A, 0, G1, B1)
                    lblM.Text = M.ToString
                    lblMCol.BackColor = Color.FromArgb(col.A, R1, 0, B1)
                    lblY.Text = Y.ToString
                    lblYCol.BackColor = Color.FromArgb(col.A, R1, G1, 0)
                    lblK.Text = K.ToString
                    Select Case GetMax(R1, G1, B1)
                        Case "R"
                            lblKCol.BackColor = Color.FromArgb(col.A, R1, 0, 0)
                        Case "G"
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, G1, 0)
                        Case "B"
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, 0, B1)
                        Case Else
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, 0, 0)
                    End Select
                    lblR.Text = R1
                    lblRCol.BackColor = Color.FromArgb(col.A, col.R, 0, 0)
                    lblG.Text = G1
                    lblGCol.BackColor = Color.FromArgb(col.A, 0, col.G, 0)
                    lblB.Text = B1
                    lblBCol.BackColor = Color.FromArgb(col.A, 0, 0, col.B)/>
                    lblColor.BackColor = col
                    Me.Invalidate()
                End If
            End If
            lastPos = MousePosition
        End If
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Pause Timer when Minimize

#2 ybadragon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,534
  • Joined: 11-May 12

Re: Pause Timer when Minimize

Posted 30 August 2013 - 08:13 AM

I figured it out, When the form is un-minimized it gains focus, so I can check the Form1_GotFocus handle, and start the timer then my new code

Public Class Form1
    Dim R1 As Integer = 0
    Dim R2 As Double = 0
    Dim G1 As Integer = 0
    Dim G2 As Double = 0
    Dim B1 As Integer = 0
    Dim B2 As Double = 0
    Dim K As Double = 0
    Dim C As Double = 0
    Dim Y As Double = 0
    Dim M As Double = 0
    Dim col As Color = Nothing
    Dim lastPos As Point = Nothing
    Dim lastReadPos As Point = Nothing
    Dim prevCol As Color = Nothing

    Private Function FindMax(ByVal dArray As Double())
        Dim max As Double
        For Each d As Double In dArray
            If d > max Then
                max = d
            End If
        Next
        Return max
    End Function

    Private Function GetMax(ByVal r As Double, ByVal g As Double, ByVal b As Double)
        Dim lstVals As New List(Of Double)
        Dim sCol As String = ""
        lstVals.AddRange(New Double() {r, g, b})
        lstVals.Sort()
        If lstVals(0) = lstVals(1) Then
            Return "K"
        Else
            Select Case True
                Case lstVals(0) = r
                    Return "R"
                Case lstVals(0) = g
                    Return "G"
                Case lstVals(0) = b
                    Return "B"
            End Select
        End If
        Return sCol
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' this is now has an Else clause where the timer is stopped
        If Not Me.WindowState = FormWindowState.Minimized Then
            Dim mouseRange As Rectangle = New Rectangle(MousePosition, New Size(50, 50))
            If MousePosition = lastPos And mouseRange.Contains(lastPos) And Not lastPos = lastReadPos Then
                lastReadPos = lastPos
                Dim myBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
                Dim g As Graphics = Graphics.FromImage(myBmp)
                g.CopyFromScreen(Point.Empty, Point.Empty, myBmp.Size)
                g.Dispose()
                col = Color.FromArgb(myBmp.GetPixel(MousePosition.X, MousePosition.Y).ToArgb())
                If Not prevCol = Color.FromArgb(col.ToArgb()) Then
                    prevCol = Color.FromArgb(col.ToArgb())
                    myBmp.Dispose()
                    R1 = col.R
                    R2 = Format(R1 / 255, "0.0000")
                    G1 = col.G
                    G2 = Format(G1 / 255, "0.0000")
                    B1 = col.B
                    B2 = Format(B1 / 255, "0.0000")
                    K = Format(1 - FindMax(New Double() {R2, G2, B2}), "0.0000")
                    C = Format((1 - R2 - K) / (1 - K), "0.0000")
                    M = Format((1 - G2 - K) / (1 - K), "0.0000")
                    Y = Format((1 - B2 - K) / (1 - K), "0.0000")
                    lblC.Text = C.ToString
                    lblCCol.BackColor = Color.FromArgb(col.A, 0, G1, B1)
                    lblM.Text = M.ToString
                    lblMCol.BackColor = Color.FromArgb(col.A, R1, 0, B1)
                    lblY.Text = Y.ToString
                    lblYCol.BackColor = Color.FromArgb(col.A, R1, G1, 0)
                    lblK.Text = K.ToString
                    Select Case GetMax(R1, G1, B1)
                        Case "R"
                            lblKCol.BackColor = Color.FromArgb(col.A, R1, 0, 0)
                        Case "G"
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, G1, 0)
                        Case "B"
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, 0, B1)
                        Case Else
                            lblKCol.BackColor = Color.FromArgb(col.A, 0, 0, 0)
                    End Select
                    'lblKCol.BackColor = Color.FromArgb(FindMax(New Double() {R1, G1, B1}))
                    lblR.Text = R1
                    lblRCol.BackColor = Color.FromArgb(col.A, col.R, 0, 0)
                    lblG.Text = G1
                    lblGCol.BackColor = Color.FromArgb(col.A, 0, col.G, 0)
                    lblB.Text = B1
                    lblBCol.BackColor = Color.FromArgb(col.A, 0, 0, col.B)/>/>
                    lblColor.BackColor = col
                    Me.Invalidate()
                End If
            End If
            lastPos = MousePosition
        Else
            Timer1.Stop()
        End If
    End Sub

    Private Sub Form1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        ' if the timer isn't started, start it
        If Not Timer1.Enabled Then
            Timer1.Start()
        End If
    End Sub
End Class



This post has been edited by ybadragon: 30 August 2013 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1