Check Clipboard via Timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed Bitzer

    Check Clipboard via Timer

    Appreciate some help. Want to continuously check clipboard and if text
    present unhide and display in a text. The following code can find nothing
    in the clipboard even though I paste information there from Notepad.
    \\
    Public Sub btnStart_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnStart.Click
    Dim t As New System.Timers.T imer(2000)
    AddHandler t.Elapsed, AddressOf TimerFired
    t.Enabled = True
    End Sub
    Public Sub TimerFired(ByVa l sender As Object, ByVal e As
    System.Timers.E lapsedEventArgs )
    Dim d As IDataObject = Clipboard.GetDa taObject()
    If (Clipboard.GetD ataObject.GetDa taPresent(DataF ormats.Text)) Then
    TextBox1.Text =
    Clipboard.GetDa taObject.GetDat a(DataFormats.T ext).ToString()
    End If
    End Sub
    //
    If I debug the TimerFired sub is repeatedly called but there is nothing in
    the Clipboard.

    Ed



  • Ken Tucker [MVP]

    #2
    Re: Check Clipboard via Timer

    Hi,

    You have to use the setclipboardvie wer api to register yourself as a
    clipboard viewer. The api will return the hwnd to the old viewer save that
    value in a form level variable. You will recieve WM_DRAWCLIPBOAR D message
    when the clipboard changes override wndproc to recieve message. Make sure
    you call mybase.wndproc in the override or you app will not work. When
    closing program set the clipboard viewer back to the old one.



    Api declares

    Declare Function SetClipboardVie wer Lib "user32" Alias "SetClipboardVi ewer"
    (ByVal hwnd As Integer) As Integer

    Declare Function ChangeClipboard Chain Lib "user32" Alias
    "ChangeClipboar dChain" (ByVal hwnd As Integer, ByVal hWndNext As Integer) As
    Integer

    In form load or where ever want to start monitoring clipboard

    hWndClipBoard = SetClipboardVie wer(Me.Handle.T oInt32)



    Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
    System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

    ChangeClipboard Chain(Me.Handle .ToInt32, hWndClipBoard)

    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows. Forms.Message)

    Const WM_DRAWCLIPBOAR D = &H308



    If m.Msg = WM_DRAWCLIPBOAR D Then

    MessageBox.Show ("Here")

    End If

    MyBase.WndProc( m)

    End Sub

    Ken
    ------------------
    "Ed Bitzer" <edbitzer@yahoo .com> wrote in message
    news:%23F3kwXyU FHA.544@TK2MSFT NGP15.phx.gbl.. .
    Appreciate some help. Want to continuously check clipboard and if text
    present unhide and display in a text. The following code can find nothing
    in the clipboard even though I paste information there from Notepad.
    \\
    Public Sub btnStart_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnStart.Click
    Dim t As New System.Timers.T imer(2000)
    AddHandler t.Elapsed, AddressOf TimerFired
    t.Enabled = True
    End Sub
    Public Sub TimerFired(ByVa l sender As Object, ByVal e As
    System.Timers.E lapsedEventArgs )
    Dim d As IDataObject = Clipboard.GetDa taObject()
    If (Clipboard.GetD ataObject.GetDa taPresent(DataF ormats.Text)) Then
    TextBox1.Text =
    Clipboard.GetDa taObject.GetDat a(DataFormats.T ext).ToString()
    End If
    End Sub
    //
    If I debug the TimerFired sub is repeatedly called but there is nothing in
    the Clipboard.

    Ed




    Comment

    • Ed Bitzer

      #3
      Re: Check Clipboard via Timer

      Ken, Will start the leaning curve playing with that API. Am very
      surprised that's necessary with net because thought that was the purpose of
      the Clipboard namespace. The following code works great activated by a
      button click - but does not work if activated by the timer..
      \\
      If Clipboard.GetDa taObject.GetDat aPresent(DataFo rmats.Text) Then
      TextBox1.Text =
      Clipboard.GetDa taObject.GetDat a(DataFormats.T ext).ToString()
      End If
      //

      Ed


      Comment

      • Ed Bitzer

        #4
        Re: Check Clipboard via Timer

        Ken
        [color=blue]
        > In form load or where ever want to start monitoring clipboard[/color]
        [color=blue]
        > hWndClipBoard = SetClipboardVie wer(Me.Handle.T oInt32)[/color]

        hWndClipBoard is not declared when I attempt to test - what am I missing?

        Ed


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Check Clipboard via Timer

          "Ed Bitzer" <edbitzer@yahoo .com> schrieb:[color=blue][color=green]
          >> In form load or where ever want to start monitoring clipboard[/color]
          >[color=green]
          >> hWndClipBoard = SetClipboardVie wer(Me.Handle.T oInt32)[/color]
          >
          > hWndClipBoard is not declared when I attempt to test - what am I missing?[/color]

          \\\
          Dim hWndClipBoard As Int32
          ///

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Ed Bitzer

            #6
            Re: Check Clipboard via Timer

            Ken,

            The API certainly works as advertised and does not require the timer that I
            used in a similar VB6 program to pick up a copy - The API picks up the
            "change" when something is copied to the clipboard. I am using this in
            small utility I use myself to copy bits and pieces of code or other
            information, usually gathered from forums, and accumulates is a temporary
            file which I can then edit and often import into a askSam database. I think
            I wrote my first version in Pascal many moons ago, after than in VB. I'm
            retired and trying to keep up with computer technology and writing a bit of
            code, now in VB.Net -- keeps me out of the taverns<g>. I do thank you
            guys.

            Ed



            Comment

            • Ken Tucker [MVP]

              #7
              Re: Check Clipboard via Timer

              Hi,

              This is how I do it. Example is for getting bitmap but should work
              for text.

              Dim data As IDataObject



              data = Clipboard.GetDa taObject()

              If data.GetDataPre sent(GetType(Sy stem.Drawing.Bi tmap)) Then

              bm = CType(data.GetD ata(GetType(Sys tem.Drawing.Bit map)), Image)

              End If



              Ken

              ----------------------------

              "Ed Bitzer" <edbitzer@yahoo .com> wrote in message
              news:%23LHK7c0U FHA.3584@TK2MSF TNGP14.phx.gbl. ..
              Ken, Will start the leaning curve playing with that API. Am very
              surprised that's necessary with net because thought that was the purpose of
              the Clipboard namespace. The following code works great activated by a
              button click - but does not work if activated by the timer..
              \\
              If Clipboard.GetDa taObject.GetDat aPresent(DataFo rmats.Text) Then
              TextBox1.Text =
              Clipboard.GetDa taObject.GetDat a(DataFormats.T ext).ToString()
              End If
              //

              Ed



              Comment

              Working...