Changing an ImageGadget image during runtime?

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 PB.

Hi,

I've created an ImageGadget and need to change its image during runtime.
I've searched these forums and tried the examples posted, but none of them
work for me (Windows 2000, Service Pack 2).

Is there an "official" way to do it? The examples I've tried just leave the
ImageGadget blank with nothing shown...


PB - Registered PureBasic Coder
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 Fangbeast.
Hi,

I've created an ImageGadget and need to change its image during runtime.
I've searched these forums and tried the examples posted, but none of them
work for me (Windows 2000, Service Pack 2).

Is there an "official" way to do it? The examples I've tried just leave the
ImageGadget blank with nothing shown...


PB - Registered PureBasic Coder
#STM_SETIMAGE = $172 ; API Constants for reloading imagegadgets after drawing
#IMAGE_BITMAP = 0
#ES_NUMBER = $2000

CreateImage(0, 512, 312) ; anywhere after openwindow command (512 wide by 312 high)
; that was just the size I was using

ImageGadget(#itempic, 0, 1, 512, 312, UseImage(0), #PB_Image_Border); now create gadget

; To reload an image into the created picture buffer, do the below!!

Item_Pic1.l = LoadImage(0, New_Picture_FileName$) ; Now load the new image
SendMessage_(GadgetID(#itempic), #STM_SETIMAGE, #IMAGE_BITMAP, Item_Pic1.l); send the new
; pic to the gadget!!

ResizeGadget(#itempic, 0, 1, 512, 312) ; resize if you have to


I cannot guarantee that this will work under 2k but I asked this question ages ago and this is what I came up witht hat works under XP in my personal home inventory package.

Regards


Fangles
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 PB.

> I cannot guarantee that this will work under 2k [snip]

Hi Fangs... it works under Windows NT (here at work) so hopefully it will with
Windows 2000 when I get home later... fingers crossed! Thanks for the code.

UPDATE: Nope, doesn't work with Windows 2000. Anyone else?


PB - Registered PureBasic Coder


Edited by - PB on 12 August 2002 22:36:35
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 Franco.

Hi PB,
that's strange.
I have @work 'Win2000 Sp3' and both of the following codes worked fine (Loupe).

This code uses DrawImage:

Code: Select all

; (c) 2002 - Franco's template - absolutely freeware
; Thank's Pupil for the hint :)
; Desktop Loupe Function
;
Procedure CaptureScreen(Left.l, Top.l,Factor.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
  BmpID.l = CreateImage(0, 200, 200)
  SelectObject_( MemDC, BmpID)
  StretchBlt_( MemDC, 0, 0,200*Factor,200*Factor, DC,Left, Top, 200, 200, #SRCCOPY )
  DeleteDC_( MemDC)
  ReleaseDC_(0,DC)
  ProcedureReturn BmpID
EndProcedure
;
If OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Loupe") : Else : End : EndIf
;
CursorPosition.POINT
StartDrawing(WindowOutput()) 
;
Repeat
  EventID.l = WindowEvent()
  Delay(10)
  GetCursorPos_(CursorPosition) 
  hImage = CaptureScreen( CursorPosition\x - 24,CursorPosition\y - 24,4)
  DrawImage(hImage, 0, 0) 
Until EventID=#PB_EventCloseWindow   
;
End
 
This code uses ImageGadget:

Code: Select all

;The picture changes the same instant i move the mouse...

;Well, we'll just have to try another one, here's another one that works for me:
;
Procedure CaptureScreen(Left.l, Top.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
;  FreeImage(0) 
  BmpID.l = CreateImage(0, 200, 200); CreateCompatibleBitmap_(DC, 200, 200)
  SelectObject_( MemDC, BmpID)
;  BitBlt_( MemDC, 0, 0, 200,200, DC, Left, Top, #SRCCOPY)
  StretchBlt_( MemDC, 0, 0,200*2,200*2, DC,Left, Top, 200, 200, #SRCCOPY )
  DeleteDC_(MemDC)
  ReleaseDC_(0,DC)
ProcedureReturn BmpID ;same BUG if Bitmap or BmpID is used...
EndProcedure
;
If OpenWindow(0,200,200,200,200,#PB_Window_SystemMenu,"Loupe") : Else : End : EndIf
If CreateGadgetList(WindowID()) : Else : End : EndIf
;
CursorPosition.POINT
hImage = CaptureScreen( CursorPosition\x, CursorPosition\y)
hGadget = ImageGadget(0, 0, 0, 200,200, hImage)
;
Repeat
  ;EventID.l = WaitWindowEvent() ;same BUG if WaitWindowEvent or WindowEvent is used
  EventID.l = WindowEvent() ;same BUG if WaitWindowEvent or WindowEvent is used
  Delay(10) ;used for WindowEvent
  GetCursorPos_(CursorPosition) 
  hImage = CaptureScreen( CursorPosition\x, CursorPosition\y)
  SendMessage_(hGadget, #STM_SETIMAGE, #IMAGE_BITMAP, hImage)
Until EventID=#PB_EventCloseWindow   
;
End
 
These are only examples (older ones) to show you that it works fine on W2K(Sp3).


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
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 PB.

> I have @work 'Win2000 Sp3' and both of the following codes worked fine (Loupe).

I worked out what was wrong -- and it's damn embarrassing!

The code where I was using SendMessage_() to redraw the ImageGadget was a
procedure... yeah... you can guess, right? The handle to the ImageGadget
wasn't global or shared in that procedure. I'll go shoot myself now.


PB - Registered PureBasic Coder
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 Franco.
> I have @work 'Win2000 Sp3' and both of the following codes worked fine (Loupe).

I worked out what was wrong -- and it's damn embarrassing!

The code where I was using SendMessage_() to redraw the ImageGadget was a
procedure... yeah... you can guess, right? The handle to the ImageGadget
wasn't global or shared in that procedure. I'll go shoot myself now.


PB - Registered PureBasic Coder


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
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 Fangbeast.
> I have @work 'Win2000 Sp3' and both of the following codes worked fine (Loupe).

I worked out what was wrong -- and it's damn embarrassing!

The code where I was using SendMessage_() to redraw the ImageGadget was a
procedure... yeah... you can guess, right? The handle to the ImageGadget
wasn't global or shared in that procedure. I'll go shoot myself now.

PB - Registered PureBasic Coder
Gee, that sounds like the sort of thing I would do:)

/me falls out of chair laughing

Fangles
Post Reply