It is currently Tue Jul 14, 2020 10:03 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Clipboard Listener [Windows]
PostPosted: Fri Sep 29, 2017 5:24 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3650
Run the snippet then copy any registered format to the clipboard
PB 5.61 x86 Windows 10 x64

Code:
Global cblistener,cbfunction,editor,txt,combo,combo2,img

#WM_CLIPBOARDUPDATE=797

Procedure registerClipFormats()
    Global cf_Text=1
    Global cf_Bitmap=2
    Global CF_ENHMETAFILE   = 14
    Global cf_metafilepict=3
    Global CF_DIB=8
    Global CF_DIBV5=17
    Global CF_JPEG=19
    Global CF_RIFF=11
    Global CF_TIFF=6
    Global CF_SYLK=4
    Global cf_RTF = RegisterClipboardFormat_("Rich Text Format")
    Global cf_html = RegisterClipboardFormat_("HTML Format")
    Global cf_Ignore = RegisterClipboardFormat_("Clipboard Viewer Ignore")
    Global cf_sequence = GetClipboardSequenceNumber_()
EndProcedure

Procedure StreamCB(dwCookie, pbBuff, cb, pcb)
  If cb
    plTxt$ + PeekS(pbBuff, cb, #PB_Ascii)
  EndIf
  pcb = cb
EndProcedure

Procedure enumFormat()
  cstr.s=Space(32)
  i = 0
  While OpenClipboard_(0) = 0
    EnumSW = 1
    i +1
    If i > 50
      CloseClipboard_()
      ProcedureReturn 0
    EndIf
  Wend
  j=0
  j = EnumClipboardFormats_(0)
  While j<>0
    j = EnumClipboardFormats_(j)
  Wend
  CloseClipboard_()
  ProcedureReturn 1
EndProcedure

Procedure winCB(hWnd,uMsg,wParam,lParam)
  Result=#PB_ProcessPureBasicEvents
  Select uMsg 
    Case #WM_CLIPBOARDUPDATE
        If GetClipboardSequenceNumber_() = cf_sequence
            ProcedureReturn Result
        Else
            cf_sequence = GetClipboardSequenceNumber_()
        EndIf
        enumFormat()
        If OpenClipboard_(0)
          item = GetGadgetState(combo)
          format = GetGadgetItemData(combo,item)
          If IsClipboardFormatAvailable_(format)
            hObject = GetClipboardData_(format)
            If format = 2 Or format = 3 Or format = 6 Or format = 8 Or format = 14 Or format = 17 Or format = 19
              SetGadgetState(img,hObject)
            Else
              ClearGadgetItems(editor)
              SetGadgetText(txt,"")
              If (GetGadgetState(combo) = 3 And GetGadgetState(combo2) = 1) Or GetGadgetState(combo) = 0
                SetGadgetText(editor,PeekS(hObject,-1,#PB_UTF8))
              ElseIf GetGadgetState(combo) = 3 And GetGadgetState(combo2) = 0
                SetGadgetText(txt,PeekS(hObject,-1,#PB_UTF8))
              ElseIf GetGadgetState(combo) = 4
                SetGadgetText(editor,PeekS(hObject,-1,#PB_UTF8))
              EndIf
            EndIf         
          EndIf 
          CloseClipboard_()
        EndIf 
    ProcedureReturn Result
  EndSelect
  ProcedureReturn Result
EndProcedure

hwnd = OpenWindow(#PB_Any,0,0,800,600,"Clipboard Listener",#PB_Window_SystemMenu| #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  tab = PanelGadget(#PB_Any, 10,10,600,580)
    AddGadgetItem (tab, -1, "Editor")
    editor = EditorGadget(#PB_Any,0,0,600,285,#PB_Editor_WordWrap)
    txt = TextGadget(#PB_Any,0,290,600,280,"",#WS_DLGFRAME)
    AddGadgetItem (tab, -1, "Image")
    img  = ImageGadget(#PB_Any,0,0,600,580,0,#WS_DLGFRAME)
  CloseGadgetList()
  combo = ComboBoxGadget(#PB_Any,620,30,170,24)
  combo2 = ComboBoxGadget(#PB_Any,620,60,170,24)
  AddGadgetItem(combo2, -1, "Plain_Text")
  AddGadgetItem(combo2, -1, "RTF_Text")
  SetGadgetState(combo2,1)
  DisableGadget(combo2,1)
   
  lib = OpenLibrary(#PB_Any,"user32.dll")
  If lib And OSVersion() > #PB_OS_Windows_XP
      cbFunction = GetFunction(lib,"AddClipboardFormatListener")
      If cbFunction
        cblistener = CallFunctionFast(cbFunction, WindowID(hwnd))
        If cblistener = 0
          MessageRequester("Error","NO clipboardlistener set "+Str(GetLastError_()),#MB_OK|#MB_ICONERROR)
        EndIf
        registerClipFormats()
      EndIf
  Else
    MessageRequester("Error","Could not open lib",#MB_OK|#MB_ICONERROR)
    End
  EndIf
 
  AddGadgetItem(combo, -1, "CF_Text")
  SetGadgetItemData(combo, 0, CF_Text)
  AddGadgetItem(combo, -1, "CF_Bitmap")
  SetGadgetItemData(combo, 1, CF_Bitmap)
  AddGadgetItem(combo, -1, "CF_metafilepict")
  SetGadgetItemData(combo, 2, CF_metafilepict)
  AddGadgetItem(combo, -1, "CF_RTF")
  SetGadgetItemData(combo, 3, CF_RTF)
  AddGadgetItem(combo, -1, "CF_html")
  SetGadgetItemData(combo, 4, CF_html)
  SetGadgetState(combo,4)
 
  SetWindowCallback(@winCB())
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case combo
          If GetGadgetState(combo) = 3
            DisableGadget(combo2,0)
          Else
            DisableGadget(combo2,1)
          EndIf
      EndSelect
  EndSelect
Until Quit = 1

If cblistener <> 0
   If lib
    cbFunction = GetFunction(lib,"RemoveClipboardFormatListener")
    If cbFunction
        CallFunctionFast(cbFunction, WindowID(hwnd))
    EndIf
    CloseLibrary(lib)
   EndIf
EndIf

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Clipboard Listener [Windows]
PostPosted: Fri Sep 29, 2017 8:27 am 
Offline
Moderator
Moderator
User avatar

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 1112
Location: Gernsbach (Germany)
Thank you for sharing but if I change the combobox for CF format then nothing happens.
I always have to select before. Can you add change-event?

_________________
ImageImageImageImage Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Clipboard Listener [Windows]
PostPosted: Tue Oct 03, 2017 3:01 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4692
Location: Lyon - France
Works for me, but it's not automatic :D
I choose a format, and copy something in the format i have choose and that appear in the tab
Just MetafilPict i can't test, because i don't know how :oops:
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye