找回密码
 加入
搜索
查看: 5758|回复: 8

[GUI管理] 【已解决】状态栏内嵌滚动条在GUI restore后显示位置不正确

  [复制链接]
发表于 2011-11-4 15:09:27 | 显示全部楼层 |阅读模式
本帖最后由 xiehuahere 于 2011-11-6 21:59 编辑

点击Create按钮后,状态栏内嵌滚动条开始滚动,最小化GUI然后再restore,滚动条显示的位置不对了。看了半天没找到症结所在,请大大们帮忙看看。

代码如下:
#Include <GuiEdit.au3>
#include <GUIListView.au3>
#include <GuiStatusBar.au3>
#include <Editconstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>
#include <SendMessage.au3>
#Include <Misc.au3>

If _Singleton("test", 1) = 0 Then
    Msgbox(4096, "test", "Program is already running!")
    Exit 1
EndIf

Global $msg, $hWnd, $hListview, $hEdit, $hStatus, $progress, $hProgress, $aHit, $hDC, $hBrush
Global $isCreating = 0, $isEnterResp = 0
Global $maxImageNum = 500

Opt("GUIOnEventMode", 1)
Const $width = 280, $height = 320

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("test", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main")
$Label = GUICtrlCreateLabel('Fill in image number for each folder and then click Create button.', 6, 12, 268, 32)
GUICtrlSetFont(-1, 10, 400, 0, "Default", 5)    ; CLEARTYPE_QUALITY=5
GUICtrlSetColor(-1, 0x000080)
; ===============================================================================
; Listview
$hListview = _GUICtrlListView_Create($Form1, '', 6, 60, 266, 157, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_BORDERSELECT, $LVS_EX_FULLROWSELECT))
; ===============================================================================
$Button1 = GUICtrlCreateButton("Create", 60, 248, 160, 32)      ;$BS_DEFPUSHBUTTON
GUICtrlSetOnEvent(-1, "_Main")
GUICtrlSetState(-1, $GUI_FOCUS)
; ===============================================================================
; Status bar - Defaults to 1 part, no text
$hStatus = _GUICtrlStatusBar_Create ($Form1)
_GUICtrlStatusBar_SetMinHeight ($hStatus, 20)
; ===============================================================================
; Insert column into listview and set width
_GUICtrlListView_InsertColumn($hListview, 0, 'Folder', 50)
_GUICtrlListView_InsertColumn($hListview, 1, 'Image Number', 84)
_GUICtrlListView_InsertColumn($hListview, 2, 'Image index range', 132)
; Set Text-Centered format for all three columns
_GUICtrlListView_JustifyColumn($hListview, 0, 2)
_GUICtrlListView_JustifyColumn($hListview, 1, 2)
_GUICtrlListView_JustifyColumn($hListview, 2, 2)
; Add item/subitem
For $i = 0 To 9
        _GUICtrlListView_AddItem($hListview, $i, $i)    ; 1st column
        _GUICtrlListView_AddSubItem($hListview, $i, '0', 1)     ; 2nd column
        _GUICtrlListView_AddSubItem($hListview, $i, '-', 2)     ; 3rd column
Next
; ===============================================================================

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Update status bar
; ===============================================================================
; Initialize parts for status bar
Const $aParts[2] = [170, $width]
_GUICtrlStatusBar_SetParts ($hStatus, $aParts)
; Part 1 - Text status information
; Part 2 - Embedded progress bar
If @OSTYPE = "WIN32_WINDOWS" Then
        $progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_SMOOTH)
        $hProgress = GUICtrlGetHandle($progress)
        _GUICtrlStatusBar_EmbedControl ($hStatus, 1, $hProgress)
Else    ; Generally Win32_NT
        $progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_MARQUEE) ; marquee works on Win XP and above
        $hProgress = GUICtrlGetHandle($progress)
        _GUICtrlStatusBar_EmbedControl ($hStatus, 1, $hProgress)
        _SendMessage($hProgress, $PBM_SETMARQUEE, True, 100) ; marquee works on Win XP and above
