Sign in to follow this  
Followers 0
scubasteve1281

GUICtrlCreateList not doing what I want

5 posts in this topic

I have been using GUICtrlCreateList to create box that I pass info to from autoit and windows I'm using it like a msg box for errors and logging. I'm having 2 problems with it.

1. Word wrapping when ever I send a line to the list box it truncates the line and doesn't wrap around. I have tried getting around this by using $WS_HSCROLL, $LBS_DISABLENOSCROLL styles but it doesn't let me scroll over to the end of the line.

2. when ever I post the same msg it doesn't added it to the list box if it is all ready there.

If I tell my script to do something resulting msg is "something something" it will put this in the list box like should. then if I run some different functions after that in the script and come back to the first func it spits out "something something" it wont write this to the list box a separate line.

Is there something better that I should use instead of GUICtrlCreateList I'm looking for a line by line log that is self contained in side my GUI so that can ensure that each command is being completed successfully or if doesn't I know what the error is.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

About showing of long lines in ListBox:

It's possible with GetTextExtentPoint32 API function

Here is my function to set horizontal scrollbar range in listbox based on the width of widest item

$sirka_max is max. width in pixels

$List1 is control ID of my ListBox

$hwnd_list is handle of my ListBox

$hwnd_list = ControlGetHandle($Form1, "", $List1)

Func ListBoxSetHorizScrollBar()
; read text from all lines of listbox and set horizontal scrollbar by the widest line
$struct = DllStructCreate("char[4096]")
$pstruct = DllStructGetPtr($struct)
$pocet = GUICtrlSendMsg($List1, $LB_GETCOUNT, 0, 0)

$hDC = _WinAPI_GetDC($hwnd_list)
$hFont = _SendMessage($hwnd_list, $WM_GETFONT, 0, 0)
$hOld = _WinAPI_SelectObject($hDC, $hFont)
$struct_size = DllStructCreate("int;int")
$sirka_max = 0

For $i = 0 to $pocet - 1
GUICtrlSendMsg($List1, $LB_GETTEXT, $i, $pstruct)
$radek = DllStructGetData($struct, 1)
; note: due to speed optimalization  not used _WinAPI_GetTextExtentPoint32
DllCall("gdi32.dll", "int", "GetTextExtentPoint32", "int", $hDC, "str", $radek, "long", StringLen($radek), "ptr", DllStructGetPtr($struct_size))
$sirka = DllStructGetData($struct_size,1)
If $sirka > $sirka_max Then $sirka_max = $sirka
Next

$hOld = _WinAPI_SelectObject($hDC, $hOld)
_WinAPI_ReleaseDC($hwnd_list, $hDC)
$struct_size = 0
$struct = 0

GUICtrlSetLimit($List1, $sirka_max) ; to limit horizontal scrolling
EndFunc

As about not showing duplicates I think that may be related to styles of your ListBox

Try to avoid default $LBS_SORT style for example like this:

$List1 = GUICtrlCreateList("", 16, 248, 585, 214, BitOR($WS_GROUP,$WS_BORDER,$WS_VSCROLL,$WS_HSCROLL))

Post your piece of code for creating of LisBox and adding lines, the best small reproducing script.

Edited by Zedna

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Ok is sample of what I have I had to butcher a lot of it to make it function with having the rest of the code so it doesn't look pretty the only button that works is the netuse one. It will give you an error that extends past the frame.

I thought I needed to use $LBS_SORT prevent alphabetizing of the list.

I will put your example code in and see if that helps.

Thanks for the help

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#Include <GuiTab.au3>
#Include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <IPAddressConstants.au3>
#include <GUIConstants.au3>
#include <Constants.au3>
#include <WinAPI.au3>
#include <ListboxConstants.au3>


