Rubber Band Selection Rectangle

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Rubber Band Selection Rectangle

Post by JRL » Fri Oct 12, 2007 3:06 pm

For a long time I've looked for a way to draw a selection window using Macro Scheduler. There are many scripts available online that are written in C or Java or multiple other programming languages that need compiling or an environment download to function. This script is pure Macro Scheduler seasoned with a little Windows API.

Speaking of Windows API. Thank you Marcus for your assistance making ShowScrollBar remove the scroll bars.

As posted, this script simply displays the X and Y coordinates of the selection window's upper left hand and lower right hand corners. One obvious functional use for those coordinates would be to create an image file of the selected portion of the screen using Macro Scheduler's ScreenCapture> command.

One deficiency of this script is that the user must select the points in the correct order. Upper left corner, then lower right corner. That is the only direction the rectangle (dialog window) can be drawn.

Hopefully someone will find this useful.

Code: Select all

//Capture the current screen contents to a file
GetScreenRes>ScreenX,ScreenY
ScreenCapture>0,0,%ScreenX%,%ScreenY%,%TEMP_DIR%Screencap~.bmp

//Capture left mouse click to get selection corners
//Capture Esc to kill process
OnEvent>KEY_DOWN,VK1,0,ExpandingWindow
OnEvent>KEY_DOWN,VK27,0,CloseWindow

//Initialize "BeenHere" variable for mouse click key control
Let>BeenHere=0
//Set a few values for windows API functions
Let>WS_THICKFRAME=262144
Let>WS_CHILD=1073741824
Let>HWND_TOPMOST=-1
Let>ESB_DISABLE_BOTH=3
Let>SB_BOTH=3

//Dialog to represent the desktop
Dialog>Dialog1
   Caption=Freeze
   Width=%ScreenX%
   Height=%ScreenY%
   Top=0
   Left=0
   Image=%TEMP_DIR%Screencap~.bmp,0,0,%ScreenX%,%ScreenY%
EndDialog>Dialog1

Show>Dialog1
//I don't know why this minimize step is necessary,  I just know that some computers I have
//will not lose their dialog borders without it.  Enable it only if necessary.

//WindowAction>2,Freeze

//Remove borders from the dialog that is the background
LibFunc>user32,SetWindowLongA,sres,Dialog1.handle,-16,WS_CHILD
CloseDialog>Dialog1
Show>Dialog1
WindowAction>0,Freeze

//Main loop
Label>Loop
  GetDialogAction>Dialog1,res4
  If>res4=2,EOF
  //BeenHere variable allows the mouse left click release to be detected
  If>BeenHere>1
    Add>BeenHere,1
  EndIf
  If>BeenHere>10,CloseWindow
  //CPU nap
  Wait>0.01
Goto>Loop

//End Of File
Label>EOF

//Selection window subroutine
SRT>ExpandingWindow
  //Grab the current mouse coordinates
  GetCursorPos>MouseX,MouseY
  //After the subroutine has been accessed once, skip setup.
  If>BeenHere>1,skipthis
  //selection window setup
  //Grab the current mouse coordinates, again to set firstX/Y variable
  GetCursorPos>FirstX,FirstY
  //Image offset from the screen image 0,0 to the selected mouse position
  Let>ImageOffsetX={(-1*%FirstX%)-3}
  Let>ImageOffsetY={(-1*%FirstY%)-3}

//Dialog to display offset image and expand with mouse movement
Dialog>Select1
   Caption=Select
   Width=0
   Height=0
   Top=%FirstX%
   Left=%FirstY%
   Image=%TEMP_DIR%Screencap~.bmp,%ImageOffsetX%,%ImageOffsetY%,%ScreenX%,%ScreenY%