EndIf

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

GUICtrlSetState($progress, $GUI_HIDE)

While 1
        Sleep(10)
WEnd

Func _Main()
        Switch @GUI_CtrlId
                Case -3 ; $GUI_EVENT_CLOSE
                                Exit
                Case $Button1
                        $isCreating = 1
                        GUICtrlSetState($Button1, $GUI_DISABLE)
                        GUICtrlSetState($progress, $GUI_SHOW)
                        Sleep(20000)
                        GUICtrlSetState($progress, $GUI_HIDE)
                        GUICtrlSetState($Button1, $GUI_ENABLE)
                        $isCreating = 0
        EndSwitch
EndFunc

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_CLICK
                        $aHit = _GUICtrlListView_SubItemHitTest($hListview)
                        If $aHit[1] = 1 And $isCreating = 0 Then
                                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, $ES_NUMBER)

                                $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(0xFF6600)

                                Local $stRect = DllStructCreate('int;int;int;int')
                                DllStructSetData($stRect, 1, 0) ; left
                                DllStructSetData($stRect, 2, 0) ; top
                                DllStructSetData($stRect, 3, _GUICtrlListView_GetColumnWidth($hListview, $aHit[1])) ; width
                                DllStructSetData($stRect, 4, 17)  ; height
                                _WinAPI_FrameRect($hDC, DllStructGetPtr($stRect), $hBrush)

                                HotKeySet("{ENTER}", "confirm_listview_input_2")
                                $isEnterResp = 0
                        EndIf
        EndSwitch
EndFunc   ;==>WM_NOTIFY

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
        Local $iCode = BitShift($wParam, 16)  ; Get hi-word
        Switch $lParam
                Case $hEdit
                        Switch $iCode
                                Case $EN_KILLFOCUS
                                        If Not $isEnterResp Then confirm_listview_input_1()
                                Case $EN_CHANGE
                                        ; Reset the whole "Image index range" column
                                        For $i = 0 To 9
                                                        _GUICtrlListView_SetItemText($hListview, $i, "-", 2)
                                        Next
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func confirm_listview_input_2()
        $isEnterResp = 1
        confirm_listview_input_1()
        _GUICtrlListView_SetItemSelected($hListview, $aHit[0])
EndFunc

Func confirm_listview_input_1()
        Local $sum = 0
        HotKeySet("{ENTER}")    ; Unset hot key

        Local $iText = _GUICtrlEdit_GetText($hEdit)
        _GUICtrlListView_SetItemText($hListview, $aHit[0], Number($iText), $aHit[1])

        ; Data validation
        For $i = 0 To 9
                        $sum += _GUICtrlListView_GetItemText($hListview, $i, 1)
        Next
        If $sum > $maxImageNum Then
                        MsgBox(4096, "Warning", "Total image number is limited to " & $maxImageNum & "!", 0, $Form1)
                        _GUICtrlListView_SetItemText($hListview, $aHit[0], $maxImageNum-($sum-Number($iText)), $aHit[1])
        EndIf

        _WinAPI_DeleteObject($hBrush)
        _WinAPI_ReleaseDC($hEdit, $hDC)
        _WinAPI_DestroyWindow($hEdit)
        _GUICtrlEdit_Destroy($hEdit)
EndFunc


附带一个小问题:
162行的_GUICtrlListView_SetItemSelected()没有生效,同样没搞清问题在哪儿。
 楼主| 发表于 2011-11-5 12:00:44 | 显示全部楼层
