Listview原地编辑,如何随意置顶显示List控件?(已解决)
本帖最后由 Alam 于 2022-6-2 07:45 编辑就是想实现类似Excel的快捷可选择性输入,想随着Edit输入变化,在List控件提供对应的数据。
但现在卡在无法随心按需要显示list控件。已提供相关简略代码,并附有注释,寻求大大们的帮助。
原地编辑代码来自论坛“蛋”大大
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <Editconstants.au3>
#include <GUIEdit.au3>
#include <GuiButton.au3>
#include <GuiListBox.au3>
#include <WinAPI.au3>
#include <WinAPIEX.au3>
Global $msg, $hWnd, $hListview, $hEdit, $aHit, $aPos, $hDC, $hBrush
Global $hListBox, $hButton
HotKeySet("{Enter}", '_end_edit')
main()
Func main()
$hWnd = GUICreate('')
$hListview = _GUICtrlListView_Create($hWnd, "", 6, 6, 708, 370, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListview, $LVS_EX_GRIDLINES)
For $i = 0 To 4
_GUICtrlListView_InsertColumn($hListview, 0, "row" & $i, 98)
Next
For $i = 0 To 4
_GUICtrlListView_AddItem($hListview, 'item' & $i, $i)
For $j = 1 To 4
_GUICtrlListView_AddSubItem($hListview, $i, 'subitem' & $j, $j)
Next
Next
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
GUISetState(@SW_SHOW)
Do
Until GUIGetMsg() = -3
EndFunc ;==>main
Func _end_edit()
If Not IsHWnd($hEdit) Then
HotKeySet('{enter}')
Send('{enter}')
HotKeySet('{enter}', '_end_edit')
Else
Local $iText = _GUICtrlEdit_GetText($hEdit)
$aHit = _GUICtrlListView_SubItemHitTest($hListview)
_GUICtrlListView_SetItemText($hListview, $aHit, $iText, $aHit)
_WinAPI_DeleteObject($hBrush)
_WinAPI_ReleaseDC($hEdit, $hDC)
_WinAPI_DestroyWindow($hEdit)
_WinAPI_DestroyWindow($hListBox)
_WinAPI_DestroyWindow($hButton)
_GUICtrlEdit_Destroy($hEdit)
_GUICtrlButton_Destroy($hButton)
_GUICtrlListBox_Destroy($hListBox)
$Item = -1
$SubItem = 0
EndIf
EndFunc ;==>_end_edit
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iCode
Case $NM_DBLCLK
If IsHWnd($hEdit) Then _end_edit()
$aHit = _GUICtrlListView_SubItemHitTest($hListview)
Local $aRect = _GUICtrlListView_GetSubItemRect($hListview, $aHit, $aHit)
Local $aPos = ControlGetPos($hWnd, "", $hListview)
Local $text = _GUICtrlListView_GetItemText($hListview, $aHit, $aHit)
$hEdit = _GUICtrlEdit_Create($hWnd, $text, $aPos + $aRect, $aPos + $aRect, _GUICtrlListView_GetColumnWidth($hListview, $aHit) - 19, 17, BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_RIGHT))
$hButton = _GUICtrlButton_Create($hWnd, '^', $aPos + $aRect + _GUICtrlListView_GetColumnWidth($hListview, $aHit) - 17, $aPos + $aRect, 20, 17, BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_RIGHT))
_WinAPI_BringWindowToTop($hButton)
_Create_ListBox() ; 这里新建控件,能正常显示
_GUICtrlEdit_SetSel($hEdit, 0, -1)
_WinAPI_SetFocus($hEdit)
$hDC = _WinAPI_GetWindowDC($hEdit)
$hBrush = _WinAPI_CreateSolidBrush(0x0000FF)
FrameRect($hDC, 0, 0, _GUICtrlListView_GetColumnWidth($hListview, $aHit), 17, $hBrush)
EndSwitch
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)
If IsHWnd($hEdit) Then
Switch $lParam
Case $hButton
If $iCode == $BN_CLICKED Then _Create_ListBox();新建控件,也无法正常显示了
;或者,如何显示隐藏的 ListBox 控件,并置顶显示出来?
Case $hListBox
If $iCode == $LBN_SELCHANGE Then ;选择后,隐藏或销毁控件
_GUICtrlEdit_SetText($hEdit, _GUICtrlListBox_GetText($hListBox, _GUICtrlListBox_GetCurSel($hListBox)))
;ControlHide($hListBox, '', '');;; 隐藏窗口
_GUICtrlListBox_Destroy($hListBox)
_WinAPI_DestroyWindow($hListBox)
EndIf
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Func _Create_ListBox()
$aHit = _GUICtrlListView_SubItemHitTest($hListview)
Local $aRect = _GUICtrlListView_GetSubItemRect($hListview, $aHit, $aHit)
Local $aPos = ControlGetPos($hWnd, "", $hListview)
$hListBox = _GUICtrlListBox_Create($hWnd, '', $aPos + $aRect, $aPos + $aRect + 18, _GUICtrlListView_GetColumnWidth($hListview, $aHit), 17 * 8, BitOR($WS_CHILD, $WS_VISIBLE))
_WinAPI_BringWindowToTop($hListBox)
For $i = 1 To 6
_GUICtrlListBox_AddString($hListBox, $i & $i & $i & $i & $i & $i)
Next
EndFunc ;==>_Create_ListBox
已解决, 显示控件后,设置焦点即可
ControlShow($hListBox, '', '')
ControlFocus ( $hListBox, '', '')
页:
[1]