EndDialog>Select1

  Show>Select1
  //Remove title bar and leave the border
  LibFunc>user32,SetWindowLongA,sres,Select1.handle,-16,WS_THICKFRAME
  CloseDialog>Select1
  Show>Select1
  //End of Setup
  Label>skipthis
    //Set variable "BeenHere" to 2 every time the subroutine is accessed, this keeps its
	//value greater than 1 (one) so that setup is accessed only once.  Also keeps its value less
	//than 10 while left mouse button is continuously pressed.  As soon as the left
	//mouse button is released "BeenHere" will stop being reset to 2 and the main loop will
	//increase its value to 10. As soon as its value bedcomes 10 the script will end.
    Let>BeenHere=2
	//As long as the mouse button is down,  subtract the original selected position
	//from the current cursor position and use the result as the window lower left location.
    Sub>MouseX,%FirstX%
    Sub>MouseY,%FirstY%
    LibFunc>User32,SetWindowPos,swpr,Select1.handle,HWND_TOPMOST,%FirstX%,%FirstY%,%MouseX%,%MouseY%,80
	//ShowScrollBar removes the scrollbars from the dialog window
    LibFunc>user32,ShowScrollBar,sbres,Select1.handle,SB_BOTH,0
    Wait>0.01
END>ExpandingWindow

SRT>CloseWindow
  //At the moment the left mouse button is released the active window is the selection box.
  //Get its coordinates so that they can be used.
  GetActiveWindow>Wintiltle,WinX,WinY,WinWide,WinHigh
  //Close the dialogs
  Closedialog>dialog1
  Closedialog>Select1
  //Manipulate the window coordinate data however it suits you
  Let>LRX={%WinX%+%WinWide%-3}
  Let>LRY={%WinY%+%WinHigh%-3}
  Add>WinX,3
  Add>WinY,3
  //Display or use the window coordinate data.
  MDL>The selection coodinates are:%CRLF%%CRLF%Upper Left X,Y%TAB%Lower Right X,Y%CRLF%%WinX%,%WinY%%TAB%%TAB%%LRX%,%LRY%
  Goto>EOF
END>CloseWindow

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Oct 12, 2007 5:14 pm

Hi JRL,

I just tried out your script. I click, drag down and to the right to enlarge the selected area and notice that the scrollbars do flicker into existence briefly before being made invisible again. I guess the flickering probably can't be helped at this point but its not a big deal, its great to be able to do this using only Macro Scheduler.

Very nice piece of code... thanks for sharing. Your contributions here on the forums are much appreciated.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 12, 2007 6:16 pm

Yes the scroll bars flicker as the window is dragged. Not nearly as annoying as scroll bars not going away at all which was where I was at last night. Perhaps there is a way to fix this that someone out there in forum land can find? In the mean time it beats what I was doing before which was pick one corner then pick another corner without being able to see the selection.

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 12, 2007 8:17 pm

Here's a sample that will print the image selection to your default printer.

Code: Select all

IfFileExists>%SYS_DIR%\mspaint.exe
  Let>paint=%SYS_DIR%\mspaint.exe
Else
  Let>RP_WAIT=0
  Let>RP_WINDOWMODE=0
  Run>cmd /c dir %WIN_DIR%\mspaint.exe /s /b > %TEMP_DIR%findmspaint.txt
  ReadLn>%TEMP_DIR%findmspaint.txt,1,paint
  DeleteFile>%TEMP_DIR%findmspaint.txt
  Separate>%paint%,\,fname
  Let>fname=fname_%fname_count%
  If>%fname%=mspaint.exe,PaintFound,PaintNotFound
EndIf

Label>PaintFound
IfFileExists>%TEMP_DIR%Captured~~.BMP
  DeleteFile>%TEMP_DIR%Captured~~.BMP
EndIf


//Capture the current screen contents to a file
GetScreenRes>ScreenX,ScreenY
ScreenCapture>0,0,%ScreenX%,%ScreenY%,%TEMP_DIR%Screencap~.bmp

//Capture left mouse click to get selection corners
//Capture Esc to kill process
OnEvent>KEY_DOWN,VK1,0,ExpandingWindow
OnEvent>KEY_DOWN,VK27,0,CloseWindow

//Initialize "BeenHere" variable for mouse click key control
Let>BeenHere=0
//Set a few values for windows API functions
Let>WS_THICKFRAME=262144
Let>WS_CHILD=1073741824
Let>HWND_TOPMOST=-1
Let>ESB_DISABLE_BOTH=3
Let>SB_BOTH=3

//Dialog to represent the desktop
Dialog>Dialog1
   Caption=Freeze
   Width=%ScreenX%
   Height=%ScreenY%
   Top=0
   Left=0
   Image=%TEMP_DIR%Screencap~.bmp,0,0,%ScreenX%,%ScreenY%
