Copy file to the Clipboard

Just starting out? Need help? Post your questions and find answers here.
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Copy file to the Clipboard

Post by Marco2007 »

Hi,

is there any chance to copy a file to the clipboard, then runprogram("word.doc") and just press ctrl-v to paste the file?

just like that:

Image

I originally posted that on the German forum: http://www.purebasic.fr/german/viewtopic.php?t=14603

thanx
Marco
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I've pieced this together from various sources (Google). It's a long shot but it might be worth a try. I don't have Word here so I can't say if it works or not with the #CF_HDROP format. It does NOT work with WordPad nor OpenOffice. :(

(It DOES work for pasting a file into Explorer :) )

Code: Select all

Procedure CopyFile2ClipBoard(file.s)
  clipFile = 0
  If OpenClipboard_(0)
    EmptyClipboard_()
    hGlobal = GlobalAlloc_(#GHND, SizeOf(DROPFILES) + Len(file)  + 2)
    If hGlobal
      *lpGlobal.DROPFILES = GlobalLock_(hGlobal)
      ZeroMemory_(*lpGlobal, SizeOf(DROPFILES))
      *lpGlobal\pFiles = SizeOf(DROPFILES)
      *lpGlobal\fWide = 0
      *lpGlobal\fNC = 0
      *lpGlobal\pt\x = 0
      *lpGlobal\pt\y = 0
      CopyMemory_((*lpGlobal + SizeOf(DROPFILES)), @file, Len(file) + 2)
      GlobalUnlock_(hGlobal)
      If SetClipboardData_(#CF_HDROP, hGlobal)
        ;SetClipboardData_(#CF_TEXT, file)
        clipFile = #True
      EndIf
    EndIf
    CloseClipboard_()
  EndIf
  ProcedureReturn clipFile
EndProcedure

file2copy.s = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0)  
If file2copy
  CopyFile2ClipBoard(file2copy)
  ;RunProgram("Wordpad")
EndIf

            
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

:shock: :!: :!: :!: :!: :!: :shock:

Thanx a lot, sparkie!!! It works with Word 2007!! :D
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

You're welcome....... I guess we got lucky with that one 8)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

Hi Sparkie,

sorry for being nasty again. :oops:

Do you know, what I have to do to get this one work

Code: Select all

CopyFileFromClipboard(target.s)
?

thanx
marco
PureBasic for Windows
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Here's another way to do it:

Code: Select all

Procedure CopyFile2ClipBoard(file.s)
Protected clipFile = 0
Protected *mem, fil
Protected fez = FileSize(file)
If fez>0
  *mem = AllocateMemory(fez+2)
  fil = ReadFile(#PB_Any, file)
  clipfile = ReadData(fil, *mem, fez)
  CloseFile(fil)
  SetClipboardText(PeekS(*mem, fez))
  FreeMemory(*mem)
EndIf
ProcedureReturn clipfile
EndProcedure

Define file2copy.s
file2copy = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0)
If file2copy
  CopyFile2ClipBoard(file2copy)
  RunProgram("Wordpad")
EndIf
End
Anthony Jordan
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Post by ar-s »

Thanks for that cool procedure :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Here's another way to do it

Sorry, akj, but your way doesn't work for me. I copy an exe file with it,
but I can't paste it in Explorer like Sparkie's code does. Don't know why.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

PB wrote:> Here's another way to do it

I can't paste it in Explorer like Sparkie's code does. Don't know why.
maybe setClipboardText has something to do with it?

Peeks isn't going to handle an exe.

cheers
Post Reply