Jump to content

List view error


Recommended Posts

This is my code :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#include <WinAPI.au3>
#RequireAdmin
#NoTrayIcon

Global $hGui, $hListView, $hEdit, $hEdit1
Global $hDC, $hBrush, $Item = -1, $SubItem = 0, $iSubItemText, $tItem, $tSubitem, $tiSubitemtext, $currentcol
Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

_GUI()

Func _GUI()
    Global $hGui = GUICreate("THỐNG KÊ CẤP PHÁT", @DesktopWidth - 380, @DesktopHeight - 250, -1, -1)
    Global $FileMenu = GUICtrlCreateMenu("&File")
    Global $mSave = GUICtrlCreateMenuItem("Lưu và xuất thành Excel", $FileMenu)
    Global $mExit = GUICtrlCreateMenuItem("Exit", $FileMenu)
    Global $Option = GUICtrlCreateMenu("&Options")
    Global $mConfig = GUICtrlCreateMenuItem("Tùy chỉnh", $Option)
    Global $Start = GUICtrlCreateButton("BẮT ĐẦU", 88, 400, 121, 49)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Global $Export = GUICtrlCreateButton("XUẤT EXCEL", 408, 400, 121, 49)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Global $Exit = GUICtrlCreateButton("KẾT THÚC", 720, 400, 121, 49)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Create_Listview()
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    OnAutoItExitRegister("Cleanup")
    Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Global $hmod = _WinAPI_GetModuleHandle(0)
    Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

    While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case $GUI_EVENT_CLOSE
                Exit
;~           Case $mConfig
;~               _OptionGUI()
        EndSwitch
    WEnd
EndFunc   ;==>_GUI

Func Create_Listview()
    $hListView = _GUICtrlListView_Create($hGui, "", 5, 5, @DesktopWidth - 400, @DesktopHeight - 400, BitOR($LVS_EDITLABELS, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

    _GUICtrlListView_AddColumn($hListView, "Họ và tên", 175, -1)
    _GUICtrlListView_AddColumn($hListView, "Ngày làm việc", 175, -1)
    _GUICtrlListView_AddColumn($hListView, "", 175, -1)
    _GUICtrlListView_AddColumn($hListView, "", 175, -1)

    For $i = 1 To 20
        _GUICtrlListView_AddItem($hListView, "")
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 1)
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 2)
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 3)
    Next