EndDialog>Dialog1

Show>Dialog1
//I don't know why this minimize step is necessary,  I just know that some computers I have
//will not lose their dialog borders without it.  Enable it only if necessary.

//WindowAction>2,Freeze

//Remove borders from the dialog that is the background
LibFunc>user32,SetWindowLongA,sres,Dialog1.handle,-16,WS_CHILD
CloseDialog>Dialog1
Show>Dialog1
WindowAction>0,Freeze

//Main loop
Label>Loop
  GetDialogAction>Dialog1,res4
  If>res4=2,EOF
  //BeenHere variable allows the mouse left click release to be detected
  If>BeenHere>1
    Add>BeenHere,1
  EndIf
  If>BeenHere>10,CloseWindow
  //CPU nap
  Wait>0.01
Goto>Loop

Goto>EOF
Label>PaintNotFound
MDL>Microsoft Paint was not found on this computer.

//End Of File
Label>EOF

//Selection window subroutine
SRT>ExpandingWindow
  //Grab the current mouse coordinates
  GetCursorPos>MouseX,MouseY
  //After the subroutine has been accessed once, skip setup.
  If>BeenHere>1,skipthis
  //selection window setup
  //Grab the current mouse coordinates, again to set firstX/Y variable
  GetCursorPos>FirstX,FirstY
  //Image offset from the screen image 0,0 to the selected mouse position
  Let>ImageOffsetX={(-1*%FirstX%)-3}
  Let>ImageOffsetY={(-1*%FirstY%)-3}

//Dialog to display offset image and expand with mouse movement
Dialog>Select1
   Caption=Select
   Width=0
   Height=0
   Top=%FirstX%
   Left=%FirstY%
   Image=%TEMP_DIR%Screencap~.bmp,%ImageOffsetX%,%ImageOffsetY%,%ScreenX%,%ScreenY%
EndDialog>Select1

  Show>Select1
  //Remove title bar and leave the border
  LibFunc>user32,SetWindowLongA,sres,Select1.handle,-16,WS_THICKFRAME
  CloseDialog>Select1
  Show>Select1
  //End of Setup
  Label>skipthis
    //Set variable "BeenHere" to 2 every time the subroutine is accessed, this keeps its
	//value greater than 1 (one) so that setup is accessed only once.  Also keeps its value less
	//than 10 while left mouse button is continuously pressed.  As soon as the left
	//mouse button is released "BeenHere" will stop being reset to 2 and the main loop will
	//increase its value to 10. As soon as its value bedcomes 10 the script will end.
    Let>BeenHere=2
	//As long as the mouse button is down,  subtract the original selected position
	//from the current cursor position and use the result as the window lower left location.
    Sub>MouseX,%FirstX%
    Sub>MouseY,%FirstY%
    LibFunc>User32,SetWindowPos,swpr,Select1.handle,HWND_TOPMOST,%FirstX%,%FirstY%,%MouseX%,%MouseY%,80
	//ShowScrollBar removes the scrollbars from the dialog window
    LibFunc>user32,ShowScrollBar,sbres,Select1.handle,SB_BOTH,0
    Wait>0.01
END>ExpandingWindow

SRT>CloseWindow
  //At the moment the left mouse button is released the active window is the selection box.
  //Get its coordinates so that they can be used.
  GetActiveWindow>Wintiltle,WinX,WinY,WinWide,WinHigh
  //Close the dialogs
  Closedialog>dialog1
  Closedialog>Select1
  //Manipulate the window coordinate data however it suits you
  Let>LRX={%WinX%+%WinWide%-3}
  Let>LRY={%WinY%+%WinHigh%-3}
  Add>WinX,3
  Add>WinY,3
   //Capture the selection to the clipboard and to a file  
