What's wrong with this code?

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

The Image disappears after 5 seconds...

Code: Select all

; Desktop Copy Function
; No Image after 5 seconds.
;
Procedure CaptureScreen(Left.l, Top.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
  BmpID.l = CreateCompatibleBitmap_(DC, 200, 200)
  SelectObject_( MemDC, BmpID)
  BitBlt_( MemDC, 0, 0, 200,200, DC, Left, Top, #SRCCOPY)
  ;Bitmap = GetCurrentObject_(MemDC,#OBJ_BITMAP) ;used with ProcedureReturn Bitmap	
  ;DeleteObject_(BmpID) ;used with ProcedureReturn Bitmap
  DeleteDC_( MemDC)
  ReleaseDC_(0,DC)
  ;ProcedureReturn Bitmap ;same BUG if Bitmap or BmpID is used...
  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
 
Tested with Win98SE (not tested yet on WinXP...)

Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.


Edited by - franco on 12 June 2002 19:48:26
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 Pupil.
The Image disappears after 5 seconds...

Tested with Win98SE (not tested yet on WinXP...)
Get more memory if you want to keep the program running longer than 5 seconds :wink:, you're not releasing the bitmap so you're creating an awfull lot of images that's being stored in memory... I did this small change and got your program to work properly, other solutions should work aswell, all i do is using the same bitmap over and over again.

Code: Select all

; Desktop Copy Function
; No Image after 5 seconds.
;
Procedure CaptureScreen(Left.l, Top.l, handle.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
  If handle = 0
    BmpID.l = CreateCompatibleBitmap_(DC, 200, 200)
  Else
    BmpID.l = handle
  EndIf
 SelectObject_( MemDC, BmpID)
  BitBlt_( MemDC, 0, 0, 200,200, DC, Left, Top, #SRCCOPY)
;Bitmap = GetCurrentObject_(MemDC,#OBJ_BITMAP) ;used with ProcedureReturn Bitmap	
;DeleteObject_(BmpID) ;used with ProcedureReturn Bitmap
  DeleteDC_( MemDC)
  ReleaseDC_(0,DC)
;ProcedureReturn Bitmap ;same BUG if Bitmap or BmpID is used...
 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, 0)
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, hImage)
  SendMessage_(hGadget, #STM_SETIMAGE, #IMAGE_BITMAP, hImage)
Until EventID=#PB_EventCloseWindow   
;
End
Works on w2k...
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 Pupil,
thanks for the response.
I'm also convinced that this has to be a memory problem.
Your changes don't work on Win98.
The pictures are coming very, veerryy, sloooooow...

And I'm not sure if with this code it will crash laterrrr..... sleeping

How fast are the pictures changing on Win2k


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 Pupil.
Hi Pupil,
thanks for the response.
I'm also convinced that this has to be a memory problem.
Your changes don't work on Win98.
The pictures are coming very, veerryy, sloooooow...

And I'm not sure if with this code it will crash laterrrr..... sleeping

How fast are the pictures changing on Win2k
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:

Code: Select all

;
Procedure CaptureScreen(Left.l, Top.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
  BmpID.l = CreateImage(0, 200, 200); CreateCompatibleBitmap_(DC, 200, 200)
  SelectObject_( MemDC, BmpID)
  BitBlt_( MemDC, 0, 0, 200,200, DC, Left, Top, #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
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.

Thanks Pupil, this is much better.

The delay now is only SEVEN seconds...

---------------------------
Bill Gates stated on trial:
"Every Windows application runs on every Windows version..."

How long have the programmers to figure out why code act's different on different Windows versions?

Ahh, I know:
"Every Windows application runs on every Windows version... If they use M$ programming Languages!"

Sometimes I dislike Windows very much! Now is such a time!!!




---------------------------

Fred, Please If You Have Time: "Work On The Linux/Amiga/otherOS Versions".

I know, I know: at this point only M$-Windows makes money, a day has only 24 hours, and nobody can live with air and love only...
---------------------------

Anyway, have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - Franco on 12 June 2002 23:11:56
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.

> Your changes don't work on Win98.
> The pictures are coming very, veerryy, sloooooow...

In Dan Appleman's Guide to the Win32 API (p493), he states: "[...] because GDI
is still 16 bits under Windows 95, the performance of some of the graphics operations
described here is significantly slower under Windows 95 than Windows NT. [...]
The drawing is nearly instantaneous under Windows NT [...] and so slow as to be
almost impractical under Windows 95."

So, Franco, it could be that Win 98, which uses the same code base as 95, simply
cannot draw the images as fast as it does on Win 2K (which is instant for me).
I'm not saying this is the case, but it may explain why it's slow on 9x and fast
on 2K/NT...

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.

@ PB,
thanks for the input, maybe this is the case...
Have you ever seen Win3.11 on never machines? (there are some graphic driver for newer cards)
It's really fast (if you got it running...) - and only 16 bit.
So I suppose there is more going on...

@ Pupil,
found out that both suggestions of you, run just fine on Win98 if I use WindowEvent/Delay(10) instead of WaitWindowEvent.
Maybe what PB mentioned (16bit-GDI) is slowing down WaitWindowEvent under Win98.

BTW:
While trying to fix the code here another approach.
DrawImage instead of ImageGadget (works well under Win98):

Whith 'StretchBlt' you have a magnifier.

Code: Select all

; Desktop Loupe Function
;
Procedure CaptureScreen(Left.l, Top.l)
  DC.l = GetDC_(0)
  MemDC.l = CreateCompatibleDC_(DC)
  BmpID.l = CreateImage(0, 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
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) ;used for WindowEvent
  GetCursorPos_(CursorPosition) 
  hImage = CaptureScreen( CursorPosition\x, CursorPosition\y)
  DrawImage(hImage, 0, 0) 
Until EventID=#PB_EventCloseWindow   
;
End
 

Thanks both of you for helping out, it's truly appreciated.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - Franco on 13 June 2002 00:36:09
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.
thanks for the input, maybe this is the case...
Have you ever seen Win3.11 on never machines? (there are some graphic driver for newer cards)
It's really fast (if you got it running...) - and only 16 bit.
So I suppose there is more going on...
I've never seen a Win3.11 machine, ever. Anyway, hopefully some other Win9x
users can test your code and let you know how it performs...

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 Tron.
I've never seen a Win3.11 machine, ever. Anyway, hopefully some other Win9x
users can test your code and let you know how it performs...
Franco I tested it on my Windows ME machine and it works really fast. I should also mention I am running a 1.4GHz Athalon with a GeForce3 video card, so this may have helped it out. It was pretty speedy drawing my desktop in the litle Loupe window.

BTW I was testing with your last code snipet with the DrawImage function.

Tron

"Honesty is the best policy, but insanity is a much better defense."
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.

Thanks Tron,
the last code works on Win98SE, WinME, Win2k and WinXP if it uses WindowEvent.
But why it works fine on Win2k and WinXP with WaitWindowEvent knows only the Microsoft employee #5749, but he left M$ 6 month ago...
We will never know.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
Post Reply