EndFunc   ;==>Create_Listview

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode, $col_width, $cInfo, $iIDFrom, $tInfo, $iLen, $aRect

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")

    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    $currentcol = DllStructGetData($tInfo, "SubItem")
                    $cInfo = _GUICtrlListView_GetColumn($hListView, $currentcol)
                    GUICtrlSetData($hEdit1, $cInfo[5])
                Case $NM_DBLCLK

                    Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        $iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)
                        $iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
                        If $iLen = 0 Then
                            $iLen = 20
                        EndIf
                        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create($hGui, $iSubItemText, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $Style)
                        _GUICtrlEdit_SetSel($hEdit, 0, -1)
                        _WinAPI_SetFocus($hEdit)
                        $hDC = _WinAPI_GetWindowDC($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush(0)
                        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $hBrush)
                    ElseIf ($aHit[0] <> -1) And ($aHit[1] = 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        $iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item)
                        $iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
                        $aRect = _GUICtrlListView_GetItemRect($hListView, $Item)
                        $hEdit = _GUICtrlEdit_Create($hGui, $iSubItemText, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $Style)
                        _GUICtrlEdit_SetSel($hEdit, 0, -1)
                        _WinAPI_SetFocus($hEdit)
                        $hDC = _WinAPI_GetWindowDC($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush(0)
                        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $hBrush)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
    Local $stRect = DllStructCreate("int;int;int;int")

    DllStructSetData($stRect, 1, $nLeft)
    DllStructSetData($stRect, 2, $nTop)
    DllStructSetData($stRect, 3, $nRight)
    DllStructSetData($stRect, 4, $nBottom)
    DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc   ;==>FrameRect

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iCode = BitShift($wParam, 16)

    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    SetText()
                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func EvaluateKey($keycode)
    Local $iLen, $aRect
    Sleep(10)

    If $keycode = 13 Then
        SetText()
        $Item = -1
        $SubItem = 0
    ElseIf $keycode = 38 Then ;up arrow
        If $Item <> -1 Then
            $tItem = $Item
            $tSubitem = $SubItem
            $tiSubitemtext = $iSubItemText
        EndIf
        SetText()
        If $tItem > 0 Then
            $tItem -= 1
        Else
            $tItem = 19
        EndIf
        $tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
        $iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
        $hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
        _GUICtrlEdit_SetSel($hEdit, 0, -1)
        _WinAPI_SetFocus($hEdit)
        $hDC = _WinAPI_GetWindowDC($hEdit)
        $hBrush = _WinAPI_CreateSolidBrush(0)
        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
    ElseIf $keycode = 40 Then ;down arrow
        If $Item <> -1 Then
            $tItem = $Item
            $tSubitem = $SubItem
            $tiSubitemtext = $iSubItemText
        EndIf
        SetText()
        If $tItem < 19 Then
            $tItem += 1
        Else
            $tItem = 0
        EndIf
        $tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
        $iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
        $hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
        _GUICtrlEdit_SetSel($hEdit, 0, -1)
        _WinAPI_SetFocus($hEdit)
        $hDC = _WinAPI_GetWindowDC($hEdit)
        $hBrush = _WinAPI_CreateSolidBrush(0)
        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
    ElseIf $keycode = 37 Then ;left arrow
        If $Item <> -1 Then
            $tItem = $Item
            $tSubitem = $SubItem
            $tiSubitemtext = $iSubItemText
        EndIf
        SetText()
        If $tSubitem > 0 Then
            $tSubitem -= 1
        Else
            $tSubitem = _GUICtrlListView_GetColumnCount($hListView) - 1
        EndIf
        $tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
        $iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
        $hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
        _GUICtrlEdit_SetSel($hEdit, 0, -1)
        _WinAPI_SetFocus($hEdit)
        $hDC = _WinAPI_GetWindowDC($hEdit)
        $hBrush = _WinAPI_CreateSolidBrush(0)
        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
    ElseIf $keycode = 39 Then ;right arrow
        If $Item <> -1 Then
            $tItem = $Item
            $tSubitem = $SubItem
            $tiSubitemtext = $iSubItemText
        EndIf
        SetText()
        If $tSubitem < _GUICtrlListView_GetColumnCount($hListView) - 1 Then
            $tSubitem += 1
        Else
            $tSubitem = 0
        EndIf
        $tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
        $iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
        $hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
        _GUICtrlEdit_SetSel($hEdit, 0, -1)
        _WinAPI_SetFocus($hEdit)
        $hDC = _WinAPI_GetWindowDC($hEdit)
        $hBrush = _WinAPI_CreateSolidBrush(0)
        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
    ElseIf $keycode = 9 Then ;tab
        If $Item <> -1 Then
            $tItem = $Item
            $tSubitem = $SubItem
            $tiSubitemtext = $iSubItemText
        EndIf
        SetText()
        If $tSubitem < _GUICtrlListView_GetColumnCount($hListView) - 1 Then
            $tSubitem += 1
        Else
            $tSubitem = 0
            If $tItem < 19 Then
                $tItem += 1
            Else
                $tItem = 0
            EndIf
        EndIf
        $tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
        $iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
        $hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
        _GUICtrlEdit_SetSel($hEdit, 0, -1)
        _WinAPI_SetFocus($hEdit)
        $hDC = _WinAPI_GetWindowDC($hEdit)
        $hBrush = _WinAPI_CreateSolidBrush(0)
        FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
    EndIf
EndFunc   ;==>EvaluateKey

Func SetText()
    Local $iText = _GUICtrlEdit_GetText($hEdit)
    If $Item <> -1 Then
        _GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
    Else
        _GUICtrlListView_SetItemText($hListView, $tItem, $iText, $tSubitem)
    EndIf
    _WinAPI_DeleteObject($hBrush)
    _WinAPI_ReleaseDC($hEdit, $hDC)
    _WinAPI_DestroyWindow($hEdit)
EndFunc   ;==>SetText

Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS

    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    Else
        Local $flags = DllStructGetData($tKEYHOOKS, "flags")
        Switch $flags
            Case $LLKHF_ALTDOWN
;~               ConsoleWrite("$LLKHF_ALTDOWN" & @CRLF)
            Case $LLKHF_EXTENDED
;~               ConsoleWrite("$LLKHF_EXTENDED" & @CRLF)
            Case $LLKHF_INJECTED
;~               ConsoleWrite("$LLKHF_INJECTED" & @CRLF)
            Case $LLKHF_UP
;~               ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @CRLF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>Cleanup

My code is error now, I can't use Tab, up arrow, down arrow, left arrow between 2 Subitem in Listview control. I try to fix this error but I can't find anything, and I think this error is come from EvaluateKey(). Please help me, I will be very grateful

Edited by MrVietA2
Link to comment
Share on other sites

Can you strip down your script to a simple reproducer script with a few lines?

I'm not going to run a script on my computer that needs admin rights.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include 
#include 
#include 
#include 
#include 
#include 
#NoTrayIcon

