Capture the Desktop except the ForeGround Window [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Capture the Desktop except the ForeGround Window [Windows]

Post by RASHAD »

Run the next snippet first
- System Tray icon added to inform that the program is running
-Ctrl + Q to Quit
-Ctrl + 1 To capture the image

Code: Select all

#CAPTUREBLT  = $40000000
Global hWnd_A,hWnd
  CatchImage(0,?View)
  AddSysTrayIcon(0,GetDesktopWindow_(),ImageID(0))
  SysTrayIconToolTip(0,"Window Information")

Repeat
    Delay(100)
    hWnd_A = GetForegroundWindow_()
   
  If GetAsyncKeyState_(#VK_CONTROL) & $8000 = 32768 And GetAsyncKeyState_(#VK_Q) & $8000 = 32768
    End
   
  ElseIf GetAsyncKeyState_(#VK_CONTROL) & $8000 = 32768 And GetAsyncKeyState_(49) & $8000 = 32768
      ExamineDesktops()
     
      hWnd = FindWindow_("ProgMan", 0)
     
      hBitmap = CreateImage(0, DesktopWidth(0), DesktopHeight(0),32)
      hdc = StartDrawing(ImageOutput(0))
           SelectObject_(hdc, hBitmap)
           BitBlt_(hdc, 0, 0,DesktopWidth(0), DesktopHeight(0), GetWindowDC_(hWnd), 0, 0, #SRCCOPY | #CAPTUREBLT)
      StopDrawing()
     
      hWnd = GetTopWindow_(GetDesktopWindow_())
      hWnd = GetWindow_(hWnd,#GW_HWNDLAST)
     
      While hWnd
        hWnd = GetWindow_(hWnd,#GW_HWNDPREV)
        If  IsWindowVisible_(hWnd) = 1 And IsIconic_(hWnd) = 0       
          WTitle$ = Space(128)
            GetWindowText_(hWnd,@WTitle$,128)
            If hWnd <> hWnd_A And WTitle$ <> "Start" And hWnd <> FindWindow_("Shell_TrayWnd", #Null)
                 GetWindowRect_(hWnd,r.RECT)
                If (r\right - r\left) > 0 And (r\bottom-r\top) > 0
                   hBitmap = CreateImage(1,r\right-r\left,r\bottom-r\top ,32)
                   hdc = StartDrawing(ImageOutput(1))
                         SelectObject_(hdc, hBitmap)
                         BitBlt_(hdc, 0, 0,r\right-r\left,r\bottom-r\top , GetWindowDC_(hWnd), 0,0, #SRCCOPY | #CAPTUREBLT)
                   StopDrawing()
                   StartDrawing(ImageOutput(0))
                    ;DrawingMode(#PB_2DDrawing_AlphaClip  )
                    DrawImage(ImageID(1),r\left,r\top)
                   StopDrawing()
                EndIf
            EndIf
         EndIf
       Wend
      ; 
      hWnd = FindWindow_("Shell_TrayWnd", #Null)
     
      hBitmap = CreateImage(1, DesktopWidth(0), 30,32)
      hdc = StartDrawing(ImageOutput(1))
         SelectObject_(hdc, hBitmap)
         BitBlt_(hdc, 0, 0,DesktopWidth(0), 30, GetWindowDC_(hWnd), 0, 0, #SRCCOPY| #CAPTUREBLT)
      StopDrawing()
     
      StartDrawing(ImageOutput(0))
        DrawingMode(#PB_2DDrawing_AlphaClip  )
        DrawAlphaImage(ImageID(1),0,DesktopHeight(0)-30,255)
      StopDrawing()
     
      SaveImage(0,"e:\test2.bmp")
    EndIf
ForEver

DataSection
View:
   Data.q $1010000200010000,$0568000800010000,$1010000000260000,$0468002000010000,$00280000058E0000,$0020000000100000,$0000000800010000,$0000000001400000
   Data.q $0100000000000000,$3668000000000000,$F3FB004040750039,$F0F70000000000E4,$00000000000000D4,$F6FB000000000000,$FCFF0000000000E6,$E2F400ECFAFC00F5
   Data.q $671000DEF3FA00C4,$610E00FFF7B200C0,$9BCA009E9EBD00BA,$0000005B58890084,$D2EB005B578A0000,$00000000000000B4,$F8FB000000000000,$00000000000000EE
   Data.q $0000000000000000,$CEED00E6F5FB0000,$00000000000000AE,$0000000000000000,$000000C0E4FA0000,$DEF2000000000000,$00000000000000C3,$741100AEAFC70000
   Data.q $00000000000000D0,$D4EC000000000000,$000000ECFFFF00B3,$0000000000000000,$0000008B43170000,$0000000000000000,$000000B6ADC40000,$0000000000000000
   Data.q $0000000000000000,$000000BCDBF10000,$C4E600D9E1EA0000,$00000000000000A2,$F1F700ABCEEB0000,$000000EFFCFD00E5,$0000000000000000,$000000EC8E150000
   Data.q $5010000000000000,$00000000000000A6,$0000000000000000,$F6FA00E4F5FB0000,$DFF100C0DDF200E5,$00000000000000C2,$0000000000000000,$FFFF00CCE9F50000
   Data.q $00000000000000E3,$C5EC000000000000,$000000000000009D,$0000000000000000,$C2CD00BFDDF00000,$401000C1B9C700C8,$000000BBD9F0008A,$0000000000000000
   Data.q $5015000000000000,$00000000000000A1,$FEFE00C2E1F50000,$00000000000000F8,$E2F200E1F3FA0000,$00000000000000C4,$0000000000000000,$0000004F45720000
   Data.q $D4ED00C4E0F30000,$5F120000000000B6,$511D0000000000B6,$76B900000000009C,$E9B100FFFEFE006C,$00000000000000FE,$F4CD00FDF4C60000,$00000000000000FF
   Data.q $000000A9C0E00000,$881F000000000000,$FFFF0000000000DF,$A4C000736E9200F4,$F8FC009CACC70097,$F0F90000000000ED,$000000EAFFFF00DD,$0000000000000000
   Data.q $0000000000000000,$7B09000000000000,$8FC50000000000E5,$000000000000007E,$000000FFC7580000,$000000ADD2F10000,$91B9000000000000,$0000000000000081
   Data.q $0000000000000000,$0000000000000000,$B1C600E3F5FB0000,$000000B9B3C500B1,$0000000000000000,$000000FFF7DB0000,$FFFF000000000000,$000000DCFAFF00DE
   Data.q $0000000000000000,$3E6F00A6CAEC0000,$C0E900F2FFFF0043,$EDF800000000009B,$000000CCE9FA00D3,$0000000000000000,$749F000000000000,$0000000000000077
   Data.q $C0CE000000000000,$000000625E8D00C4,$0000000000000000,$0000005F62970000,$F2F9000000000000,$00000000000000D9,$F2FA000000000000,$0000006C608800DA
   Data.q $C7D100EDFBFC0000,$D4EF0000000000CD,$CDEC0000000000B6,$00000000000000AD,$000000C3E3F40000,$FFFE000000000000,$DBF000B2D1EE00F8,$00000000000000BE
   Data.q $0000000000000000,$E3F3000000000000,$00000000000000C5,$E8F6000000000000,$000000AACCEE00CC,$0000000000000000,$0000009549160000,$0000000000000000
   Data.q $FFFF00C4E1F30000,$680300FFFFFF00FF,$0303030303030335,$0F82030303030303,$030303030303F9A0,$A54E030303030303,$03030303036EA086,$1003030303030303
   Data.q $0303030380A08AA5,$0303030303030303,$0303030E4BB7A510,$0303030303030303,$94C0D3A089A52C03,$030303030301AA95,$C14615A5A5900303,$0303030300BA5C99
   Data.q $0BE8FECF84030303,$030303C2C5280402,$0B728541CA030303,$03037B25287D0402,$531A969211030303,$03037BE016C4981F,$B10808312B030303,$030313E97645550A
   Data.q $75DAD6BBB2030303,$0303DB200C47FEE5,$0D5BF4F039030303,$030393F56554DD30,$42FD71CE03030303,$030303A2603F56EA,$69A78D0303030303,$0303030312BFE27E
   Data.q $DE8D030303030303,$FF9F030303B36766,$FF070000FF0F0000,$FFC10000FF830000,$03F0000007E00000,$00F8000001F80000,$00F8000000F80000,$00F8000000F80000
   Data.q $03FE000001FC0000,$0028000007FF0000,$0020000000100000,$0000002000010000,$0000000004400000,$0000000000000000,$6AAE000000000000,$2703F676330D5F5D
   Data.q $000000000000B578,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$623A000000000000,$8A14FFFFD470FF9D
   Data.q $0000CE792802FFFD,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$5900000000000000,$FFCFFFFFF1A1B8B7
   Data.q $3C02FFF69117FFFF,$000000000000C997,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$E27EC9B753000000
   Data.q $A12BFFFFFFE0FFFF,$0000CDAC4B00FFF6,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$6100000000000000
   Data.q $FFE9FFFFD965CDC6,$6B00FFF7B34BFFFF,$000000000000C9E4,$225208302D590000,$00001026234E1A27,$0000000000000000,$0000000000000000,$0000000000000000
   Data.q $D558C9D56D000000,$8B11FFFFFBBCFFFF,$2F676E625775FFF0,$85A6E16A6E94A42D,$2956E8636F99F378,$000030231E47BF2B,$0000000000000000,$0000000000000000
   Data.q $8200000000000000,$B8C7FFFFA519D0E9,$FAFAFF6E74ABFFBC,$FFFFFFFFFFFFFFF6,$F8FFFFEBFFFFFFF3,$1E48FF5C6998FFD2,$0000000000005F23,$0000000000000000
   Data.q $0000000000000000,$8AC964947A700000,$FFFFFFFFFFFDFF7D,$F2FAFFECFAFDFFFF,$E8F8FFD3F0F6FFE1,$6999FFD9FFFFFFCB,$0000262D274EFF59,$0000000000000000
   Data.q $0000000000000000,$EFF56F5559960000,$FCFEFFFFFFFFFFE8,$F2FBFFEEF9FCFFF5,$D5EDFFD7F3F9FFDF,$D5F5FFBCDCF4FFB7,$0000AE3E406FFFAE,$0000000000000000
   Data.q $0000000000000000,$FFFFCB8380A50000,$F9FCFFF2FAFDFFFC,$F8FDFFE5F4FBFFEF,$E5F5FFDDF3FAFFE9,$E4FFFFB6D4ECFFC9,$0000F8616895FFBC,$0000000000000000
   Data.q $0000000000000000,$FFFFCA9D9EBA0000,$F5FAFFE5F4FBFFE9,$F4FCFFE9F7FDFFE2,$D9F1FFC9E3F4FFE2,$E1FFFFC0DDEFFFBF,$0000F56C75A2FFB9,$0000000000000000
   Data.q $0000000000000000,$FEFFC29B9CB60000,$F3FAFFDBF2FAFFDA,$E3F4FFE0F5FBFFD9,$FFFFFFFBFDFEFFC6,$D5FDFFCFEBF5FFFB,$0000FC6C729EFFAB,$0000000000000000
   Data.q $0000000000000000,$DAEEA1A499B40000,$DEF2FFCEEFFCFFBB,$E1F3FFD0EAF6FFC0,$F8FAFFEDFDFDFFC6,$B8E8FFC8E8F7FFE3,$00009E696894FF92,$0000000000000000
   Data.q $0000000000000000,$AEC90DB3ACBF0000,$D9F1FFB3D8F6FFAA,$E2F4FFACCCE8FFB7,$E3F7FFCAE9F4FFC6,$86B9FF9FCCF3FFC3,$000037706484FF77,$0000000000000000
   Data.q $0000000000000000,$BAC7000000000000,$C4E7FFA7B1CB38C1,$C5EEFF98C4EDFF9F,$AFE1FF90BDEBFF9A,$7291FF798FBFFF84,$0000000000007483,$0000000000000000
   Data.q $0000000000000000,$0000000000000000,$B4C620C3BDC70000,$ADC8F5A4B0CB8FB6,$96B5F89AA3C3F3A5,$0000439B8FA4A896,$FF1F000000000000,$FF070000FF0F0000
   Data.q $8FC10000FF830000,$01F0000003E00000,$00F8000000F80000,$00F8000000F80000,$00F8000000F80000,$01FC000000F80000,$000003FE0000
Viewend:
EndDataSection 

Edit : Code modified
Last edited by RASHAD on Wed Jan 16, 2013 5:11 pm, edited 1 time in total.
Egypt my love
American Ninja
User
User
Posts: 24
Joined: Mon Jan 14, 2013 8:01 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by American Ninja »

Still doesn't quite work... this is what it does to my system tray:

Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Capture the Desktop except the ForeGround Window [Window

Post by SFSxOI »

Crashes for me at this line when i do "-Ctrl + 1 To capture the image" - on Windows 7 Ultimate x86 and 64bit - PB 5.0
hBitmap = CreateImage(1,r\right-r\left,r\bottom-r\top ,32)
Crashes with this error > "Image 'Width' is too small, should be > 0"
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by RASHAD »

Hi SFSxOI
You are deeply involved with IP :mrgreen:

Previous post updated
Egypt my love
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Capture the Desktop except the ForeGround Window [Window

Post by SFSxOI »

Thanks for the update RASHAD, however, works now but also captures the foreground window as well so what you get is the desktop with the foreground window showing also plus the desktop background is always black so it does not show any wallpaper the desktop may be using (but does show all the icons on the desktop)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
American Ninja
User
User
Posts: 24
Joined: Mon Jan 14, 2013 8:01 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by American Ninja »

I think it's a lost cause..... although I appreciate Rashad's efforts to help. :mrgreen:

The only close way to achieve the goal is to hide the foreground window, take the shot, then restore the window, but that brings issues of lost focus, major flicker (especially when the window is maximized), and re-arranged taskbar (on XP at least). There has to be a way, though.... I'll work something out eventually.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by RASHAD »

@SFSxOI Hi
Did you compiled the code to exe?
It can not be unless some other window took the focus (GetForeGroundWindow() should get it at fixed intervals) you design the proper one
By decreasing the delay time
Delay(50) for exam

@American Ninja Suit yourself
The boys gave you what you suggested from the beginning
But you start giving us false cases like Caculator then WordPad

The only way to get the last Active window is using a Wach Dog or Global Hook
Do what you like it is your case after all

The code is for users to adapt it for there needs not to copy and paste :)
Egypt my love
American Ninja
User
User
Posts: 24
Joined: Mon Jan 14, 2013 8:01 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by American Ninja »

RASHAD wrote:@American Ninja Suit yourself
The boys gave you what you suggested from the beginning
But you start giving us false cases like Caculator then WordPad

The only way to get the last Active window is using a Wach Dog or Global Hook
Do what you like it is your case after all
Um, that is utter bullshit. In the other post, I said without including the foreground window, not last active window. The Calculator in the screenshot was just an example, because it happened to be the foreground window at the time. But the foreground window can be anything.... a maximized Notepad, for example. So don't start spreading lies and bullshit about what I suggested. I suggest you read my post in the other thread again. I was quite clear.

Regardless, I can see that these forums are pretty much bullshit, so I'm leaving. I'm also still worried about the future of PureBasic's command set, and I don't need this bullshit in my life. I'm going to cut my losses and move to another language. God bless you all, every fucking one of you.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by RASHAD »

@American Ninja
Take easy man
What is wrong with PureBasic?
Do what you are asking for in any other language and I will be the first one to follow you
And about the forum members if you are upset with me what about the others?
They were quite friendly I think :P
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5847
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Capture the Desktop except the ForeGround Window [Window

Post by idle »

Thanks Rashad
That's a much nice way of walking the windows
Windows 11, Manjaro, Raspberry Pi OS
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4948
Joined: Sun Apr 12, 2009 6:27 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by RASHAD »

Hi idle
You started the Parade then NM then I finished it :mrgreen:
Thanks
Egypt my love
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Capture the Desktop except the ForeGround Window [Window

Post by electrochrisso »

It is hard to please some people, I think American Ninja has been watching too many Kung Fu movies, and given PB the chop. :lol:
PureBasic! Purely the best 8)
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by yrreti »

electrochrisso wrote:
[quote]It is hard to please some people, I think American Ninja has been watching too many Kung Fu movies, and given PB the chop. :lol:[quote]

:lol: I think the Administrators or Global moderators should give him the chop too :!: :!: :!:
We don't need foul mouth unappreciative people like that on this forum, as all they do is cause dissension among the members.

yrreti
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Capture the Desktop except the ForeGround Window [Window

Post by IdeasVacuum »

I think American Ninja just lost it there, not pretty but who knows what pressure in life he is subject to. Up until that last post he was polite and appreciative of the help - of course he should not have lost his temper, especially not with Rashad who has spent a tremendous amount of time helping him (even if you don't need this code, there are always gems to learn from when Rashad posts). So, American Ninja is out of order, but if he regrets it now, let's forgive him.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Capture the Desktop except the ForeGround Window [Window

Post by yrreti »

Yes I guess your right IdeasVacuum.
I was looking at some of his earlier post in other topics after I posted that, and can sympathize with him
about changes some times. You work hard to get something working just right, and then you have to figure
out how to get it to work all over again with the new changes. Granted some changes are very nice,
and some are necessary. But when it breaks your code, it can sometimes be very frustrating. :cry:
But change is necessary, and with a little effort, and the helpful comments and suggestions from many
on this forum, we usually end up with a better program. I forgot who it was who wrote that program sometime
back called PB394to400. It was an invaluable tool back then. Wouldn't it be nice to have something like
that again when ever an upgrade happens, that could either convert your code like it did, or be able to flag
your code and offer suggestions. :idea: Something like that could help make the transition a lot smoother.
That was a very helpful program for me.

yrreti
Post Reply