ScreenCapture>%WinX%,%WinY%,%LRX%,%LRY%,%TEMP_DIR%Captured~~.BMP
  //Ask to print or not
  Ask>Print Selection?,answer
  If>answer=NO
    //Display or use the window coordinate data.
    MDL>The selection coodinates are:%CRLF%%CRLF%Upper Left X,Y%TAB%Lower Right X,Y%CRLF%%WinX%,%WinY%%TAB%%TAB%%LRX%,%LRY%
  Else
    //If yes is selected, print the file using mspaint command line
    Let>RP_WAIT=1
    Run>%paint% /p %TEMP_DIR%Captured~~.BMP
  EndIf
  IfFileExists>%TEMP_DIR%Captured~~.BMP
    DeleteFile>%TEMP_DIR%Captured~~.BMP
  EndIf
  Goto>EOF
END>CloseWindow


User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 12, 2007 10:30 pm

Here's an example that will use the gpulawski mentioned here to select text from the screen and paste it into notepad.

This basic code is slightly different than the first two, I added two more instances of the ShowScrollBar to try to make the scroll bars turn off sooner.

Code: Select all

VBStart
Function DoOCR(bitmapfile)
  Dim miDoc
  Dim miLayout
  Dim stringOut

  set miDoc=CreateObject("MODI.Document")
  miDoc.Create (bitmapfile)
  ' Perform OCR.
  'You can change the mousepointer here to an hourglass or something.
  miDoc.Images(0).OCR
  'Change the mouse back to normal default.
  set miLayout = miDoc.Images(0).Layout
  stringOut=miLayout.Text
  'MsgBox(stringOut)
  DoOCR = stringOut
  Set miLayout = Nothing
  Set miDoc = Nothing
End Function
VBEND


IfFileExists>%TEMP_DIR%Captured~~.BMP
  DeleteFile>%TEMP_DIR%Captured~~.BMP
EndIf


//Capture the current screen contents to a file
GetScreenRes>ScreenX,ScreenY
ScreenCapture>0,0,%ScreenX%,%ScreenY%,%TEMP_DIR%Screencap~.bmp

//Capture left mouse click to get selection corners
//Capture Esc to kill process
OnEvent>KEY_DOWN,VK1,0,ExpandingWindow
OnEvent>KEY_DOWN,VK27,0,CloseWindow

//Initialize "BeenHere" variable for mouse click key control
Let>BeenHere=0
//Set a few values for windows API functions
Let>WS_THICKFRAME=262144
Let>WS_CHILD=1073741824
Let>HWND_TOPMOST=-1
Let>ESB_DISABLE_BOTH=3
Let>SB_BOTH=3

//Dialog to represent the desktop
Dialog>Dialog1
   Caption=Freeze
   Width=%ScreenX%
   Height=%ScreenY%
   Top=0
   Left=0
   Image=%TEMP_DIR%Screencap~.bmp,0,0,%ScreenX%,%ScreenY%
EndDialog>Dialog1

Show>Dialog1
//I don't know why this minimize step is necessary,  I just know that some computers I have
//will not lose their dialog borders without it.  Enable it only if necessary.

//WindowAction>2,Freeze

//Remove borders from the dialog that is the background
LibFunc>user32,SetWindowLongA,sres,Dialog1.handle,-16,WS_CHILD
CloseDialog>Dialog1
Show>Dialog1
WindowAction>0,Freeze

//Main loop
Label>Loop
  GetDialogAction>Dialog1,res4
  If>res4=2,EOF
  //BeenHere variable allows the mouse left click release to be detected
  If>BeenHere>1
    LibFunc>user32,ShowScrollBar,sbres,Select1.handle,SB_BOTH,0
    Add>BeenHere,1
  EndIf
  If>BeenHere>10,CloseWindow
  //CPU nap
  //Wait>0.001
Goto>Loop


//End Of File
Label>EOF

//Selection window subroutine
SRT>ExpandingWindow
  //Grab the current mouse coordinates
  GetCursorPos>MouseX,MouseY
  LibFunc>user32,ShowScrollBar,sbres,Select1.handle,SB_BOTH,0
  //After the subroutine has been accessed once, skip setup.
  If>BeenHere>1,skipthis
  //selection window setup
  //Grab the current mouse coordinates, again to set firstX/Y variable
  GetCursorPos>FirstX,FirstY
  //Image offset from the screen image 0,0 to the selected mouse position
  Let>ImageOffsetX={(-1*%FirstX%)-3}
  Let>ImageOffsetY={(-1*%FirstY%)-3}