Global $hGui, $hListView, $hEdit, $hEdit1
Global $hDC, $hBrush, $Item = -1, $SubItem = 0, $iSubItemText, $tItem, $tSubitem, $tiSubitemtext, $currentcol
Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

_GUI()

Func _GUI()
Global $hGui = GUICreate("script", @DesktopWidth - 380, @DesktopHeight - 250, -1, -1)
GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
Global $Button1 = GUICtrlCreateButton("Button 1", 408, 400, 121, 49)
GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
Global $Button2 = GUICtrlCreateButton("Button2", 720, 400, 121, 49)
GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
Create_Listview()
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
OnAutoItExitRegister("Cleanup")
Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)

While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE
Exit
;~ Case $mConfig
;~ _OptionGUI()
EndSwitch
WEnd
EndFunc ;==>_GUI

Func Create_Listview()
$hListView = _GUICtrlListView_Create($hGui, "", 5, 5, @DesktopWidth - 400, @DesktopHeight - 400, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListView_AddColumn($hListView, "Column 1", 175, -1)
_GUICtrlListView_AddColumn($hListView, "Column 2", 175, -1)
_GUICtrlListView_AddColumn($hListView, "", 175, -1)
_GUICtrlListView_AddColumn($hListView, "", 175, -1)

For $i = 1 To 20
_GUICtrlListView_AddItem($hListView, "")
_GUICtrlListView_AddSubItem($hListView, $i - 1, "", 1)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "", 2)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "", 3)
Next

EndFunc ;==>Create_Listview

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMHDR, $hWndFrom, $iCode, $col_width, $cInfo, $iIDFrom, $tInfo, $iLen, $aRect

$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")

Switch $hWndFrom
Case $hListView
Switch $iCode
Case $LVN_COLUMNCLICK ; A column was clicked
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$currentcol = DllStructGetData($tInfo, "SubItem")
$cInfo = _GUICtrlListView_GetColumn($hListView, $currentcol)
GUICtrlSetData($hEdit1, $cInfo[5])
Case $NM_DBLCLK

Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
$Item = $aHit[0]
$SubItem = $aHit[1]
$iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
If $iLen = 0 Then
$iLen = 20
EndIf
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
$hEdit = _GUICtrlEdit_Create($hGui, $iSubItemText, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $hBrush)
ElseIf ($aHit[0] <> -1) And ($aHit[1] = 0) Then
$Item = $aHit[0]
$SubItem = $aHit[1]
$iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
$aRect = _GUICtrlListView_GetItemRect($hListView, $Item)
$hEdit = _GUICtrlEdit_Create($hGui, $iSubItemText, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $SubItem), 17, $hBrush)
EndIf
EndSwitch
EndSwitch

Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
Local $stRect = DllStructCreate("int;int;int;int")

DllStructSetData($stRect, 1, $nLeft)
DllStructSetData($stRect, 2, $nTop)
DllStructSetData($stRect, 3, $nRight)
DllStructSetData($stRect, 4, $nBottom)
DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc ;==>FrameRect

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
Local $iCode = BitShift($wParam, 16)

Switch $lParam
Case $hEdit
Switch $iCode
Case $EN_KILLFOCUS
SetText()
$Item = -1
$SubItem = 0
EndSwitch
EndSwitch

Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND

Func EvaluateKey($keycode)
Local $iLen, $aRect
Sleep(10)

If $keycode = 13 Then
SetText()
$Item = -1
$SubItem = 0
ElseIf $keycode = 38 Then ;up arrow
If $Item <> -1 Then
$tItem = $Item
$tSubitem = $SubItem
$tiSubitemtext = $iSubItemText
EndIf
SetText()
If $tItem > 0 Then
$tItem -= 1
Else
$tItem = 19
EndIf
$tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
$hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
ElseIf $keycode = 40 Then ;down arrow
If $Item <> -1 Then
$tItem = $Item
$tSubitem = $SubItem
$tiSubitemtext = $iSubItemText
EndIf
SetText()
If $tItem < 19 Then
$tItem += 1
Else
$tItem = 0
EndIf
$tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
$hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
ElseIf $keycode = 37 Then ;left arrow
If $Item <> -1 Then
$tItem = $Item
$tSubitem = $SubItem
$tiSubitemtext = $iSubItemText
EndIf
SetText()
If $tSubitem > 0 Then
$tSubitem -= 1
Else
$tSubitem = _GUICtrlListView_GetColumnCount($hListView) - 1
EndIf
$tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
$hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
ElseIf $keycode = 39 Then ;right arrow
If $Item <> -1 Then
$tItem = $Item
$tSubitem = $SubItem
$tiSubitemtext = $iSubItemText
EndIf
SetText()
If $tSubitem < _GUICtrlListView_GetColumnCount($hListView) - 1 Then
$tSubitem += 1
Else
$tSubitem = 0
EndIf
$tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
$hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
ElseIf $keycode = 9 Then ;tab
If $Item <> -1 Then
$tItem = $Item
$tSubitem = $SubItem
$tiSubitemtext = $iSubItemText
EndIf
SetText()
If $tSubitem < _GUICtrlListView_GetColumnCount($hListView) - 1 Then
$tSubitem += 1
Else
$tSubitem = 0
If $tItem < 19 Then
$tItem += 1
Else
$tItem = 0
EndIf
EndIf
$tiSubitemtext = _GUICtrlListView_GetItemText($hListView, $tItem, $tSubitem)
$iLen = _GUICtrlListView_GetStringWidth($hListView, $tiSubitemtext)
$aRect = _GUICtrlListView_GetSubItemRect($hListView, $tItem, $tSubitem)
$hEdit = _GUICtrlEdit_Create($hGui, $tiSubitemtext, $aRect[0] + 3, $aRect[1] + 4, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $Style)
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListView, $tSubitem), 17, $hBrush)
EndIf
EndFunc ;==>EvaluateKey

