#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[0], $iText, $aHit[1])
_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[0], $aHit[1])
Local $aPos = ControlGetPos($hWnd, "", $hListview)
Local $text = _GUICtrlListView_GetItemText($hListview, $aHit[0], $aHit[1])
$hEdit = _GUICtrlEdit_Create($hWnd, $text, $aPos[0] + $aRect[0], $aPos[1] + $aRect[1], _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]) - 19, 17, BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_RIGHT))
$hButton = _GUICtrlButton_Create($hWnd, '^', $aPos[0] + $aRect[0] + _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]) - 17, $aPos[1] + $aRect[1], 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[1]), 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[0], $aHit[1])
Local $aPos = ControlGetPos($hWnd, "", $hListview)
$hListBox = _GUICtrlListBox_Create($hWnd, '', $aPos[0] + $aRect[0], $aPos[1] + $aRect[1] + 18, _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]), 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