Jump to content

Edit in place for more than one listview control


 Share

Recommended Posts

So, I found that rasim's and smashly's code worked very well for one listview control, and i started thinking about how to make it work with two listview controls.

I know there is a lot of duplication in the WM_Notify function that follows, but it does seem to work with the two listview controls.

The problem I'm having now is trying to figure out how to make the WM_Command function work with two controls. No matter what I've tried I can't seem to find the handle for the listview control that is being edited.

Any ideas about how to make the WM_Command function work for both hListView and hListView2?

Thank you,

DiscoEd

#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <GuiListView.au3>;Added by DiscoEd
#include <GUIConstantsEx.au3>;Added by DiscoEd

Opt("GuiCloseOnESC", 0)

Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

$hGUI = GUICreate("ListView Subitems edit in place", 300, 500)

$hListView = _GUICtrlListView_Create ($hGUI, "Items|SubItems|SubItems2", 2, 2, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle ($hListView, $LVS_EX_GRIDLINES)

$hListView2 = _GUICtrlListView_Create ($hGUI, "Items|SubItems|SubItems2", 2, 210, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle ($hListView2, $LVS_EX_GRIDLINES)


For $i = 1 To 10
    _GUICtrlListView_AddItem ($hListView, "Item " & $i)
    _GUICtrlListView_AddSubItem ($hListView, $i - 1, "SubItem " & $i, 1)
    _GUICtrlListView_AddSubItem ($hListView, $i - 1, "SubItem " & $i, 2)
    _GUICtrlListView_AddItem ($hListView2, "Item " & $i)
    _GUICtrlListView_AddSubItem ($hListView2, $i - 1, "SubItem " & $i, 1)
    _GUICtrlListView_AddSubItem ($hListView2, $i - 1, "SubItem " & $i, 2)

Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

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

    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest ($hListView)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText ($hListView, $Item, $SubItem)
                        Local $iLen = _GUICtrlListView_GetStringWidth ($hListView, $iSubItemText)
                        Local $aRect = _GUICtrlListView_GetSubItemRect ($hListView, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create ($hGUI, $iSubItemText, $aRect[0] + 6, $aRect[1], $iLen + 10, 17, $Style)
                        _GUICtrlEdit_SetSel ($hEdit, 0, -1)
                        _WinAPI_SetFocus ($hEdit)
                        $hDC = _WinAPI_GetWindowDC ($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush (0)
                        FrameRect($hDC, 0, 0, $iLen + 10, 17, $hBrush)

                    EndIf
            EndSwitch
        Case $hListView2
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest ($hListView2)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText ($hListView2, $Item, $SubItem)
                        Local $iLen = _GUICtrlListView_GetStringWidth ($hListView2, $iSubItemText)
                        Local $aRect = _GUICtrlListView_GetSubItemRect ($hListView2, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create ($hGUI, $iSubItemText, $aRect[0] + 6, $aRect[1], $iLen + 10, 17, $Style)
                        _GUICtrlEdit_SetSel ($hEdit, 0, -1)
                        _WinAPI_SetFocus ($hEdit)
                        $hDC = _WinAPI_GetWindowDC ($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush (0)
                        FrameRect($hDC, 0, 0, $iLen + 10, 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)

               ;<<<<< ---- CAN I GET THE LISTVIEW CONTROL HANDLE FROM THE WM_COMMAND PARAMETERS AND USE IT IN A SWITCH HERE?

    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText ($hEdit)
                    _GUICtrlListView_SetItemText ($hListView, $Item, $iText, $SubItem)
                    _WinAPI_DeleteObject ($hBrush)
                    _WinAPI_ReleaseDC ($hEdit, $hDC)
                    _WinAPI_DestroyWindow ($hEdit)

                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc  ;==>WM_COMMAND
Link to comment
Share on other sites

Hi,

#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <GuiListView.au3>;Added by DiscoEd
#include <GUIConstantsEx.au3>;Added by DiscoEd

Opt("GuiCloseOnESC", 0)

Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0, $LVHit = 0

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

$hGUI = GUICreate("ListView Subitems edit in place", 300, 500)

$hListView = _GUICtrlListView_Create ($hGUI, "Items|SubItems|SubItems2", 2, 2, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle ($hListView, $LVS_EX_GRIDLINES)

$hListView2 = _GUICtrlListView_Create ($hGUI, "Items|SubItems|SubItems2", 2, 210, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle ($hListView2, $LVS_EX_GRIDLINES)


For $i = 1 To 10
    _GUICtrlListView_AddItem ($hListView, "Item " & $i)
    _GUICtrlListView_AddSubItem ($hListView, $i - 1, "SubItem " & $i, 1)
    _GUICtrlListView_AddSubItem ($hListView, $i - 1, "SubItem " & $i, 2)
    _GUICtrlListView_AddItem ($hListView2, "Item " & $i)
    _GUICtrlListView_AddSubItem ($hListView2, $i - 1, "SubItem " & $i, 1)
    _GUICtrlListView_AddSubItem ($hListView2, $i - 1, "SubItem " & $i, 2)

Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

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

    Switch $hWndFrom
        Case $hListView,$hListView2
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest ($hWndFrom)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $LVHit = $hWndFrom
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $CGP = ControlGetPos($hGUI, "", $hWndFrom)
                        Local $iSubItemText = _GUICtrlListView_GetItemText ($hWndFrom, $Item, $SubItem)
                        Local $iLen = _GUICtrlListView_GetStringWidth ($hWndFrom, $iSubItemText)
                        Local $aRect = _GUICtrlListView_GetSubItemRect ($hWndFrom, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create ($hGUI, $iSubItemText, $CGP[0] + $aRect[0] + 3, ($CGP[1] + $aRect[1]) - 1, $iLen + 10, 16, $Style)
                        _GUICtrlEdit_SetSel ($hEdit, 0, -1)
                        _WinAPI_SetFocus ($hEdit)
                        $hDC = _WinAPI_GetWindowDC ($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush (0)
                        FrameRect($hDC, 0, 0, $iLen + 10, 16, $hBrush)
                        _WinAPI_DeleteObject ($hBrush)
                        _WinAPI_ReleaseDC ($hEdit, $hDC)
                    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
                    Local $iText = _GUICtrlEdit_GetText ($hEdit)
                    _GUICtrlListView_SetItemText ($LVHit, $Item, $iText, $SubItem)
                    _WinAPI_DestroyWindow ($hEdit)

                    $Item = -1
                    $SubItem = 0
                    $LVHit = 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc  ;==>WM_COMMAND

Cheers

Edited by smashly
Link to comment
Share on other sites

your script gives: muttley

"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\Home\Projects\Auto IT 3.0\Test4.au3"

D:\Home\Projects\Auto IT 3.0\Test4.au3 (65) : ==> Unknown function name.:

$hBrush = _WinAPI_CreateSolidBrush (0)

$hBrush = ^ ERROR

>Exit code: 1 Time: 28.808

-= [font="Verdana"]A Men Who believes in himself and not circumstances is the real Winner =-[/font]

Link to comment
Share on other sites

your script gives: muttley

"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\Home\Projects\Auto IT 3.0\Test4.au3"

D:\Home\Projects\Auto IT 3.0\Test4.au3 (65) : ==> Unknown function name.:

$hBrush = _WinAPI_CreateSolidBrush (0)

$hBrush = ^ ERROR

>Exit code: 1 Time: 28.808

Then you have an error on your end as _WinAPI_CreateSolidBrush () is a function from the WinAPI.au3 include.

Both the op and my updated code have declared the WinAPI.au3 include file in the script and for me it's working like it should.

Link to comment
Share on other sites

your script gives: muttley

"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\Home\Projects\Auto IT 3.0\Test4.au3"

D:\Home\Projects\Auto IT 3.0\Test4.au3 (65) : ==> Unknown function name.:

$hBrush = _WinAPI_CreateSolidBrush (0)

$hBrush = ^ ERROR

>Exit code: 1 Time: 28.808

working for me fine ! While double click on listview i got error !

-= [font="Verdana"]A Men Who believes in himself and not circumstances is the real Winner =-[/font]

Link to comment
Share on other sites

working for me fine ! While double click on listview i got error !

It's a problem on your end.. Update your Autoit to a current version.

That error is happening to you because autoit doesn't see the function _WinAPI_CreateSolidBrush()

That Function is part of the WinAPI.au3 include file which is part of autoit's udf library that comes with autoit.

If your autoit doesn't see the function then you've got something wrong with your autoit.

My first guess at your problem is your using an older version of Autoit.

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...