看客多,答者少......
对于提问哪里不清楚,请提出。我继续补充。
发表于 2011-11-5 15:01:54 | 显示全部楼层
  1. #include <GuiEdit.au3>
  2. #include <GUIListView.au3>
  3. #include <GuiStatusBar.au3>
  4. #include <Editconstants.au3>
  5. #include <ButtonConstants.au3>
  6. #include <GUIConstantsEx.au3>
  7. #include <ListViewConstants.au3>
  8. #include <StaticConstants.au3>
  9. #include <WindowsConstants.au3>
  10. #include <ProgressConstants.au3>
  11. #include <SendMessage.au3>
  12. #include <Misc.au3>

  13. If _Singleton("test", 1) = 0 Then
  14.         MsgBox(4096, "test", "Program is already running!")
  15.         Exit 1
  16. EndIf

  17. Global $msg, $hWnd, $hListview, $hEdit, $hStatus, $progress, $hProgress, $aHit, $hDC, $hBrush
  18. Global $isCreating = 0, $isEnterResp = 0
  19. Global $maxImageNum = 500

  20. Opt("GUIOnEventMode", 1)
  21. Const $width = 280, $height = 320

  22. #Region ### START Koda GUI section ### Form=
  23. $Form1 = GUICreate("test", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2)
  24. GUISetOnEvent($GUI_EVENT_CLOSE, "_Main")
  25. $Label = GUICtrlCreateLabel('Fill in image number for each folder and then click Create button.', 6, 12, 268, 32)
  26. GUICtrlSetFont(-1, 10, 400, 0, "Default", 5) ; CLEARTYPE_QUALITY=5
  27. GUICtrlSetColor(-1, 0x000080)
  28. ; ===============================================================================
  29. ; Listview
  30. $hListview = _GUICtrlListView_Create($Form1, '', 6, 60, 266, 157, BitOR($LVS_EDITLABELS, $LVS_REPORT))
  31. _GUICtrlListView_SetExtendedListViewStyle($hListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_BORDERSELECT, $LVS_EX_FULLROWSELECT))
  32. ; ===============================================================================
  33. $Button1 = GUICtrlCreateButton("Create", 60, 248, 160, 32) ;$BS_DEFPUSHBUTTON
  34. GUICtrlSetOnEvent(-1, "_Main")
  35. GUICtrlSetState(-1, $GUI_FOCUS)
  36. ; ===============================================================================
  37. ; Status bar - Defaults to 1 part, no text
  38. $hStatus = _GUICtrlStatusBar_Create($Form1)
  39. _GUICtrlStatusBar_SetMinHeight($hStatus, 20)
  40. ; ===============================================================================
  41. ; Insert column into listview and set width
  42. _GUICtrlListView_InsertColumn($hListview, 0, 'Folder', 50)
  43. _GUICtrlListView_InsertColumn($hListview, 1, 'Image Number', 84)
  44. _GUICtrlListView_InsertColumn($hListview, 2, 'Image index range', 132)
  45. ; Set Text-Centered format for all three columns
  46. _GUICtrlListView_JustifyColumn($hListview, 0, 2)
  47. _GUICtrlListView_JustifyColumn($hListview, 1, 2)
  48. _GUICtrlListView_JustifyColumn($hListview, 2, 2)
  49. ; Add item/subitem
  50. For $i = 0 To 9
  51.         _GUICtrlListView_AddItem($hListview, $i, $i) ; 1st column
  52.         _GUICtrlListView_AddSubItem($hListview, $i, '0', 1) ; 2nd column
  53.         _GUICtrlListView_AddSubItem($hListview, $i, '-', 2) ; 3rd column
  54. Next
  55. ; ===============================================================================

  56. GUISetState(@SW_SHOW)
  57. #EndRegion ### END Koda GUI section ###

  58. ; Update status bar
  59. ; ===============================================================================
  60. ; Initialize parts for status bar
  61. Const $aParts[2] = [170, $width]
  62. _GUICtrlStatusBar_SetParts($hStatus, $aParts)
  63. ; Part 1 - Text status information
  64. ; Part 2 - Embedded progress bar
  65. If @OSType = "WIN32_WINDOWS" Then
  66.         $progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_SMOOTH)
  67.         $hProgress = GUICtrlGetHandle($progress)
  68.         _GUICtrlStatusBar_EmbedControl($hStatus, 1, $hProgress)
  69. Else ; Generally Win32_NT
  70.         $progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_MARQUEE) ; marquee works on Win XP and above
  71.         $hProgress = GUICtrlGetHandle($progress)
  72.         _GUICtrlStatusBar_EmbedControl($hStatus, 1, $hProgress)
  73.         _SendMessage($hProgress, $PBM_SETMARQUEE, True, 100) ; marquee works on Win XP and above
  74. EndIf

  75. GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
  76. GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
  77. GUIRegisterMsg($WM_ACTIVATE, 'WM_ACTIVATE')

  78. GUICtrlSetState($progress, $GUI_HIDE)

  79. While 1
  80.         Sleep(10)
  81. WEnd

  82. Func _Main()
  83.         Switch @GUI_CtrlId
  84.                 Case -3 ; $GUI_EVENT_CLOSE
  85.                         Exit
  86.                 Case $Button1
  87.                         $isCreating = 1
  88.                         GUICtrlSetState($Button1, $GUI_DISABLE)
  89.                         GUICtrlSetState($progress, $GUI_SHOW)
  90.                         Sleep(20000)
  91.                         GUICtrlSetState($progress, $GUI_HIDE)
  92.                         GUICtrlSetState($Button1, $GUI_ENABLE)
  93.                         $isCreating = 0
  94.         EndSwitch
  95. EndFunc   ;==>_Main

  96. Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
  97.         Local $tNMHDR, $hWndFrom, $iCode
  98.         $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
  99.         $hWndFrom = DllStructGetData($tNMHDR, 'hWndFrom')
  100.         $iCode = DllStructGetData($tNMHDR, 'Code')

  101.         Switch $iCode
  102.                 Case $NM_CLICK
  103.                         $aHit = _GUICtrlListView_SubItemHitTest($hListview)
  104.                         If $aHit[1] = 1 And $isCreating = 0 Then
  105.                                 Local $aRect = _GUICtrlListView_GetSubItemRect($hListview, $aHit[0], $aHit[1])
  106.                                 Local $aPos = ControlGetPos($hWnd, '', $hListview)
  107.                                 Local $text = _GUICtrlListView_GetItemText($hListview, $aHit[0], $aHit[1])
  108.                                 Local $iStyle = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT, $ES_NUMBER)

  109.                                 $hEdit = _GUICtrlEdit_Create($hWnd, $text, $aPos[0] + $aRect[0], $aPos[1] + $aRect[1], _
  110.                                                 _GUICtrlListView_GetColumnWidth($hListview, $aHit[1]), 17, $iStyle)
  111.                                 _GUICtrlEdit_SetSel($hEdit, 0, -1)
  112.                                 _WinAPI_SetFocus($hEdit)
  113.                                 $hDC = _WinAPI_GetWindowDC($hEdit)
  114.                                 $hBrush = _WinAPI_CreateSolidBrush(0xFF6600)

  115.                                 Local $stRect = DllStructCreate('int;int;int;int')
  116.                                 DllStructSetData($stRect, 1, 0) ; left
  117.                                 DllStructSetData($stRect, 2, 0) ; top
  118.                                 DllStructSetData($stRect, 3, _GUICtrlListView_GetColumnWidth($hListview, $aHit[1])) ; width
  119.                                 DllStructSetData($stRect, 4, 17) ; height
  120.                                 _WinAPI_FrameRect($hDC, DllStructGetPtr($stRect), $hBrush)

  121.                                 HotKeySet("{ENTER}", "confirm_listview_input_2")
  122.                                 $isEnterResp = 0
  123.                         EndIf
  124.         EndSwitch
  125. EndFunc   ;==>WM_NOTIFY

  126. Func WM_ACTIVATE($hWnd, $msg, $wParam)
  127.         #forceref $hWnd, $msg
  128.         If $wParam = 1 Then _GUICtrlStatusBar_EmbedControl($hStatus, 1, $hProgress)
  129. Endfunc

  130. Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
  131.         Local $iCode = BitShift($wParam, 16) ; Get hi-word

  132.         Switch $lParam
  133.                 Case $hEdit
  134.                         Switch $iCode
  135.                                 Case $EN_KILLFOCUS
  136.                                         If Not $isEnterResp Then confirm_listview_input_1()
  137.                                 Case $EN_CHANGE
  138.                                         ; Reset the whole "Image index range" column
  139.                                         For $i = 0 To 9
  140.                                                 _GUICtrlListView_SetItemText($hListview, $i, "-", 2)
  141.                                         Next
  142.                         EndSwitch
  143.         EndSwitch
  144.         Return $GUI_RUNDEFMSG
  145. EndFunc   ;==>WM_COMMAND

  146. Func confirm_listview_input_2()
  147.         $isEnterResp = 1
  148.         confirm_listview_input_1()
  149.         _GUICtrlListView_SetItemSelected($hListview, $aHit[0])
  150. EndFunc   ;==>confirm_listview_input_2

  151. Func confirm_listview_input_1()
  152.         Local $sum = 0
  153.         HotKeySet("{ENTER}") ; Unset hot key

  154.         Local $iText = _GUICtrlEdit_GetText($hEdit)
  155.         _GUICtrlListView_SetItemText($hListview, $aHit[0], Number($iText), $aHit[1])

  156.         ; Data validation
  157.         For $i = 0 To 9
  158.                 $sum += _GUICtrlListView_GetItemText($hListview, $i, 1)
  159.         Next
  160.         If $sum > $maxImageNum Then
  161.                 MsgBox(4096, "Warning", "Total image number is limited to " & $maxImageNum & "!", 0, $Form1)
  162.                 _GUICtrlListView_SetItemText($hListview, $aHit[0], $maxImageNum - ($sum - Number($iText)), $aHit[1])
  163.         EndIf

  164.         _WinAPI_DeleteObject($hBrush)
  165.         _WinAPI_ReleaseDC($hEdit, $hDC)
  166.         _WinAPI_DestroyWindow($hEdit)
  167.         _GUICtrlEdit_Destroy($hEdit)
  168. EndFunc   ;==>confirm_listview_input_1
