Inactive window capture

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mhcha
Newbie
Posts: 1
Joined: Sat Jan 01, 2022 11:10 am

Inactive window capture

Post by mhcha » Wed May 25, 2022 1:09 am

Hello,
I'm trying to make a macro that captures the screen of an inactive window and clicks it.

Through many Google posts, I found that the following Windows api function is needed, and I want to apply it to the macro scheduler.

The procedure is as below.
1. user32.dll FindWindowA function //can replace it with the GetWindowHandle of the macro scheduler.
2.user32.dll GetDC function
3. user32.dll GetWindowRect function // can replace it with the GetWindowSize of the macro scheduler.
4. user32.dll PrintWindow function
5. user32.dll Post/SendMessageA function // Use user32.dll to click instead of Mouse_Event.

Any help would be appreciated.

#Below is the site I referenced.
https://intrepidgeeks.com/tutorial/c-ca ... ve-windows

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1106
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Inactive window capture

Post by Dorian (MJT support) » Wed May 25, 2022 8:43 am

This example will take a screenshot of "Untitled - Notepad" and then click in the top left corner of that window. It saves to a unique file name each time, based on the date and time it was created.

It may be slightly over-engineered for your needs. I adapted it from a script I use regularly to take a screenshot of whichever window is active (I've removed that part).

Hopefully the comments will suffice and you'll find this a good starting point for your project...

Code: Select all

//Note. Works best if you assign this macro a Hot Key.

//Click on window?
Let>WindowClick=yes

//Show the screenshot in Windows Explorer? yes/no
Let>ShowInFolder=no

//Open the screenshot in MS Paint? yes/no
Let>EditFile=no

//Get the window size and pos (using Notepad as a test)
GetWindowPos>Untitled - Notepad,Xpos,Ypos
GetWindowSize>Untitled - Notepad,Width,Height

//Calculate the window coordinates
Let>BottomRightX=Xpos+Width
Let>BottomRightY=Ypos+Height

//Get time and date for the filename (for unique file name)
Year>TheYear
Month>TheMonth
Day>TheDay
Hour>TheHour
Min>TheMin
Sec>TheSec

//Set file path and name
Let>FilePath=%USERDOCUMENTS_DIR%\Screenshots
Let>FileName=%FilePath%\Screenshot-%TheYear%-%TheMonth%-%TheDay%-%TheHour%-%TheMin%-%TheSec%.png

//Create the output folder if it doesn't exist
IfNotDirExists>%FilePath%
    CreateDir>%FilePath%
Endif

//Capture the screenshot
ScreenCapture>Xpos,Ypos,%BottomRightX%,%BottomRightY%,%FileName%

//Show the screenshot in Windows Explorer if ShowInFolder = yes
if>ShowInFolder=yes
    RunProgram>Explorer /select,%FileName%
Endif


//Edit the screenshot in MS Paint if EditFile = yes
If>EditFile=yes
    RunProgram>msPaint.exe %FileName%
    Wait>1
    WindowAction>1,Paint*
Endif

//Click on top left corner of window (add to Xpos and Ypos if you want to click further down or in) 
If>WindowClick=yes
  mousemove>Xpos,Ypos
  lclick
Endif
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts