找回密码
 加入
搜索
查看: 8373|回复: 14

[原创] 在蛋蛋兄的"编辑Listview全部子项"增加单击子项

[复制链接]
发表于 2011-4-2 23:24:11 | 显示全部楼层 |阅读模式
#include <GUIEdit.au3>
#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>


Global $msg, $hWnd, $hListview, $hEdit, $aHit, $aPos, $hDC, $hBrush,$lable

$hWnd = GUICreate('Listview edit all subitem')

$hListview = _GUICtrlListView_Create($hWnd, '', 6, 6, 708, 370, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListview, $LVS_EX_GRIDLINES)
$lable=GUICtrlCreateLabel("",10,10,10,10)
GUICtrlSetState(-1,$gui_HIDE)

For $i = 0 To 4
        _GUICtrlListView_InsertColumn($hListview, 0, 'row' & 4 - $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

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
                        GUICtrlSetState($lable,$gui_HIDE)
                        $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])
                        Local $iStyle = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

                        $hEdit = _GUICtrlEdit_Create($hWnd, $text, $aPos[0] + $aRect[0], $aPos[1] + $aRect[1], _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]), 17, $iStyle)
                        _GUICtrlEdit_SetSel($hEdit, 0, -1)
                        _WinAPI_SetFocus($hEdit)
                        $hDC = _WinAPI_GetWindowDC($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush(0x0000FF)
                        
                        Local $stRect = DllStructCreate('int;int;int;int')
                        DllStructSetData($stRect, 1, 0)
                        DllStructSetData($stRect, 2, 0)
                        DllStructSetData($stRect, 3, _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]))
                        DllStructSetData($stRect, 4, 17)
                        _WinAPI_FrameRect($hDC, DllStructGetPtr($stRect), $hBrush)
                Case $NM_CLICK
                        $aHit1 = _GUICtrlListView_SubItemHitTest($hListview)
                        IF $aHit1[0]<>-1 Then
                        Local $aRect1 = _GUICtrlListView_GetSubItemRect($hListview, $aHit1[0], $aHit1[1])
                        Local $aPos1 = ControlGetPos($hWnd, '', $hListview)
                        Local $text1 = _GUICtrlListView_GetItemText($hListview, $aHit1[0], $aHit1[1])
                        Local $iStyle1 = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)
                        GUICtrlSetState($lable,$GUI_SHOW)
                        GUICtrlSetData($lable,$text1)
                        GUICtrlSetPos($lable,$aPos1[0] + $aRect1[0], $aPos1[1] + $aRect1[1], _GUICtrlListView_GetColumnWidth($hListview, $aHit1[1]),$aRect1[3]-$aRect1[1])                        
                        GUICtrlSetBkColor($lable,0x2468a2)
                        EndIf
        EndSwitch
EndFunc   ;==>WM_NOTIFY

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($hListview, $aHit[0], $iText, $aHit[1])
                                        _WinAPI_DeleteObject($hBrush)
                                        _WinAPI_ReleaseDC($hEdit, $hDC)
                                        _WinAPI_DestroyWindow($hEdit)
                                        _GUICtrlEdit_Destroy($hEdit)
                                        $Item = -1
                                        $SubItem = 0
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

评分

参与人数 1金钱 +50 贡献 +5 收起 理由
afan + 50 + 5 精品文章

查看全部评分

发表于 2011-4-2 23:29:07 | 显示全部楼层
Case $NM_DBLCLK, $NM_CLICK
 楼主| 发表于 2011-4-2 23:52:34 | 显示全部楼层
回复 2# netegg
呵呵,没看源码吧.
其实也很简单.只是加了个LABLE实现单击选中子项而已.
发表于 2011-4-3 00:10:06 | 显示全部楼层
回复 3# 3mile
运行了下,我理解错了,不好意思,不错
发表于 2011-4-3 20:03:03 | 显示全部楼层
好东西,谢谢分享
发表于 2011-4-5 18:35:05 | 显示全部楼层
本帖最后由 蜘蛛抱蛋 于 2011-4-5 18:39 编辑

回复 3# 3mile


又见LABLE。。。这个词在论坛出现的频率灰常高
14行  $lable=GUICtrlCreateLabel("",10,10,10,10,0x50820100);加上边框更逼真了
72行  GUICtrlSetData($lable,' '&$text1);好像表格离得文字都会空一格。。。
74行  GUICtrlSetBkColor($lable,0x3399FF);0x2468a2  在win7下的颜色
编辑框的光标没了,不知道能不能显示出来。、。
发表于 2011-10-29 17:15:24 | 显示全部楼层
本帖最后由 xiehuahere 于 2011-10-31 21:58 编辑

附基础知识:
WM_COMMAND,WM_NOTIFY等消息
分析与理解VC的通知消息WM_NOTIFY  
比较权威的(MSDN,CSDN):
WM_NOTIFY message
WM_COMMAND message
WM_COMMAND和WM_NOTIFY有什么区别?
基本上,Edit,Button,ListBox等发送WM_COMMAND消息,ListView,TreeView,Toolbar等发送WM_NOTIFY消息。

问题:
双击subitem显示编辑框后,鼠标没法做文本选择操作(只能通过键盘操作);
编辑后无法直接按回车确认。
这些如何解决呢?
发表于 2011-10-31 09:08:40 | 显示全部楼层
"编辑subitem后直接按回车确认",这个如何实现呢?
3mile兄的代码只能通过鼠标点击其他地方来实现。
发表于 2011-10-31 15:20:39 | 显示全部楼层
本帖最后由 xiehuahere 于 2011-10-31 22:04 编辑

为ENTER注册热键会影响其他应用程序,所以有没有什么其他方法可以截获ENTER按键消息而又不影响其他应用程序接收这个按键呢?
看到有人说:
如果要处理回车键,最好给所有编辑框添加EN_KILLFOCUS消息,然后重载对话框消息PreTranslateMessage函数,并添加代码
if((pMsg->message ==WM_KEYDOWN) && (pMsg->wParam ==VK_RETURN))
{
    CWnd *pWnd=this.GetFocus();
     this->SetFocus();
     pWnd->SetFocus();
    return 0;
}
对C语言不是很熟悉,谁能帮忙翻译成au3代码?
发表于 2011-11-1 14:20:06 | 显示全部楼层
本帖最后由 xiehuahere 于 2011-11-1 14:33 编辑

回复 1# 3mile

_IsPressed()方法试过了也不行:
$dll = DllOpen("user32.dll")
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
        Local $iCode = BitShift($wParam, 16)
        Switch $lParam
        Case $hEdit
                Switch $iCode
                Case $EN_KILLFOCUS
                                 confirm_listview_input()
                       Case $EN_CHANGE
                               If _IsPressed("0D", $dll) Then
                                           confirm_listview_input()
                                            _GUICtrlListView_SetItemState($hListview, ..., $LVIS_FOCUSED, ...)
                                EndIf
                EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND
 
Func confirm_listview_input()
   。。。。
EndFunc   ;==>confirm_listview_input
 
DllClose($dll)
个人能力不够,望指点。
发表于 2011-11-1 15:34:25 | 显示全部楼层
搜索“listview return” 和 “列表框 回车”未果。
今天搜索了一下“listview 回车”就突然看到了我想要的结果:
http://www.autoitx.com/forum.php ... view%2B%BB%D8%B3%B5

让我情何以堪啊!!
压力山大啊~~~
发表于 2011-11-27 10:10:50 | 显示全部楼层
获益匪浅!
留爪学习!
发表于 2011-11-27 13:15:15 | 显示全部楼层
谢谢分享,我就是这么慢慢积累的,虽然很慢。
发表于 2017-1-2 22:54:13 | 显示全部楼层
......选中项一旦被移出便会消失,怀疑移除便会覆盖
发表于 2017-1-3 14:29:22 | 显示全部楼层
支持
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 21:40 , Processed in 0.080384 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表