复制代码
发表于 2011-11-5 15:33:21 | 显示全部楼层
楼上正解。。呵呵
 楼主| 发表于 2011-11-5 17:36:43 | 显示全部楼层
本帖最后由 xiehuahere 于 2011-11-5 17:37 编辑

回复 3# afan


    原来是需要处理 WM_ACTIVATE,谢谢afan! 虽然并不是很明白为什么滚动条控件在窗口被重新激活后需要再次嵌入。
   
   上面的第二个问题能否也帮忙回答一下?
   我代码中的162行(你代码里的168行)这句,为啥没有生效呢?即在listview中第二列输入内容并按回车确认后,该行(item)不能被高亮选中。
发表于 2011-11-6 00:32:45 | 显示全部楼层
listview选中高亮问题我也遇到过
后来查原因发现是样式问题
你去掉样式试下
 楼主| 发表于 2011-11-6 13:18:37 | 显示全部楼层
回复 6# drunk


    样式都是必须的,不能去啊。。。去掉了试过,也不行。
    谢谢关注!
发表于 2011-11-6 13:35:09 | 显示全部楼层
listview控件失去了焦点而已
 楼主| 发表于 2011-11-6 21:36:26 | 显示全部楼层
本帖最后由 xiehuahere 于 2011-11-6 21:58 编辑

回复 8# afan


早就怀疑是这个问题。但就是一直不知道如何解决。
现在不得不老老实实地换成:
  1. $Listview = GUICtrlCreateListView('', 6, 60, 266, 157, $LVS_REPORT)
  2. $hListview = GUICtrlGetHandle($Listview)
复制代码
然后,
  1. Case $EN_KILLFOCUS
  2.              If $isEnterResp Then
  3.                        GUICtrlSetState($Listview, $GUI_FOCUS)
  4.               _GUICtrlListView_SetItemSelected($hListview, $aHit[0])
  5.              Else
  6.                        confirm_listview_input_1()
  7.             EndIf
复制代码
问题解决。
afan,还是多谢了!
我杀鸡还是用了你这把牛刀。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2026-9-21 00:26 , Processed in 0.076642 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

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