How to capture screen by code

How to capture screen by code

Postby hua » Thu Feb 23, 2023 5:10 am

Hi guys,
I was tasked to write code based on the following pseudocode. I have no idea where to start

i. Run external exe that will display numbers // this part is straight forward
ii. Once it displayed, pop a dialog box at the edge of the external exe's window // 2 issues. Determining when the external exe appear and positioning my dialog box
iii. If user click Ok on the dialog, create a screenshot and email it // what's the code to do screenshot

TIA
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: How to capture screen by code

Postby Marc Venken » Thu Feb 23, 2023 8:21 am

For the screenshot this could work. Part of a error log/send function

In the main section :

SetPostErrorAction( { |cErrorLogFileName, oError| MyErrorAction( cErrorLogFileName, oError ) } )


Code: Select all  Expand view  RUN
function MyErrorAction( cErrorLogFileName, oError )
    LOCAL cErrScreen:=""
    LOCAL cFile := "screen.bmp", cErrScreen:=""
    local hBmp := WndBitmap( GetDesktopWindow() )
    local hDib := DibFromBitmap( hBmp )
    local cErrtext := memoread("error.log")
    local nKwaliteit := 72
    DibWrite( cFile, hDib )
    GloBalFree( hDib )
    DeleteObject( hBmp )
    //  Second option ?
    oImage   := GdiBmp():New( "screen.bmp" )
    oImage:Resize( 600 )
    oImage:Save( "small.bmp", 72 )
    oImage:End()

    msginfo(cErrorLogFileName)
    cErrScreen = memoread( "small.bmp" )
    xbrowser(oCn)
    FWCONNECT oCn HOST cServer USER cUser PASSWORD cPassword DATABASE cDatabase

    if oCn == nil
      ? "Failed to connect"
      return nil
    endif

    oCn:Insert( "errors", "datum,uur,user,error,errorlog", { date(),time(), setup_naam, oError:dEscription,cErrText } )
    msginfo("Er is een fout opgetreden in het programma.  Gelieve het programma opnieuw te starten")

return NIL
 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1446
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: How to capture screen by code

Postby Natter » Thu Feb 23, 2023 8:23 am

You can try this (by timer)

Code: Select all  Expand view  RUN
procedore Finf_File()
local st, fl
local oList, oProc
private dim:={{upper(MyExe), "TWINDOW", {}}}

  EnumChildWindows( 0, ;
      { | hWn, nLParam | L_Tsk(hWn, "window title" }, 0 ) // optional supplied value

  oList:= oWmi:ExecQuery( "select * from Win32_Process where Name = '" + dim[1,1] + "'")
  for each oProc in oList // List of similar processes
    fl:=ascan(dim[1,3], {|xx|xx[1]=oProc:ProcessId})

    if fl>0 //found your exe file
      *** you code ***
    endif
  next
return

Function L_Tsk(hWn, ttl) && CALLBACK EnumChildWindows
local fl
** hWn - handle window 
** ttll - window title 

  DEFAULT ttl:=""

  fl:=ascan(dim, {|xx|upper(xx[2])=upper(GetClassName(hWn))}) // process name
  if fl>0.and.IsWindowVisible(hWn).and.ttl==GetWindowText(hWn)
      aadd(dim[fl,3], {GetWindowProcessID(hCt), hWn})
  endif
return .T.

** It would be useful to keep this function in the library **
FUNCTION WMIService() && It would be useful to keep this function in the library
local oLocator

  if oWMI == nil
    oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
    oWMI   := oLocator:ConnectServer()
  endif
return oWMI

***********************************
#pragma BEGINDUMP

#include "windows.h"
#include "hbapi.h"

HB_FUNC( GETWINDOWPROCESSID ) // hWnd --> nProcessId
{
   DWORD dwProcessId;

   GetWindowThreadProcessId( ( HWND ) hb_parnl( 1 ), &dwProcessId );

   hb_retnl( dwProcessId );
}
#pragma ENDDUMP
Natter
 
Posts: 1224
Joined: Mon May 14, 2007 9:49 am

Re: How to capture screen by code

Postby Marc Venken » Thu Feb 23, 2023 8:38 am

I check if my program is already running : (Maybe put this in a loop and background process)

Code: Select all  Expand view  RUN
  IF IsExeRunning( cFileName( HB_ARGV( 0 ) ) )
      msginfo("Program is running")
      QUIT
   ENDIF
 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1446
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 88 guests