PDA

Click to See Complete Forum and Search --> : Copy file into Clipboard


Olly
May 26th, 2001, 02:44 PM
How do I copy a file using the api into the clipboard, so that i
can paste ist later into an application? (for example a picture).
And how can I delete it from the clipboard?

Thank you!

Vlatko
May 26th, 2001, 02:59 PM
Here is an example (not a file):

Const LR_LOADFROMFILE = &H10
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const IMAGE_CURSOR = 2
Const IMAGE_ENHMETAFILE = 3
Const CF_BITMAP = 2
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Sub Form_Load()
Dim hDC As Long, hBitmap As Long
'Load the bitmap into the memory
hBitmap = LoadImage(App.hInstance, "c:\windows\logow.sys", IMAGE_BITMAP, 320, 200, LR_LOADFROMFILE)
If hBitmap = 0 Then
MsgBox "There was an error while loading the bitmap"
Exit Sub
End If
'open the clipboard
OpenClipboard Me.hwnd
'Clear the clipboard
EmptyClipboard
'Put our bitmap onto the clipboard
SetClipboardData CF_BITMAP, hBitmap
'Check if there's a bitmap on the clipboard
If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then
MsgBox "There was an error while pasting the bitmap to the clipboard!"
End If
'Close the clipboard
CloseClipboard
'Get the picture from the clipboard
Me.Picture = Clipboard.GetData(vbCFBitmap)
End Sub

Vlatko
May 26th, 2001, 03:00 PM
Oh, and here is the parameter info:

Declare Function SetClipboardData Lib "user32" Alias "SetClipboardDataA" (ByVal wFormat As Long, ByVal hMem As Long) As Long

· uFormat
Specifies a clipboard format. This parameter can be a registered format or any of the standard clipboard formats listed in the following Remarks section. For information about registered clipboard formats, see the RegisterClipboardFormat function.

· hMem
Identifies the data in the specified format. This parameter can be NULL, indicating that the window provides data in the specified clipboard format (renders the format) upon request. If a window delays rendering, it must process the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages.
Once SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked. If the hMem parameter identifies a memory object, the object must have been allocated using the GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags.

Olly
May 26th, 2001, 03:09 PM
thank you :)

Olly
May 26th, 2001, 03:12 PM
But I got a second question. How can I send a paste event to a rich textbox? (probably quite easy).
Is there a property for that or do I really need to use sendkeys to
paste something?

Serge
May 26th, 2001, 10:26 PM
Knowing the hWnd of the RichTextBox is enough to do that.
All you have to do is to send WM_PASTE message to the RichTextBox.
Here is the declaration:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_PASTE = &H302

Here is the code to do it:

Call SendMessage(RichTextHwnd, WM_PASTE, 0, 0)

Regards,

Olly
May 27th, 2001, 04:11 AM
thanx, that's more professional than using

RichTextBox1.SetFocus
SendKeys "^(v)"

:D

And also it's sure that it works.