//Dialog to display offset image and expand with mouse movement
Dialog>Select1
   Caption=Select
   Width=0
   Height=0
   Top=%FirstX%
   Left=%FirstY%
   Image=%TEMP_DIR%Screencap~.bmp,%ImageOffsetX%,%ImageOffsetY%,%ScreenX%,%ScreenY%
EndDialog>Select1

  Show>Select1
  //Remove title bar and leave the border
  LibFunc>user32,SetWindowLongA,sres,Select1.handle,-16,WS_THICKFRAME
  CloseDialog>Select1
  Show>Select1
  //End of Setup
  Label>skipthis
    //Set variable "BeenHere" to 2 every time the subroutine is accessed, this keeps its
	//value greater than 1 (one) so that setup is accessed only once.  Also keeps its value less
	//than 10 while left mouse button is continuously pressed.  As soon as the left
	//mouse button is released "BeenHere" will stop being reset to 2 and the main loop will
	//increase its value to 10. As soon as its value bedcomes 10 the script will end.
    Let>BeenHere=2
	//As long as the mouse button is down,  subtract the original selected position
	//from the current cursor position and use the result as the window lower left location.
    Sub>MouseX,%FirstX%
    Sub>MouseY,%FirstY%
    LibFunc>User32,SetWindowPos,swpr,Select1.handle,HWND_TOPMOST,%FirstX%,%FirstY%,%MouseX%,%MouseY%,80
	//ShowScrollBar removes the scrollbars from the dialog window
    LibFunc>user32,ShowScrollBar,sbres,Select1.handle,SB_BOTH,0
    //Wait>0.001
END>ExpandingWindow

SRT>CloseWindow
  //At the moment the left mouse button is released the active window is the selection box.
  //Get its coordinates so that they can be used.
  GetActiveWindow>Wintiltle,WinX,WinY,WinWide,WinHigh
  //Close the dialogs
  Closedialog>dialog1
  Closedialog>Select1
  //Manipulate the window coordinate data however it suits you
  Let>LRX={%WinX%+%WinWide%-3}
  Let>LRY={%WinY%+%WinHigh%-3}
  Add>WinX,3
  Add>WinY,3
  ScreenCapture>%WinX%,%WinY%,%LRX%,%LRY%,%TEMP_DIR%Captured~~.BMP
  //Display or use the window coordinate data.
  Ask>Display Selection?,answer
  If>answer=NO
    //MDL>The selection coodinates are:%CRLF%%CRLF%Upper Left X,Y%TAB%Lower Right X,Y%CRLF%%WinX%,%WinY%%TAB%%TAB%%LRX%,%LRY%
  Else
    //Use OCR to get text from active window
    VBEval>DoOCR("%TEMP_DIR%Captured~~.BMP"),TheText
	PutClipBoard>%TheText%
	Let>RP_WAIT=0
    Run>notepad.exe
	//Not good to just wait but I'm in a hurry
	wait>2
	SetFocus>NotePad*
	Press Ctrl
	Send>v
	Release ctrl
  EndIf
  IfFileExists>%TEMP_DIR%Captured~~.BMP
    DeleteFile>%TEMP_DIR%Captured~~.BMP
  EndIf
  Goto>EOF
END>CloseWindow

A new and greatly improved image capture.

Code: Select all

/*=========================================
Rubber Band Selection Rectangle by Dick Lockey
October 25, 2007
===========================================
*/

//Convert TEMP_DIR path from 8.3 format to Windows long format.
//VBScript needs a file to process
Let>temp-file=%TEMP_DIR%~text~bug~.txt
WriteLn>%temp-file%,wresult,delete this file

VBSTART
Dim TargetLong
function LongName(shortpath)
TargetShort = shortpath
Set oShell = CreateObject("WScript.Shell")
Set oShellLink = oShell.CreateShortcut("c:\foo.lnk")
oShellLink.TargetPath = TargetShort
TargetLong = oShellLink.TargetPath
End function
VBEND

VBEval>LongName("%temp-file%"),longpath
VBEval>TargetLong,longpath

DeleteFile>%TEMP_DIR%~text~bug~.txt
Separate>longpath,~text~bug~.txt,var
Let>TEMP-DIR=%var_1%

