bitmap to clipboard problem?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cor.

Look at the code below

Clicking on the button will copy the bitmap to clipboard

Then paste it into e.g. wordpad ->>> No problem -> WM_PAINT works ok

Then clicking for the second time on the button --->> Nothing on clipboard->> WM_PAINT doesn't work anymore. see if you focus to other windows and go back to this program.

Someone a solution?

Code: Select all

;
;
InitGadget(5)

If OpenWindow(0, 100, 100, 500, 300, $10CF0000, "PureBasic - Image")

If CreateGadgetList(WindowID())

 ButtonGadget(0,300,20,150,24,"Copy to clipboard")

Else

MessageRequester("Info","Error creating gadgetlist",0)
EndIf

  Gosub CreateImage

  Procedure WindowCallback(WindowID, Message, lParam, wParam)
    If Message = #WM_PAINT
      StartDrawing(WindowOutput())
        DrawImage(ImageID(), 20, 10)
      StopDrawing()
    EndIf
  EndProcedure
      
  SetWindowCallback(@WindowCallback())

  Repeat
    EventID = WaitWindowEvent()
    
    If EventID=#PB_EventGadget
       Select EventgadgetID
         Case 0
           Openclipboard_(0)
           Emptyclipboard_()
           setclipboarddata_(#CF_BITMAP,hIMG)          
           closeclipboard_()
       EndSelect
    EndIf
  Until EventID = #PB_EventCloseWindow  ; If the user has pressed on the close button
  
EndIf

End   ; All the opened windows are closed automatically by PureBasic


;
; Some 2D graphics functions...
;

CreateImage:

  hIMG= CreateImage(0, 255, 255)
   If hIMG

    StartDrawing(ImageOutput())
    
    For k=0 To 255
      FrontColour(k,0, k)  ; a rainbow, from black to pink
      Line(0, k, 255, 0)
    Next

    DrawingMode(1)
    Locate(40, 50)
    FrontColour(255,255,255) ; print the text to white !
    DrawText("An image created easely...")

    StopDrawing() ; This is absolutely needed when the drawing operations are finished !!! Never forget it !
    
  EndIf

Return    
; ExecutableFormat=Windows
; DisableDebugger
 
Using Windows 98 SE
Registered PB version : 2.90 (Windows)
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

>OpenWindow(0, 100, 100, 500, 300, $10CF0000, "PureBasic - Image")

Whats the $10CF0000 ?? This doesnt open a Window here (Win2k),
but the application is running.
If i change it to 0 or 1, its working.


Your WindowCallback Function is still working,
and the WM_PAINT too.
Simply insert a Beep_(1000,100) in your WM_PAINT
handler, and you will hear it.

The problem looks more like a API-problem to me,
not a problem with PureBasic.
Read some more about the Windows ClipBoard
in MSDN, and you should see...

Read especially about the following:
1.) what happens with the Image if you copy
it to the clipboard ?? Is the handle then
owned by Windows ??
2.) CloseClipboard (if i remember correctly,
this function should be called at the end
of your program)

Clipboard API doesnt work so easy (like
most WinAPI-functions), because it is shared
with other programs - so you have to follow
exactly the programming rules for it.

It works the first time, and at the end
of the first call you use CloseClipboard().
It doesnt work the next time
("Error: getting clipboard data" with MS Paint),
and your program cant repaint the image -
so it looks like the image(-handle) is destroyed/owned
by windows or something like that.

cya,
...Danilo
(registered PureBasic user)

Edited by - Danilo on 12 February 2002 23:26:15
Post Reply