; builds main gui background
$GUImain = GUICreate(" Tools", 790, 737, 626, 184)
;;;;;; creates tab form
$Maintab= GUICtrlCreateTab(8, 104, 769, 569, $TCS_BUTTONS)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO+$GUI_DOCKLEFT+$GUI_DOCKRIGHT+$GUI_DOCKTOP+$GUI_DOCKBOTTOM+$GUI_DOCKHCENTER+$GUI_DOCKVCENTER+$GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
;;;;;;;;; creates  tab  and buttons fopr test script
$test = GUICtrlCreateTabItem("test script")
$Label2 = GUICtrlCreateLabel("test Script", 460, 148, 168, 33)
GUICtrlSetFont(-1, 17, 800, 0, "Arial")
$hostname = GUICtrlCreateInput("hostname", 18, 385, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$username = GUICtrlCreateInput("username", 18, 417, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$pass = GUICtrlCreateInput("password", 18, 457, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Netuse = GUICtrlCreateButton("Netuse", 18, 497, 88, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$testoutput = GUICtrlCreateList("", 319, 194, 452, 326,BitOR($LBS_DISABLENOSCROLL,$LBS_NOSEL,$WS_HSCROLL,$LBS_SORT))
GUICtrlSetData(-1, "")

GUISetState(@SW_SHOW)


;;;;;;;

$banner = GUICtrlCreatePic("banner.jpg", 4, 0, 780, 100, BitOR($GUI_SS_DEFAULT_PIC,$SS_RIGHTJUST,$WS_CLIPSIBLINGS))
$Exit = GUICtrlCreateButton("Exit", 694, 678, 75, 25)


;;;;;;;;;;;;;;;;;;;
; varibles for IP ADDy
Global $TargetIP = _GUICtrlIpAddress_Create($GUImain, 14, 354, 130, 21)
 _GUICtrlIpAddress_Set($TargetIP, "0.0.0.0")



#region
; OS detection is done for later use
Global $OSArch = @OSArch
Global $OSversion = @OSVersion
 GUICtrlSetData($testoutput, "Operting system is: " & $OSversion & " " & $OSArch & " version")


#endregion


;main loop
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
IF $nMsg = $Exit Then Exit
If $nMsg=$Netuse then Netuse()

WEnd

; required to hide show tabs
; net use func runs here
Func Netuse()
;get the data/text here when it's needed
    Local $sIP = _GUICtrlIpAddress_Get($TargetIP)
    Local $sHostn = GUICtrlRead($hostname)
    Local $sUser = GUICtrlRead($username)
Local $sPass = GUICtrlRead($pass)

$netuseAPP = 'net use \\' & $sIP & '\ipc$ '& $sPass &' /user:' & $sHostn & '\' & $sUser & ''
    $PID = Run(@ComSpec & " /k " & $netuseAPP, "", @SW_HIDE, $STDERR_MERGED)
;WinActive, Send($sPass)
    ProcessWaitClose($PID)
$read = StdoutRead($PID)
msgbox (0,"",$read)

    GUICtrlSetData($testoutput, $read)

EndFunc ;==>Netuse
;

made code smaller

Edited by scubasteve1281

Share this post


Link to post
Share on other sites

Here is my code incorporated into your code.

I just commented your netuse and put there my example of output to ListBox control

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <GuiTab.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <IPAddressConstants.au3>
#include <GUIConstants.au3>
#include <Constants.au3>
#include <WinAPI.au3>
#include <ListboxConstants.au3>

; builds main gui background
$GUImain = GUICreate(" Tools", 790, 737, 626, 184)
;;;;;; creates tab form
$Maintab = GUICtrlCreateTab(8, 104, 769, 569, $TCS_BUTTONS)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO + $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM + $GUI_DOCKHCENTER + $GUI_DOCKVCENTER + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
;;;;;;;;; creates tab and buttons fopr test script
$test = GUICtrlCreateTabItem("test script")
$Label2 = GUICtrlCreateLabel("test Script", 460, 148, 168, 33)
GUICtrlSetFont(-1, 17, 800, 0, "Arial")
$hostname = GUICtrlCreateInput("hostname", 18, 385, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$username = GUICtrlCreateInput("username", 18, 417, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$pass = GUICtrlCreateInput("password", 18, 457, 121, 24)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Netuse = GUICtrlCreateButton("Netuse", 18, 497, 88, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$testoutput = GUICtrlCreateList("", 319, 194, 452, 326, BitOR($WS_GROUP,$WS_BORDER,$WS_VSCROLL,$WS_HSCROLL,$LBS_NOSEL)) ; BitOR($LBS_DISABLENOSCROLL, $LBS_NOSEL, $WS_HSCROLL))
$hwnd_list = ControlGetHandle($GUImain, "", $testoutput)
GUICtrlSetData(-1, "")

$banner = GUICtrlCreatePic("banner.jpg", 4, 0, 780, 100, BitOR($GUI_SS_DEFAULT_PIC, $SS_RIGHTJUST, $WS_CLIPSIBLINGS))
$Exit = GUICtrlCreateButton("Exit", 694, 678, 75, 25)
GUISetState(@SW_SHOW)


; varibles for IP ADDy
Global $TargetIP = _GUICtrlIpAddress_Create($GUImain, 14, 354, 130, 21)
_GUICtrlIpAddress_Set($TargetIP, "0.0.0.0")

#region
; OS detection is done for later use
Global $OSArch = @OSArch
Global $OSversion = @OSVersion
GUICtrlSetData($testoutput, "Operting system is: " & $OSversion & " " & $OSArch & " version")
#endregion

;main loop
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
If $nMsg = $Exit Then Exit
If $nMsg = $Netuse Then Netuse()

WEnd

; required to hide show tabs
; net use func runs here
Func Netuse()
;get the data/text here when it's needed
Local $sIP = _GUICtrlIpAddress_Get($TargetIP)
Local $sHostn = GUICtrlRead($hostname)
Local $sUser = GUICtrlRead($username)
Local $sPass = GUICtrlRead($pass)

;~ $netuseAPP = 'net use \\' & $sIP & '\ipc$ ' & $sPass & ' /user:' & $sHostn & '\' & $sUser & ''
;~ $PID = Run(@ComSpec & " /k " & $netuseAPP, "", @SW_HIDE, $STDERR_MERGED)
;~ ;WinActive, Send($sPass)
;~ ProcessWaitClose($PID)
;~ $read = StdoutRead($PID)
;~ MsgBox(0, "", $read)

; example of output to LisBox
Output('')
$line = '123' & @CRLF & '456' & @CRLF & '789'
Output($line)

Output('')
For $n = 1 To 100
Output($n & ': ' & StringRepeat('x', $n))
Next
EndFunc ;==>Netuse

Func Output($text)
Local $i,$j
; if there are @CRLF in text then split it to lines because LB_ADDSTRING ignore line breaks
If StringInStr($text, @CRLF) Then
$tmp = StringSplit($text, @CRLF, 1)
For $i = 1 To $tmp[0]
$j = GUICtrlSendMsg($testoutput, $LB_ADDSTRING, 0, String($tmp[$i]))
GUICtrlSendMsg($testoutput, $LB_SETTOPINDEX, $j, 0)
Next
Else
$i = GUICtrlSendMsg($testoutput, $LB_ADDSTRING, 0, String($text)) ; returns index of added line
GUICtrlSendMsg($testoutput, $LB_SETTOPINDEX, $i, 0)
EndIf
ListBoxSetHorizScrollBar()
EndFunc

Func ListBoxSetHorizScrollBar()
; read text from all lines of listbox and set horizontal scrollbar by the widest line
$struct = DllStructCreate("wchar[4096]")
$pstruct = DllStructGetPtr($struct)
$count = GUICtrlSendMsg($testoutput, $LB_GETCOUNT, 0, 0)

$hDC = _WinAPI_GetDC($hwnd_list)
$hFont = _SendMessage($hwnd_list, $WM_GETFONT, 0, 0)
$hOld = _WinAPI_SelectObject($hDC, $hFont)
$struct_size = DllStructCreate("int;int")
$width_max = 0

For $i = 0 To $count - 1
GUICtrlSendMsg($testoutput, $LB_GETTEXT, $i, $pstruct)
$line = DllStructGetData($struct, 1)
; note: due to speed optimalization not used _WinAPI_GetTextExtentPoint32
DllCall("gdi32.dll", "int", "GetTextExtentPoint32", "int", $hDC, "str", $line, "long", StringLen($line), "ptr", DllStructGetPtr($struct_size))
$width = DllStructGetData($struct_size, 1)
If $width > $width_max Then $width_max = $width
Next

$hOld = _WinAPI_SelectObject($hDC, $hOld)
_WinAPI_ReleaseDC($hwnd_list, $hDC)
$struct_size = 0
$struct = 0

GUICtrlSetLimit($testoutput, $width_max) ; to limit horizontal scrolling
EndFunc ;==>ListBoxSetHorizScrollBar

Func StringRepeat($sChar, $nCount)
$tBuffer = DLLStructCreate("char[" & $nCount & "]")
DllCall("msvcrt.dll", "ptr:cdecl", "memset", "ptr", DLLStructGetPtr($tBuffer), "int", Asc($sChar), "int", $nCount)
Return DLLStructGetData($tBuffer, 1)
EndFunc

Share this post


Link to post
Share on other sites

Here is my code incorporated into your code.

I just commented your netuse and put there my example of output to ListBox control

Sweetness

Much thanks Zedna

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