Func SetText()
Local $iText = _GUICtrlEdit_GetText($hEdit)
If $Item <> -1 Then
_GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
Else
_GUICtrlListView_SetItemText($hListView, $tItem, $iText, $tSubitem)
EndIf
_WinAPI_DeleteObject($hBrush)
_WinAPI_ReleaseDC($hEdit, $hDC)
_WinAPI_DestroyWindow($hEdit)
EndFunc ;==>SetText

Func _KeyProc($nCode, $wParam, $lParam)
Local $tKEYHOOKS

$tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
If $nCode < 0 Then
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndIf
If $wParam = $WM_KEYDOWN Then
EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
Else
Local $flags = DllStructGetData($tKEYHOOKS, "flags")
Switch $flags
Case $LLKHF_ALTDOWN
;~ ConsoleWrite("$LLKHF_ALTDOWN" & @CRLF)
Case $LLKHF_EXTENDED
;~ ConsoleWrite("$LLKHF_EXTENDED" & @CRLF)
Case $LLKHF_INJECTED
;~ ConsoleWrite("$LLKHF_INJECTED" & @CRLF)
Case $LLKHF_UP
;~ ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @CRLF)
EndSwitch
EndIf
Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc ;==>_KeyProc

Func Cleanup()
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_KeyProc)
EndFunc ;==>Cleanup

I think I know where this error comes from, if I created 2 button on my GUI, the Tab key couldn't use, but If I didn't move 2 button out my GUI, the Tab key could be used

Link to comment
Share on other sites

  • Moderators

MrVietA2,

Firstly, I suggest you create your buttons outside of the area covered by the ListView. :)

Secondly, if you want an editable ListView I suggest you look at the Beta version of my GUIListViewEx UDF that you can find here. By using it I have reduced your code to the following, which looks a lot simpler to me: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include "GUIListViewEx_Mod.au3"

Global $hListView

_GUI()

Func _GUI()
    Global $hGui = GUICreate("script", @DesktopWidth - 380, @DesktopHeight - 250, -1, -1)
    GUISetBkColor(0xC4C4C4)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Global $Button1 = GUICtrlCreateButton("Button 1", 408, @DesktopHeight - 350, 121, 49)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Global $Button2 = GUICtrlCreateButton("Button2", 720, @DesktopHeight - 350, 121, 49)
    GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
    Create_Listview()
    GUISetState(@SW_SHOW)

    Local $aLV_Array = _GUIListViewEx_ReadToArray($hListView)
    _GUIListViewEx_Init($hListView, $aLV_Array, 0, 0, 2)
    _GUIListViewEx_DragRegister(True, False, False)

    While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch

        _GUIListViewEx_Edit()

    WEnd
EndFunc   ;==>_GUI

Func Create_Listview()
    $hListView = _GUICtrlListView_Create($hGui, "", 5, 5, @DesktopWidth - 400, @DesktopHeight - 400, $LVS_REPORT)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

    _GUICtrlListView_AddColumn($hListView, "Column 1", 175)
    _GUICtrlListView_AddColumn($hListView, "Column 2", 175)
    _GUICtrlListView_AddColumn($hListView, "", 175)
    _GUICtrlListView_AddColumn($hListView, "", 175)

    For $i = 1 To 20
        _GUICtrlListView_AddItem($hListView, "")
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 1)
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 2)
        _GUICtrlListView_AddSubItem($hListView, $i - 1, "", 3)
    Next

EndFunc   ;==>Create_Listview

Just doubleclick in the ListView to enter "edit" mode. ;)

M23

Edited by Melba23
Updated link to the newer Beta

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...