肖恩5
如何捕获无法选择的文本(例如命令提示符窗口中的文本,但我没有命令提示符中的 Edit=>Mark 选项)...
我有几个例子:
检索多行:
ptr:=A_PtrSize ? "Ptr":"UInt", suffix:=A_IsUnicode ? "W":"A", numReaded:=data:=""
INVALID_HANDLE_VALUE:=-1, STD_INPUT_HANDLE:=-10, STD_OUTPUT_HANDLE:=-11
VarSetCapacity(buffer, (size:=1030)*(A_IsUnicode ? 2:1))
Run, % "cmd.exe",,, procID
WinWaitActive, % "ahk_pid"procID
;~ Input, dummyVar, % "I", % "{vk20}" ; wait a space button to press
;~ WinGet, procID, PID, A
SendEvent, % "{Raw}TEST WRITE TO CONSOLE"
If !DllCall("AttachConsole", "UInt", procID, A_PtrSize ? "UInt":"")
{
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail attach to console", % 2.5
ExitApp
}
If (hConsole:=DllCall("GetStdHandle", "Int", STD_OUTPUT_HANDLE
, A_PtrSize ? "Ptr":""))=INVALID_HANDLE_VALUE
{
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail retrive a handle of console", % 2.5
ExitApp
}
If !DllCall("ReadConsoleOutputCharacter"suffix, ptr, hConsole
, ptr, &buffer
, "UInt", size
, "UInt", 0 ; begin read from first row
, "UInt*", numReaded
, A_PtrSize ? "UInt":"")
{
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail get data from console", % 2.5
ExitApp
}
; line width is 320 pixels (property/layout/screen buffer size),
; here I cut the unnecessary white spaces in each row
Loop, % numReaded
Mod(A_Index, 320) ? data.=Chr(NumGet(buffer, (A_Index-1)*(A_IsUnicode ? 2:1)
, A_IsUnicode ? "UShort":"UChar"))
. "" : data:=RTrim(data)"`r"
MsgBox, 262208, % A_LineNumber, % RTrim(data) ;, % 2.5
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID
检索指定的行:
ptr:=A_PtrSize ? "Ptr":"UInt", suffix:=A_IsUnicode ? "W":"A", numReaded:=""
INVALID_HANDLE_VALUE:=-1, STD_INPUT_HANDLE:=-10, STD_OUTPUT_HANDLE:=-11
VarSetCapacity(buffer, (size:=319)*(A_IsUnicode ? 2:1))
Run, % "cmd.exe",,, procID
WinWaitActive, % "ahk_pid"procID
;~ Input, dummyVar, % "I", % "{vk20}" ; wait a space button to press
;~ WinGet, procID, PID, A
SendEvent, % "{Raw}TEST WRITE TO CONSOLE"
If !DllCall("AttachConsole", "UInt", procID, A_PtrSize ? "UInt":"")
{
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail attach to console", % 2.5
ExitApp
}
If (hConsole:=DllCall("GetStdHandle", "Int", STD_OUTPUT_HANDLE
, A_PtrSize ? "Ptr":""))=INVALID_HANDLE_VALUE
{
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail retrive a handle of console", % 2.5
ExitApp
}
If !DllCall("ReadConsoleOutputCharacter"suffix, ptr, hConsole
, ptr, &buffer
, "UInt", size
, "UInt", 3<<16 ; skip some rows
, "UInt*", numReaded
, A_PtrSize ? "UInt":"")
{
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID
MsgBox, 262192, % A_LineNumber, % "Fail get data from console", % 2.5
ExitApp
}
; cut the unnecessary white spaces and show
MsgBox, 262208, % A_LineNumber, % RTrim(StrGet(&buffer, numReaded
, A_IsUnicode ? "UTF-16":"CP0"))
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID