Jump to content

ClipBoard_SetData


Recommended Posts

I want to be able to store clipboard data in my program and retrieve it later. Text works fine, but now I want to get screenshots working.

I made a little example program to show the problem. I want to be able to take a screenshot, and click on the "Pic" button to have it stored there. Then copy some text and have it stored when I click on the "Text" button. Then, if I click on the "Pic" button a second time, it should place back into the clipboard the screenshot. The problem is, when I open up mspaint and try to paste, and error message box pops up saying "Error getting the clipboard data!".

Also, is there an easier way to handle different formats? I tried just _ClipBoard_GetData() and _ClipBoard_SetData($vData), but that didn't seem to work. Am I going to have to determine what type of data is on the clipboard first, then call the get/set functions with the appropriate format? The help for GetData/SetData seem to indicate they will take care of that for me if I can use them correctly.

#include <ClipBoard.au3>
#include <Misc.au3>

opt("GUIOnEventMode",1)


Global $vClip1, $vClip2, $bClip1 = False, $bClip2 = False

$hWnd = GuiCreate("")
$hButton1 = GuiCtrlCreateButton("Pic",5,5,50)
GUICtrlSetOnEvent(-1,"Button1")
$hButton2 = GuiCtrlCreateButton("Text",5,30,50)
GuiCtrlSetOnEvent(-1,"Button2")
GuiSetState()


While 1
    Sleep(10)
WEnd

Func Button1()
; Hold CTRL while clicking button to clear. 
    If _IsPressed("11") Then
        $vClip1 = ""
        GUICtrlSetBkColor($hButton1,0xCCCCCC)
        $bClip1 = False
        Return
    EndIf
    
    If Not $bClip1 Then
        ConsoleWrite("Getting data from clipboard.." & @CRLF)
        $bClip1 = True
        _ClipBoard_Open(0)
        $vClip1 = _ClipBoard_GetData($CF_BITMAP)
        GUICtrlSetBkColor($hButton1,0x00FF00)
        _ClipBoard_Close()
    ElseIf $bClip1 Then
        ConsoleWrite("Setting data to clipboard.." & @CRLF)
        _ClipBoard_Open(0)
        If _ClipBoard_SetData($vClip1, $CF_BITMAP) = 0 Then Consolewrite("Failed to set data.." & @CRLF)
        If _ClipBoard_IsFormatAvailable($CF_BITMAP) Then ConsoleWrite("Bitmap format available on clipboard" & @CRLF)
        _ClipBoard_Close()
    EndIf
EndFunc

Func Button2()
        
    If _IsPressed("11") Then
        $vClip2 = ""
        GUICtrlSetBkColor($hButton2,0xCCCCCC)
        $bClip2 = False
        Return
    EndIf   
    
    If Not $bClip2 Then
        $bClip2 = Not $bClip2
        _ClipBoard_Open(0)
        $vClip2 = _ClipBoard_GetData()
        GUICtrlSetBkColor($hButton2,0x00FF00)
        _ClipBoard_Close()
    ElseIf $bClip2 Then
        _ClipBoard_Open(0)
        _ClipBoard_Empty()
        _ClipBoard_SetData($vClip2)
        _ClipBoard_Close()
    EndIf
EndFunc
Link to comment
Share on other sites

Link to comment
Share on other sites

Thanks for the reply. Another problem crops up though. I want to be able to store other types of data on the clipboard in the meantime, and recall the bitmap later. As soon as you put some text or something on the clipboard, the SetDataEx function doesn't place the bitmap back in when executed.

So for example:

#Include <Clipboard.au3>
HotKeySet("{F11}", "_display")
While 1
    Sleep(10)
WEnd
func _display()
    Local $var
    $var = _ClipBoard_GetDataEx($CF_BITMAP)
    msgbox(0,"","Copy some text from somewhere now, then click Ok here to put image back on clipboard")
    _ClipBoard_SetDataEx($var, $CF_BITMAP)
    msgox(0,"","Try pasting into paint now.. It won't work, the text is still on the clipboard")
EndFunc

Now when the first message box pops up, copy some text from somewhere. Then click OK. Next it should execute the _ClipBoard_SetDataEx($var,$CF_BITMAP) and place the contents of $var back into the clipboard shouldn't it? But it doesn't. If I go into mspaint after and paste, nothing happens. The text is still on the clipboard.

Link to comment
Share on other sites

try with clipboard open and close:

#Include <Clipboard.au3>
HotKeySet("{F11}", "_display")
$GUI = GUICreate("title", 200, 200)
While 1
    Sleep(10)
WEnd
func _display()
    _ClipBoard_Open ($GUI)
    Local $var
    $var = _ClipBoard_GetDataEx($CF_BITMAP)
    msgbox(0,"","Copy some text from somewhere now, then click Ok here to put image back on clipboard")
    _ClipBoard_SetDataEx($var, $CF_BITMAP)
    _ClipBoard_Close()
EndFunc
Link to comment
Share on other sites

So, it doesn't work as expected for you either right?

1. I press print screen to take a screenshot

2. Run your latest script

3. When the message box pops up, I open notepad and copy some text, eg: "Foo"

4. Then click ok on the message box

5. Then try to paste into paint.

It gives me the same error I was getting in my original script, "Error getting the clipboard data!"

If after running your script above, I run the example script in the helpfile for _ClipBoard_FormatStr it shows the formats on the clipboard as Bitmap, DIB and DIB V5. The same as it does if I run it right after pressing print screen. So the _ClipBoard_SetDataEx function is putting something back on the clipboard with the same format. But something must be missing that is preventing it from pasting properly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...