//Make these two variables any valid path and BMP or JPG filenames
Let>CaptureFilename=%TEMP-DIR%Screencap~.bmp
Let>TempScreenCapFilename=%TEMP-DIR%Screencap~.bmp

IfFileExists>%TEMP-DIR%Screencap~.bmp
  DeleteFile>%TEMP-DIR%Screencap~.bmp
EndIf

//Opacity of window 0 = clear 255 = completely opaque
Let>byteOpacity=150


Let>startflag=0
Let>refreshflag=0
Let>NoClickFlag=0
Let>ClickHoldFlag=0

GetScreenRes>ScreenX,ScreenY
ScreenCapture>0,0,%ScreenX%,%ScreenY%,%TempScreenCapFilename%

//Let>ScreenCenterX=%ScreenX%/2
//Sub>ScreenCenterX,30
Let>Width=X
Let>Height=Y
Let>CoordsOnFlag=1
Let>comma=,

//Dialog to represent the desktop
Dialog>Frozen1
   Caption=Freeze
   Width=%ScreenX%
   Height=%ScreenY%
   Top=0
   Left=0
   //Image=c:\bytesize.bmp,0,0,%ScreenX%,%ScreenY%,newimage
   Image=%TempScreenCapFilename%,0,0,%ScreenX%,%ScreenY%,newimage
EndDialog>Frozen1

//Translucent dialog to draw on
Dialog>Frozen2
   Caption=Freeze2
   Width=%ScreenX%
   Height=%ScreenY%
   Top=-1000
   Left=-1000
   Button=%Width%%comma%%Height%,-10000,50,260,20,100
   //Button=Selection saved to file... %TempScreenCapFilename%...     Selection also saved to clipboard,-10000,50,%ScreenX%,20,120
   //Font=Ariel,12
EndDialog>Frozen2

SetDialogObjectFont>Frozen2,msButton1,Arial,14,1,255

Show>Frozen1
//I don't know why this minimize step is necessary,  I just know that some computers I have
//will not lose their dialog borders without it.  Enable it only if necessary.

WindowAction>2,Freeze

//Remove borders from the dialog that is the background
Let>WS_CHILD=1073741824
LibFunc>user32,SetWindowLongA,sres,Frozen1.handle,-16,WS_CHILD
CloseDialog>Frozen1
Show>Frozen1
WindowAction>0,Freeze

Show>Frozen2
SetDialogObjectFont>frozen2,msbutton1,Arial,12,1,255
//I don't know why this minimize step is necessary,  I just know that some computers I have
//will not lose their dialog borders without it.  Enable it only if necessary.

WindowAction>2,Freeze2

//Remove borders from the dialog that is the background
Let>WS_CHILD=1073741824
LibFunc>user32,SetWindowLongA,sres,Frozen2.handle,-16,WS_CHILD
CloseDialog>Frozen2
Show>Frozen2
WindowAction>0,Freeze2

//constants
Let>GWL_EXSTYLE=-20
Let>WS_EX_LAYERED=524288
Let>LWA_ALPHA=2

//get style attributes of window
LibFunc>user32,GetWindowLongA,attribs,%frozen2.handle%,GWL_EXSTYLE
Let>attribs={%attribs% OR %WS_EX_LAYERED%}

//make window transparent
LibFunc>user32,SetWindowLongA,swl,%frozen2.handle%,GWL_EXSTYLE,attribs
LibFunc>user32,SetLayeredWindowAttributes,res,%frozen2.handle%,0,byteOpacity,LWA_ALPHA

MoveWindow>Freeze,0,0
MoveWindow>Freeze2,0,0

//Subroutine executes when Esc key is pressed
SRT>Quit
Goto>EOF
END>Quit

