Sign in to follow this  
Followers 0
adam162

_WinAPI_CursorShowConsole() - undefined function

6 posts in this topic

Is there another function that does the same or am I missing something? (winapi.au3)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

:)

Edit: I see that there is some forum scrambling of the code, but it occurs just after the part you wanted. Here is the extracted code by rover for the _WinAPI_CursorShowConsole() function:

; #FUNCTION# =======================================================
; Name...........: _WinAPI_CursorShowConsole
; Description ...: Show or Hide console cursor, set cursor size
; Syntax.........: _WinAPI_CursorShowConsole($hConsole, $fShow = True, $iSize = Default)
; Parameters ....: $hConsole - Handle to standard output device
;                  $fShow    - Boolean: True - show cursor, False - hide cursor
;                  $iSize    - (Optional) set cursor size 1-100
;
; Return values .: Success     - return previous cursor size
;                         Failure        - return 0, set error and extended
; Author ........: rover
; Modified.......:
; Remarks .......:
;
; Related .......:
; Link ..........; @@MsdnLink@@ SetConsoleCursorInfo / GetConsoleCursorInfo
; Example .......; Yes
; ==================================================================
Func _WinAPI_CursorShowConsole($hConsole = -1, $fShow = True, $iSize = Default)
    If $hConsole = -1 Then Return SetError(1, 1, 0)
    ;// create console cursor info struct
    Local $aRet, $iErr
    Local Const $tagCONSOLE_CURSOR_INFO = "dword dwSize;int bVisible"
    Local $tCONSOLE_CURSOR_INFO = DllStructCreate($tagCONSOLE_CURSOR_INFO)
    If @error Then Return SetError(@error, 2, 0)
   
    Local $pCONSOLE_CURSOR_INFO = DllStructGetPtr($tCONSOLE_CURSOR_INFO)
    If @error Then Return SetError(@error, 3, 0)
   
    $aRet = DllCall("Kernel32.dll", "int", "GetConsoleCursorInfo", _
            "hwnd", $hConsole, "ptr", $pCONSOLE_CURSOR_INFO)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 4, 0)
   
    Local $iPrevSize = DllStructGetData($tCONSOLE_CURSOR_INFO, "dwSize")
    If @error Then Return SetError(@error, 5, 0)
   
    DllStructSetData($tCONSOLE_CURSOR_INFO, "bVisible", $fShow)
    If @error Then Return SetError(@error, 6, 0)
   
    If Not IsKeyword($iSize) And IsNumber($iSize) Then
        DllStructSetData($tCONSOLE_CURSOR_INFO, "dwSize", $iSize)
        If @error Then Return SetError(@error, 7, 0)
    EndIf
   
    DllCall("Kernel32.dll", "int", "SetConsoleCursorInfo", _
            "hwnd", $hConsole, "ptr", $pCONSOLE_CURSOR_INFO)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 8, 0)
   
    Return SetError(@error, 0, $iPrevSize)
EndFunc   ;==>_WinAPI_CursorShowConsole?

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

adam162,

_WinAPI_CursorShowConsole() was written by rover here, but the code hss been trashed by a forum upgrade. :)

Given that rover had to write it in the first place, it is most unlikely to have an equivalent elsewhere. Your best hope is that rover sees your post and still has the code somewhere. :)

M23

Edit: The penguin needed to scroll down the page a bit! :P

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I have found it in my collection.

;http://www.autoitscript.com/forum/index.php?showtopic=92049
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

If Not @Compiled Then Exit MsgBox(262144 + 16, "_WinAPI_ClearConsole() Test", "Script must be compiled as a console app to run")

Opt('MustDeclareVars', 1)

Local $hStdOut = _WinAPI_ClearConsole() ; clear screen -  clears prompt if run from command line

;save previous cursor, hide cursor
Local $iPrevSize = _WinAPI_CursorShowConsole($hStdOut, False)

;append text to first row
Local $aArray = StringSplit("4D7900204B61726D61002072756E00206F76657200206D790020446F676D61", "00", 1)
Local $iCnt
For $i = 1 To $aArray[0]
    ConsoleWrite(BinaryToString("0x" & $aArray[$i]))
    Sleep(750)
    $iCnt += (StringLen($aArray[$i]) / 2)
    _WinAPI_ClearConsole(-1, $iCnt, 0); append text to end of row 0 ($iCnt is character column position on row)
Next

ConsoleWrite(@CRLF)

;add block of rows to demonstrate line editing
For $i = 1 To 6
    ConsoleWrite("Jackdaws love my big sphinx of quartz" & @CRLF)
Next

Sleep(1000)

;set block cursor
_WinAPI_CursorShowConsole($hStdOut, True, 100)

