import winax = require ( 'winax' ) ; import { execSync } from 'child_process' ; import * as path from 'path' ; import { Coordinate , DmRet , ErrorDisplay , FindDir , FindRet , GetWindowFlag , KeyState , OcrRet , Size , WindowState } from './types' ; type DisplayType = 'normal' | 'gdi' | 'gdi2' | 'dx' | 'dx2' | 'dx3' ; type MouseType = 'normal' | 'windows' | 'windows2' | 'windows3' | 'dx' | 'dx2' ; type KeypadType = 'normal' | 'windows' | 'dx' ; type FindPicDir = FindDir . LeftToRightAndTopToBottom | FindDir . LeftToRightAndBottomToTop | FindDir . RightToLeftAndTopToBottom | FindDir . RightToLeftAndBottomToTop ; function getDM ( ) { try { return new winax . Object ( 'dm.dmsoft' ) ; } catch ( e ) { execSync ( `regsvr32 ${ path . resolve ( __dirname , 'dm.dll' ) } /s` ) ; return new winax . Object ( 'dm.dmsoft' ) ; } } const dm = getDM ( ) ; let mouseRange ; function setMouseRange ( ) ; function setMouseRange ( x1 , y1 , x2 , y2 ) ; function setMouseRange ( ) { if ( arguments . length === 4 ) mouseRange = Array . from ( arguments ) ; else mouseRange = undefined ; } export = { dll : dm , getPath ( ) { return dm . GetPath ( ) ; } , setPath ( path ) { return dm . SetPath ( path ) ; } , setErrorDisplay ( flag ) { return dm . SetShowErrorMsg ( flag ) ; } , getCursorPos ( ) { let x = new winax . Variant ( - 1 , 'byref' ) ; let y = new winax . Variant ( - 1 , 'byref' ) ; dm . GetCursorPos ( x , y ) ; return { x : Number ( x ) , y : Number ( y ) } ; } , getKeyState ( keyCode ) { return dm . GetKeyState ( keyCode ) ; } , setMouseRange , moveTo ( x , y ) { if ( mouseRange ) { if ( x < mouseRange [ 0 ] ) x = mouseRange [ 0 ] ; else if ( x > mouseRange [ 2 ] ) x = mouseRange [ 2 ] ; if ( y < mouseRange [ 1 ] ) y = mouseRange [ 1 ] ; else if ( y > mouseRange [ 3 ] ) y = mouseRange [ 3 ] ; } return dm . MoveTo ( x , y ) ; } , leftClick ( ) { return dm . LeftClick ( ) ; } , leftDoubleClick ( ) { return dm . LeftDoubleClick ( ) ; } , leftDown ( ) { return dm . LeftDown ( ) ; } , leftUp ( ) { return dm . LeftUp ( ) ; } , rightClick ( ) { return dm . RightClick ( ) ; } , rightDown ( ) { return dm . RightDown ( ) ; } , rightUp ( ) { return dm . RightUp ( ) ; } , wheelDown ( ) { return dm . WheelDown ( ) ; } , wheelUp ( ) { return dm . WheelUp ( ) ; } , keyPress ( keyCode ) { return dm . KeyPress ( keyCode ) ; } , keyDown ( keyCode ) { return dm . KeyDown ( keyCode ) ; } , keyUp ( keyCode ) { return dm . KeyUp ( keyCode ) ; } , findWindow ( className , title , parentHWnd ? ) { const hWnd = parentHWnd ? this . enumWindow ( className , title , 3 , parentHWnd ) [ 0 ] : dm . FindWindow ( className , title ) ; if ( hWnd ) return hWnd ; } , enumWindow ( className , title , filter , parentHWnd = 0 ) { const wins = dm . EnumWindow ( parentHWnd , title , className , filter ) ; return wins . length > 0 ? wins . split ( ',' ) . map ( hWnd => Number ( hWnd ) ) : [ ] ; } , getWindow ( hWnd , flag ) { return dm . GetWindow ( hWnd , flag ) ; } , getPointWindow ( x , y ) { return dm . GetPointWindow ( x , y ) ; } , getClientSize ( hWnd ) { let width = new winax . Variant ( - 1 , 'byref' ) ; let height = new winax . Variant ( - 1 , 'byref' ) ; const ret = dm . GetClientSize ( hWnd , width , height ) ; if ( ret ) { return { width : Number ( width ) , height : Number ( height ) } ; } } , moveWindow ( hWnd , x , y ) { return dm . MoveWindow ( hWnd , x , y ) ; } , setWindowSize ( hWnd , width , height ) { return dm . SetWindowSize ( hWnd , width , height ) ; } , setWindowState ( hWnd , state ) { return dm . SetWindowState ( hWnd , state ) ; } , sendPaste ( hWnd ) { return dm . sendPaste ( hWnd ) ; } , sendString ( hWnd , content ) { return dm . SendString ( hWnd , content ) ; } , bindWindow ( hWnd , display , mouse , keypad , mode ) { return dm . BindWindow ( hWnd , display , mouse , keypad , mode ) ; } , unBindWindow ( ) { return dm . UnBindWindow ( ) ; } , capture ( x1 , y1 , x2 , y2 , fileName ) { return dm . Capture ( x1 , y1 , x2 , y2 , fileName ) ; } , findPic ( x1 , y1 , x2 , y2 , picName , deltaColor , sim , dir ) { let x = new winax . Variant ( - 1 , 'byref' ) ; let y = new winax . Variant ( - 1 , 'byref' ) ; const index = dm . FindPic ( x1 , y1 , x2 , y2 , picName , deltaColor , sim , dir , x , y ) ; if ( index !== - 1 ) { return { x : Number ( x ) , y : Number ( y ) , index } ; } } , findPicEx ( x1 , y1 , x2 , y2 , picName , deltaColor , sim , dir ) { const ret = dm . FindPicEx ( x1 , y1 , x2 , y2 , picName , deltaColor , sim , dir ) ; if ( ret . length > 0 ) { return ret . split ( '|' ) . map ( ( pic ) => { const [ index , x , y ] = pic . split ( ',' ) ; return { index : Number ( index ) , x : Number ( x ) , y : Number ( y ) } ; } ) ; } else return [ ] ; } , getColor ( x , y ) { return dm . GetColor ( x , y ) ; } , getColorNum ( x1 , y1 , x2 , y2 , color , sim ) { return dm . GetColorNum ( x1 , y1 , x2 , y2 , color , sim ) ; } , getAveRGB ( x1 , y1 , x2 , y2 ) { return dm . GetAveRGB ( x1 , y1 , x2 , y2 ) ; } , findColor ( x1 , y1 , x2 , y2 , color , sim , dir ) { let x = new winax . Variant ( - 1 , 'byref' ) ; let y = new winax . Variant ( - 1 , 'byref' ) ; const ret = dm . FindColor ( x1 , y1 , x2 , y2 , color , sim , dir , x , y ) ; if ( ret ) { return { x : Number ( x ) , y : Number ( y ) } ; } } , getNowDict ( ) { return dm . GetNowDict ( ) ; } , setDict ( index , file ) { return dm . SetDict ( index , file ) ; } , findStr ( x1 , y1 , x2 , y2 , str , colorFormat , sim ) { let x = new winax . Variant ( - 1 , 'byref' ) ; let y = new winax . Variant ( - 1 , 'byref' ) ; const index = dm . FindStr ( x1 , y1 , x2 , y2 , str , colorFormat , sim , x , y ) ; if ( index !== - 1 ) { return { index , x : Number ( x ) , y : Number ( y ) } ; } } , ocr ( x1 , y1 , x2 , y2 , colorFormat , sim ) { return dm . Ocr ( x1 , y1 , x2 , y2 , colorFormat , sim ) ; } , getWords ( x1 , y1 , x2 , y2 , colorFormat , sim ) { const words = dm . GetWords ( x1 , y1 , x2 , y2 , colorFormat , sim ) ; if ( words . length > 0 ) { const info = words . split ( '|' ) ; return { x : Number ( info [ 0 ] ) , y : Number ( info [ 1 ] ) , words : info [ 2 ] } ; } } , getScreenSize ( ) { return { width : dm . GetScreenWidth ( ) , height : dm . GetScreenHeight ( ) } ; } } ;