//Subroutine executes when Mouse Click button is pressed
SRT>Click
If>NoClickFlag>0,SkipDraw
  Add>refreshflag,1
  If>refreshflag>1
	Let>Frozen1.newimage=%TempScreenCapFilename%
	Rda>Frozen1
	Let>refreshflag=0
  EndIf
  If>startflag=0
    GetCursorPos>StartX,StartY
	Let>startflag=1
	LibFunc>user32,GetDC,HDC,0
    Libfunc>gdi32,MoveToEx,mtres,HDC,StartX,StartY,0
	Let>LabY=%StartY%-20
	///*
	Let>WIN_USEHANDLE=1
	MoveWindow>frozen2.msbutton1.handle,%StartX%,%LabY%
	Sub>LabY,20
	MoveWindow>frozen2.msbutton2.handle,0,%LabY%
	Let>WIN_USEHANDLE=0
  EndIf
  If>StartFlag>1
    Let>StartFlag=1
  EndIf
    GetCursorPos>CurX,CurY
    Libfunc>gdi32,LineTo,ltres,HDC,StartX,CurY
	Libfunc>gdi32,LineTo,ltres,HDC,CurX,CurY
	Libfunc>gdi32,LineTo,ltres,HDC,CurX,StartY
	Libfunc>gdi32,LineTo,ltres,HDC,StartX,StartY

	Let>Width={abs(%StartX%-%CurX%)}
	Let>Height={abs(%StartY%-%CurY%)}
	Let>CoordsXPos=%CurX%+2
	Let>CoordsYPos=%CurY%-22
	SetFocus>Freeze2
	Let>frozen2.msbutton1=Position:X%StartX% Y%StartY%   Size:%Width%x%Height%
	
	Rda>Frozen2
	Wait>0.001
Label>SkipDraw
GoSub>StartSub
END>Click

SRT>Finish
ReadIniFile>C:\Program Files\IrfanView\i_view32.ini,Print,Orient,Portrait
If>Portrait=,PrintAppNotFound
Sub>Portrait,1
ReadIniFile>C:\Program Files\IrfanView\i_view32.ini,Print,Centered,Center
If>Center=1
  Let>Cent=True
Else
  Let>Cent=False
EndIf
Let>SkipPrint=0


Dialog>Dialog21
   Caption=Print
   Width=274
   Height=223
   Top=CENTER
   Left=CENTER
   Label=Do you want to print your selection?%CRLF%%CRLF%File and clipboard will still contain your%CRLF%selection image if  [No]  is picked.,32,24,true
   Button=Yes,24,88,75,25,3
   Button=No,160,88,75,25,2
   CheckBox=msCheckBox1,Centered,24,144,97,%Cent%
   RadioGroup=msRadioGroup1,Orientation,136,136,129,49,Portrait%CRLF%Landscape,%Portrait%
EndDialog>Dialog21

Show>dialog21,res21

  If>res21=2
    Let>SkipPrint=1
  EndIf

  CloseDialog>Frozen1
  CloseDialog>Frozen2

  Let>Portrait=dialog21.msradiogroup1.itemindex
Add>Portrait,1

If>dialog21.mscheckbox1=False
  Let>Center=0
Else
  Let>Center=1
EndIf
EditIniFile>C:\Program Files\IrfanView\i_view32.ini,Print,Orient,Portrait
EditIniFile>C:\Program Files\IrfanView\i_view32.ini,Print,Centered,Center

Let>PrintApplication=C:\Program Files\IrfanView\i_view32.exe
IfFileExists>%PrintApplication%
  Let>PrintApp=%PrintApplication%
Else
  Let>RP_WAIT=0
  Let>RP_WINDOWMODE=0
  Run>cmd /c dir "C:\Program Files\i_view32.exe" /s /b > %TEMP-DIR%findPrintApp.txt
  ReadLn>%TEMP-DIR%findPrintApp.txt,1,PrintApp
  DeleteFile>%TEMP-DIR%findPrintApp.txt
  Separate>%PrintApp%,\,fname
  Let>fname=fname_%fname_count%
  If>%fname%=i_view32.exe,PrintAppFound,PrintAppNotFound
EndIf

Label>PrintAppFound
  If>%CurX%>%StartX%
    Let>X1=%StartX%
    Let>X2=%CurX%
  Else
    Let>X1=%CurX%
    Let>X2=%StartX%
  EndIf
  If>%CurY%>%StartY%
    Let>Y1=%StartY%
    Let>Y2=%CurY%
  Else
    Let>Y1=%CurY%
    Let>Y2=%StartY%
  EndIf
  CloseDialog>Frozen1
  CloseDialog>Frozen2
  ScreenCapture>%X1%,%Y1%,%X2%,%Y2%,%CaptureFilename%
  If>SkipPrint=1,EOF
  Let>RP_WAIT=1
  Run>%PrintApp% %CaptureFilename% /print
  Goto>EOF