;clear then write single rows with new text
For $i = 1 To 3
    _WinAPI_ClearConsole(-1, Default, $i)
    Sleep(1000)
    ConsoleWrite("New row " & $i)
Next

Sleep(2000)
_WinAPI_ClearConsole(-1, -2, -1) ; clear a block of 2 rows starting on row 1
ConsoleWrite("correcting 'Typo' in row 0")
Sleep(2000)
_WinAPI_ClearConsole(-1, -10, 0) ; change letter in first row of text
ConsoleWrite("a")
Sleep(2000)
_WinAPI_ClearConsole(-1, Default, 1) ;clear row 1
ConsoleWrite("Done")
Sleep(1000)
_WinAPI_ClearConsole(-1, 0, 0) ; set cursor on character column 0 and row 0
Sleep(1000)
_WinAPI_ClearConsole(-1, 0, Default) ; set cursor on character column 0 and bottom row
Sleep(1000)
_WinAPI_ClearConsole(-1, Default, -1) ; clear all rows starting at second row
Sleep(1000)
_WinAPI_ClearConsole() ; clear screen (set cursor on character column 0 and row 0)

;Overwriting StdOut in CUI compiled app, for use with progress indicator
;http://www.autoitscript.com/forum/index.php?showtopic=66486

;hide cursor
_WinAPI_CursorShowConsole($hStdOut, False)

;Example showing label and data updating on a row
Local $progress, $buffer, $ClearProgress
ConsoleWrite("Milliseconds: " & @CRLF)
ConsoleWrite("Seconds     :  " & @CRLF)
ConsoleWrite("Progress" & @CRLF)

For $i = 0 To 500
    Sleep(10)
    _WinAPI_ClearConsole(-1, 14, 0)
    ConsoleWrite(@MSEC)
    _WinAPI_ClearConsole(-1, 14, 1)
    ConsoleWrite(@SEC)
    If @SEC <> $buffer Then
        _WinAPI_ClearConsole(-1, 8, 2)
        $buffer = @SEC
        $progress &= "."
        ConsoleWrite($progress)
        $ClearProgress += 1
        If $ClearProgress = 25 Then
            $ClearProgress = 0
            $progress = ""
        EndIf
    EndIf
Next

;restore and show cursor, clear console
_WinAPI_CursorShowConsole($hStdOut, True, $iPrevSize)
_WinAPI_ClearConsole()
Sleep(1000)
Exit


; #FUNCTION# =======================================================
; Name...........: _WinAPI_ClearConsole
; Description ...: Clears console screen buffer /w six options to clear rows & characters & set cursor position
; Syntax.........: _WinAPI_ClearConsole($hConsole = -1, $iX = Default, $iY = Default)
; Parameters ....: $hConsole - (Optional) Handle to standard output device
;                  $iX - (Optional) zero based character column position of cursor
;                         Default or 0 to screen buffer max width (negative values for some modes)
;                  $iY - (Optional) zero based row position of cursor
;                         Default or 0 to screen buffer max height (negative values for some modes)
;
;No params         ; clear screen (-1, Default, Default)
;(-1, Default, 0)  ; clear single row: $iY = row number (0 is row 1)
;(-1, -2, -3)      ; clear block of rows: -$iX = number of rows, -$iY = starting row (minimum $iX = -1, $iY = -1)
;(-1, Default, -1) ; clear all rows from start row: -$iY = starting row (minimum $iY = -1)
;(-1, 20, 2)       ; clear characters to end of row: $iX = start character column, $iY = row number
;(-1, -20, 2)      ; set cursor at coordinates: -$iX = start character column, $iY = row number (minimum $iX = -1)
;(-1, 0, Default)  ; set cursor on bottom row of console window: $iX = start character column

