To print a window on a dialog image

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

To print a window on a dialog image

Post by mhcha » Sun Jan 15, 2023 2:37 am

Hello, I would like to add a program called Calculator to my dialog image.

A program called a calculator is an example and requires the image to be dynamically.

Below is the dialog and code I wrote.

I want to put the calculator screen in MSImage1.
Is there a good way?

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 1024
  Top = 557
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 530
  ClientWidth = 732
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -17
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 144
  TextHeight = 20
  object MSImage1: tMSImage
    Left = 16
    Top = 16
    Width = 561
    Height = 497
  end
  object MSButton1: tMSButton
    Left = 591
    Top = 18
    Width = 130
    Height = 55
    Caption = 'MSButton1'
    TabOrder = 8
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,MSButton1_Clicked

Show>Dialog1,res1



SRT>MSButton1_Clicked
  
Let>lpWindowName=Calculator
Let>lpWindowName2=CustomDialog

LibFunc>User32,FindWindowA,Handle1,0,%lpWindowName%
LibFunc>User32,FindWindowA,Handle2,0,%lpWindowName2%

LibFunc>User32,PrintWindow,p,Handle1,Handle2,1

SetDialogProperty>Dialog1,MSImage1,LoadImage,%p%  // I want to put the calculator screen in MSImage1. It doesn't work.

END>MSButton1_Clicked

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

Re: To print a window on a dialog image

Post by Dorian (MJT support) » Sun Jan 15, 2023 1:28 pm

I have almost zero knowledge of user32.dll, so others may be able to offer help here using that. The only way I can think of is to use screencapture to capture the image to your temp folder. Then display that. The problem is that the calculator would need to be visible for ScreenCapture to see it. I'm not sure if using the user32 method would be the same in that regard, and also whether the resulting image would still be too large 9as it was when I tested the following on my 4k laptop).

I'll ask Marcus if he can chime in.

Code: Select all

IfNotWindowOpen>Calculator
  mdl>Please start Calculator first
  exit
Endif

Dialog>Dialog1
object Dialog1: TForm
  Left = 1024
  Top = 557
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 530
  ClientWidth = 732
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -17
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 144
  TextHeight = 20
  object MSImage1: tMSImage
    Left = 16
    Top = 16
    Width = 561
    Height = 497
  end
  object MSButton1: tMSButton
    Left = 591
    Top = 18
    Width = 130
    Height = 55
    Caption = 'MSButton1'
    TabOrder = 8
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSButton1,OnClick,MSButton1_Clicked
Show>Dialog1,res1

SRT>MSButton1_Clicked
  Let>screenshot=%temp_dir%calcwin.jpg
  SetFocus>Calculator
  GetWindowPos>Calculator,XX,YY
  GetWindowSize>Calculator,WW,HH
  Let>XX2=XX+WW
  Let>YY2=YY+HH
  ScreenCapture>XX,YY,XX2,YY2,%screenshot%
  WindowAction>2,Calculator
  SetDialogProperty>Dialog1,MSImage1,LoadImage,%screenshot%
END>MSButton1_Clicked
Yes, we have a Custom Scripting Service. Message me or go here

mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

Re: To print a window on a dialog image

Post by mhcha » Mon Jan 16, 2023 12:18 am

Thank you. What I want is...
Using the macro scheduler of the PrintWindow function in User32.dll.

The screenshot function you suggested is a function that is only possible if the window is visible on the monitor.
That's not what I want.

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: To print a window on a dialog image

Post by Marcus Tettmar » Mon Jan 16, 2023 11:54 am

The PrintWindow function is only going to work if the application in question responds to the WM_PRINT message and does something useful with it. Do you know if it does that? My bet is it doesn't. Most are unlikely to care. Most likely to be able to print a screenshot version of itself it also needs to render which means it needs to be visible and focused. Therefore it's going to be simpler to focus it and use ScreenCapture anyway. If you are wanting an actual screenshot then you would really need to do that. If it's some kind of screen which prints some textual representation of itself them things may be different but I doubt you want that.

See:
https://learn.microsoft.com/en-us/windo ... rintwindow

Note the bit that says:

"The application that owns the window referenced by hWnd processes the PrintWindow call and renders the image in the device context that is referenced by hdcBlt. The application receives a WM_PRINT message or, if the PW_PRINTCLIENT flag is specified, a WM_PRINTCLIENT message. For more information, see WM_PRINT and WM_PRINTCLIENT"

So you also need to create a device context and specify it. Note the above says this is typically a printer DC. Again, I think you want a screenshot not something printed to a printer, as this is designed for.

If you want a capture of something on the screen, use ScreenCapture . If you want a capture of something that is not visible, make it visible. You can't capture a screenshot of something that isn't visible.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

Re: To print a window on a dialog image

Post by mhcha » Mon Jan 16, 2023 1:30 pm

Dear Dorian, Marcus, Thank you for answering my question.

What I wanted was to get a specific window screen even if a specific window was covered like the image below.
https://drive.google.com/file/d/1GSFg94 ... share_link
I realized that there's a limit to macro schedulers.
So I'm planning to develop it with C#.
Nevertheless, I would appreciate it if you could let me know if you have a good opinion.

I love the simple code of the macro scheduler.
Thank you always for your help.

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: To print a window on a dialog image

Post by Marcus Tettmar » Tue Jan 17, 2023 1:37 pm

It's not a limit of "macro schedulers". Whatever you use, whether Macro Scheduler or C#, you will need to first focus the window to make it fully visible before you can get a screenshot of it. Good luck.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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