END>Finish

//VK1=Click;VK27=Esc;VK38=Up;Vk40=Down;VK17=Ctrl
//Press Escape key to quit at any time
OnEvent>KEY_DOWN,VK1,0,Click
OnEvent>KEY_DOWN,VK27,0,Quit
Onevent>KEY_DOWN,VK38,0,Up
Onevent>KEY_DOWN,VK40,0,Down
Onevent>KEY_DOWN,VK17,2,ClickHold

SRT>ClickHold
  If>ClickHoldFlag=0
    Let>StartFlag=0
	Repeat>NoClickFlag
	  add>NoClickFlag,1
	  Wait>0.01
	Until>NoClickFlag=20
	Let>NoClickFlag=0
  EndIf
  //Let>NoClickFlag=1
  Let>ClickHoldFlag=1
  Wait>0.01
END>ClickHold

SRT>Up
Let>NoClickFlag=1
Let>ClickHoldFlag=1
Add>byteOpacity,8
LibFunc>user32,SetLayeredWindowAttributes,res,%frozen2.handle%,0,byteOpacity,LWA_ALPHA
 Wait>0.001
 GoSub>StartSub
END>Up

SRT>Down
Let>NoClickFlag=1
Let>ClickHoldFlag=1
Sub>byteOpacity,8
LibFunc>user32,SetLayeredWindowAttributes,res,%frozen2.handle%,0,byteOpacity,LWA_ALPHA
 Wait>0.001
 GoSub>StartSub
END>Down

Let>MSG_XPOS=0
Let>MSG_YPOS=0
Let>MSG_HEIGHT=400
Let>VarReport=Clickholdflag=%Clickholdflag%%CRLF%
Concat>VarReport,NoClickFlag=%NoClickFlag%%CRLF%
Concat>VarReport,StartFlag=%StartFlag%%CRLF%
Concat>VarReport,refreshflag=%refreshflag%%CRLF%
Concat>VarReport,byteOpacity=%byteOpacity%%CRLF%
Concat>VarReport,%StartX%%CRLF%
Concat>VarReport,%StartY%%CRLF%
Concat>VarReport,%CurX%%CRLF%
Concat>VarReport,%CurY%%CRLF%
Concat>VarReport,HDC=%HDC%%CRLF%

Label>start
  GoSub>StartSub
Goto>start

SRT>StartSub
  GetDialogAction>Frozen1,res4
  If>res4=2,EOF

  If>ClickHoldFlag<>0
    Add>ClickHoldFlag,1
    If>ClickHoldFlag>2
	  Let>ClickHoldFlag=0
	EndIf
  EndIf

  If>NoClickFlag<>0
    Add>NoClickFlag,1
  Else
    If>StartFlag<>0
      Add>StartFlag,1
    EndIf
    If>StartFlag>20,Finish
  EndIf
  If>NoClickFlag>20
    Let>NoClickFlag=0
  EndIf

  Wait>0.01
  Let>frozen2.msbutton0=Position:X%StartX% Y%StartY%   Size:%Width%x%Height%
  RDA>frozen2
//Unremark to watch variables
/*
Let>VarReport=Clickholdflag=%Clickholdflag%%CRLF%
Concat>VarReport,NoClickFlag=%NoClickFlag%%CRLF%
Concat>VarReport,StartFlag=%StartFlag%%CRLF%
Concat>VarReport,refreshflag=%refreshflag%%CRLF%
Concat>VarReport,byteOpacity=%byteOpacity%%CRLF%
Concat>VarReport,%StartX%%CRLF%
Concat>VarReport,%StartY%%CRLF%
Concat>VarReport,%CurX%%CRLF%
Concat>VarReport,%CurY%%CRLF%
Concat>VarReport,HDC=%HDC%%CRLF%
Message>%VarReport%
*/
END>StartSub

Label>PrintAppNotFound
MDL>This program uses IrFanView to print%CRLF%IrFanView not found on this computer%CRLF%%CRLF%www.irfanview.com

Label>EOF

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