; Return values .: Success - return handle to standard output device (does not have to be closed)
;                  Failure - return 0, set error and extended
; Author ........: rover
; Modified.......:
; Remarks .......:
; Related .......:
;
; Link ..........; @@MsdnLink@@ FillConsoleOutputCharacter
; Example .......; Yes
; ==================================================================
Func _WinAPI_ClearConsole($hConsole = -1, $iX = Default, $iY = Default)
    Local $dwCoord, $fFlag = False
    Local $bChar = 0x20, $iErr ; fill character: 0x20 (Space)
    Local Const $STD_OUTPUT_HANDLE = -11
    Local Const $INVALID_HANDLE_VALUE = -1
    Local Const $tagCONSOLE_SCREEN_BUFFER_INFO = "short dwSizeX; short dwSizeY;short dwCursorPositionX;" & _
            "short dwCursorPositionY; short wAttributes;short Left; short Top; short Right; short Bottom;" & _
            "short dwMaximumWindowSizeX; short dwMaximumWindowSizeY"

    ;// get handle to standard output device (handle does not have to be closed on return)
    Local $hDLLK32 = DllOpen("Kernel32.dll"), $aRet
    If $hConsole = -1 Then
        $aRet = DllCall($hDLLK32, "hwnd", "GetStdHandle", "dword", $STD_OUTPUT_HANDLE)
        $iErr = @error
        If @error Or UBound($aRet) <> 2 Or $aRet[0] = $INVALID_HANDLE_VALUE Then
            Return SetError($iErr, 1, $INVALID_HANDLE_VALUE)
        EndIf
        $hConsole = $aRet[0]
    EndIf
   
    ;// create console screen buffer struct, get buffer
    Local $tCONSOLE_SCREEN_BUFFER_INFO = DllStructCreate($tagCONSOLE_SCREEN_BUFFER_INFO)
    If @error Then Return SetError(@error, 2, 0)
    Local $pConsoleScreenBufferInfo = DllStructGetPtr($tCONSOLE_SCREEN_BUFFER_INFO)
    If @error Then Return SetError(@error, 3, 0)
   
    $aRet = DllCall($hDLLK32, "int", "GetConsoleScreenBufferInfo", "hwnd", _
            $hConsole, "ptr", $pConsoleScreenBufferInfo)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 4, 0)
   
    ;// Get the screen buffer max width (character columns) and height (rows)
    Local $dwSizeX = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "dwSizeX")
    Local $dwSizeY = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "dwSizeY")
    Local $dwConSize
   
    ;// input coordinates range check
    If IsNumber($iX) And IsNumber($iY) Then
        If $iX > $dwSizeX Then $iX = $dwSizeX
        If $iY > $dwSizeY Then $dwSizeY = $dwSizeY
    EndIf
   
    Select
        ;// clear screen (Default) - max screen buffer width multiplied by height
        Case IsNumber($iX) = 0 And IsNumber($iY) = 0
            ; handles Default keyword and strings in params
            $dwConSize = ($dwSizeX * $dwSizeY)
            $iX = 0
            $iY = 0
            ;// overwrite or clear any single row - cursor now set to start of that row
        Case IsKeyword($iX) = 1 And IsKeyword($iY) = 0 And $iY >= 0
            $dwConSize = $dwSizeX
            $iX = 0
            ;// overwrite or clear a number of rows from starting row
            ;(-$iX parameter is number of rows to overwrite, second row minimum)
        Case $iX < 0 And $iY < 0
            $iY = Abs($iY)
            $dwConSize = ($dwSizeX * Abs($iX))
            $iX = 0
            ;// overwrite or clear all rows from starting row to last row, second row minimum)
        Case IsKeyword($iX) = 1 And $iY < 0
            $iY = Abs($iY)
            $dwConSize = ($dwSizeX * $dwSizeY) - ($dwSizeX * $iY)
            $iX = 0
            ;// overwrite or clear text from character position on row to end of row
        Case $iX >= 0 And IsKeyword($iY) = 0 And $iY >= 0
            $dwConSize = ($dwSizeX - $iX)
            If $iX = 0 And $iY = 0 Then
                $fFlag = True
                ContinueCase
            EndIf
            ;// place cursor at last row of console window
        Case $iX >= 0 And IsKeyword($iY) = 1
            If Not $fFlag Then $iY = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "Bottom")
            ContinueCase
            ;// places cursor at coordinates for overwriting
        Case $iX < 0 And $iY >= 0
            $dwCoord = BitOR($iY * 0x10000, BitAND(Abs($iX), 0xFFFF))
            $aRet = DllCall($hDLLK32, "int", "SetConsoleCursorPosition", "hwnd", _
                    $hConsole, "dword", $dwCoord)
            $iErr = @error
            If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 5, 0)
            DllClose($hDLLK32)
            Return SetError(0, 0, $hConsole)
        Case Else
            Return SetError(@error, 6, 0)
    EndSelect
   
    ;// Cursor position: make DWord of X,Y coordinates)
    $dwCoord = BitOR($iY * 0x10000, BitAND($iX, 0xFFFF))
   
    ;// Fill selected rows with blanks
    $aRet = DllCall($hDLLK32, "int", "FillConsoleOutputCharacterW", "hwnd", $hConsole, _
            "byte", $bChar, "dword", $dwConSize, "dword", $dwCoord, "int*", 0)
    $iErr = @error
    If @error Or UBound($aRet) <> 6 Or $aRet[5] <> $dwConSize Then Return SetError($iErr, 7, 0)

    ;// Get the current text attributes
    $aRet = DllCall($hDLLK32, "int", "GetConsoleScreenBufferInfo", "hwnd", _
            $hConsole, "dword", $pConsoleScreenBufferInfo)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 8, 0)
    Local $wAttribute = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "wAttributes")

    ;// Set the buffer's attributes
    $aRet = DllCall($hDLLK32, "int", "FillConsoleOutputAttribute", "hwnd", $hConsole, _
            "short", $wAttribute, "dword", $dwConSize, "dword", $dwCoord, "int*", 0)
    $iErr = @error
    If @error Or UBound($aRet) <> 6 Or $aRet[5] <> $dwConSize Then Return SetError($iErr, 9, 0)

    ;// Put the cursor at 0,0 or supplied coordinates
    $aRet = DllCall($hDLLK32, "int", "SetConsoleCursorPosition", "hwnd", _
            $hConsole, "dword", $dwCoord)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 10, 0)
    DllClose($hDLLK32)
    Return SetError(@error, 0, $hConsole)
