Hooking the windows clipboard?

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Hooking the windows clipboard?

Post by jassing »

Has anyone done any work with regards to hooking the windows clipboard (and willing to share)?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Hooking the windows clipboard?

Post by netmaestro »

Personally, I've never done it but if I were to need it at some point, I think I'd start here: http://www.delphidabbler.com/articles?article=9
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Hooking the windows clipboard?

Post by srod »

Yes it is straight forward enough; registering one of your windows as a clipboard viewer. I am not on my development machine right now so cannot yank out any code, but once you have registered your window you just have to look out for and respond to a couple of Windows messages, checking the available clipboard formats as and when appropriate.
I may look like a mule, but I'm not a complete ass.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Hooking the windows clipboard?

Post by jassing »

Thank you! That's helpful -- much easier reading than examples I found.
Thanks srod, for the info.
-j
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Hooking the windows clipboard?

Post by rsts »

This might be a start

Code: Select all

nextviewer=SetClipboardViewer_(WindowID(#window_0)) ; So we are informed of clipboard changes.
SetWindowCallback(@windowCallback())

; in windows callback
Case #WM_DRAWCLIPBOARD 
;process the TEXT format
   If OpenClipboard_(0) 
      If IsClipboardFormatAvailable_(#CF_TEXT) <> 0
        hStrPtr.l = GetClipboardData_(#CF_TEXT)
        If hStrPtr
          text$= PeekS(hStrPtr)
        endif
      endif
 ;other formats?
  endif
CloseClipboard_()
SendMessage_(nextviewer,message,wParam,lParam); free the clipboard
          ProcedureReturn Result
might also see EnumClipboardFormats_
and IsClipboardFormatAvailable_

There are some complete examples in the forums, I believe, but this may get you started.

cheers
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hooking the windows clipboard?

Post by MachineCode »

Hooking never worked for me on some PCs I used at work, so I gave up relying on it. Just a warning. Setting a clipboard viewer and then forwarding the messages on is not going to work on all PCs, trust me. I ended up just using my own clipboard monitoring routine instead.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Hooking the windows clipboard?

Post by utopiomania »

I had the same question. Thanks, rsts!
Post Reply