EndFunc   ;==>_WinAPI_ClearConsole

; #FUNCTION# =======================================================
; Name...........: _WinAPI_CursorShowConsole
; Description ...: Show or Hide console cursor, set cursor size
; Syntax.........: _WinAPI_CursorShowConsole($hConsole, $fShow = True, $iSize = Default)
; Parameters ....: $hConsole - Handle to standard output device
;                  $fShow    - Boolean: True - show cursor, False - hide cursor
;                  $iSize    - (Optional) set cursor size 1-100
;
; Return values .: Success     - return previous cursor size
;                         Failure        - return 0, set error and extended
; Author ........: rover
; Modified.......:
; Remarks .......:
;
; Related .......:
; Link ..........; @@MsdnLink@@ SetConsoleCursorInfo / GetConsoleCursorInfo
; Example .......; Yes
; ==================================================================
Func _WinAPI_CursorShowConsole($hConsole = -1, $fShow = True, $iSize = Default)
    If $hConsole = -1 Then Return SetError(1, 1, 0)
    ;// create console cursor info struct
    Local $aRet, $iErr
    Local Const $tagCONSOLE_CURSOR_INFO = "dword dwSize;int bVisible"
    Local $tCONSOLE_CURSOR_INFO = DllStructCreate($tagCONSOLE_CURSOR_INFO)
    If @error Then Return SetError(@error, 2, 0)
   
    Local $pCONSOLE_CURSOR_INFO = DllStructGetPtr($tCONSOLE_CURSOR_INFO)
    If @error Then Return SetError(@error, 3, 0)
   
    $aRet = DllCall("Kernel32.dll", "int", "GetConsoleCursorInfo", _
            "hwnd", $hConsole, "ptr", $pCONSOLE_CURSOR_INFO)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 4, 0)
   
    Local $iPrevSize = DllStructGetData($tCONSOLE_CURSOR_INFO, "dwSize")
    If @error Then Return SetError(@error, 5, 0)
   
    DllStructSetData($tCONSOLE_CURSOR_INFO, "bVisible", $fShow)
    If @error Then Return SetError(@error, 6, 0)
   
    If Not IsKeyword($iSize) And IsNumber($iSize) Then
        DllStructSetData($tCONSOLE_CURSOR_INFO, "dwSize", $iSize)
        If @error Then Return SetError(@error, 7, 0)
    EndIf
   
    DllCall("Kernel32.dll", "int", "SetConsoleCursorInfo", _
            "hwnd", $hConsole, "ptr", $pCONSOLE_CURSOR_INFO)
    $iErr = @error
    If @error Or UBound($aRet) <> 3 Or Not $aRet[0] Then Return SetError($iErr, 8, 0)
   
    Return SetError(@error, 0, $iPrevSize)
EndFunc   ;==>_WinAPI_CursorShowConsole

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

I have implemented most of the console winapi here. I implemented clearing the console in Au3Int as:

Func _Au3Int_Clr()
    Local $tScreenBufferInfo = _Console_GetConsoleScreenBufferInfo()

    _Console_FillOutputCharacter(" ", DllStructGetData($tScreenBufferInfo, "SizeX") * (DllStructGetData($tScreenBufferInfo, "SizeY")), 0, 0)
    _Console_SetCursorPos(0, 0)

    Return True
EndFunc   ;==>_Au3Int_Clr

This is exactly the same method rover uses :)

And showing + hiding the cursor is in the console library as _Console_SetCursorVisible. You can set the size of the cursor and the visibility by _Console_SetCursorInfo or just the size with _Console_SetCursorSize. If you do use them then examples and testing is needed for the project before I release it properly, so whatever you do use, please report bugs and post examples :) You may also want to read the FAQ when things go wrong.

Share this post


Link to post
Share on other sites

Great stuff Mat! :)

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0