本帖最后由 daiyu116 于 2011-7-8 18:35 编辑
如题,使用滚动条控件管理对GUI设置滚动条后,在该GUI上创建控件时,控件的位置会发生偏移。
可能口述不太清楚,请直接看下面的图。
不滑动边栏,点击按钮创建控件:
滑动边栏后,点击按钮创建控件:
$ReportGUI、$GUImenu、$GUIbody分别是三个GUI窗体。
$ReportGUI为父窗体,$GUImenu、$GUIbody是他的子窗体。
问题如上,请 任何侠们 给予指点!感谢!
下面是源码:
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiDateTimePicker.au3>
#Include <GuiListView.au3>
#include <GUIScrollBars.au3>
#include <ScrollBarConstants.au3>
#Include <Array.au3>
#include <StructureConstants.au3>
Global Const $WM_MOVING = 0x216
Global $addtimes=0
Dim $Group[401]
GUIRegisterMsg($WM_VSCROLL, "WM_VSCROLL")
$ReportGUI= GUICreate("工作报告编写器",635,500,192,124,BitOR($WS_MINIMIZEBOX,$WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
WinSetOnTop($ReportGUI,"", 1)
GUISetState()
$addButton = GUICtrlCreateButton("增加行", 295, 36, 48, 23)
;$GUImenu = GUICreate('Menu',615,150,10, 10,BitOR($WS_SYSMENU, $WS_POPUP,$WS_SIZEBOX,$WS_BORDER, $WS_CHILD), -1, $ReportGUI)
$GUImenu = GUICreate('Menu',615,150,10, 10,BitOR($WS_CHILD,$WS_BORDER), -1, $ReportGUI)
GUISetState()
;$GUIbody = GUICreate('Body',615,300,10,160,BitOR($WS_SYSMENU, $WS_POPUP,$WS_SIZEBOX,$WS_BORDER, $WS_CHILD), -1, $ReportGUI)
$GUIbody = GUICreate('Body',615,320,10,165,BitOR($WS_CHILD,$WS_BORDER), -1, $ReportGUI)
GUISetState()
_GUIScrollBars_Init($GUIbody)
_GUIScrollBars_SetScrollRange($GUIbody,$SB_VERT,10,60)
While 1
$Msg = GUIGetMsg()
Select
Case $Msg=$GUI_EVENT_CLOSE
Exit
Case $Msg=$addButton
GUISwitch($GUIbody)
$Group[10*$addtimes+1] = GUICtrlCreateCheckbox("", 5,5+22*$addtimes,15,20)
$Group[10*$addtimes+2] = _GUICtrlDTP_Create($GUIbody,22,5+22*$addtimes,60,20,$DTS_TIMEFORMAT)
_GUICtrlDTP_SetFormat($Group[10*$addtimes+2], "HH:mm")
$addtimes +=1
EndSelect
WEnd
Func WM_VSCROLL($hWnd, $Msg, $wParam, $lParam)
Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
Local $index = -1, $yChar, $yPos
Local $Min, $bechecked_MaxIndex, $Page, $Pos, $TrackPos
For $x = 0 To UBound($aSB_WindowInfo) - 1
If $aSB_WindowInfo[$x][0] = $hWnd Then
$index = $x
$yChar = $aSB_WindowInfo[$index][3]
ExitLoop
EndIf
Next
If $index = -1 Then Return 0
Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
$Min = DllStructGetData($tSCROLLINFO, "nMin")
$bechecked_MaxIndex = DllStructGetData($tSCROLLINFO, "nMax")
$Page = DllStructGetData($tSCROLLINFO, "nPage")
$yPos = DllStructGetData($tSCROLLINFO, "nPos")
$Pos = $yPos
$TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
Switch $nScrollCode
Case $SB_TOP
DllStructSetData($tSCROLLINFO, "nPos", $Min)
Case $SB_BOTTOM
DllStructSetData($tSCROLLINFO, "nPos", $bechecked_MaxIndex)
Case $SB_LINEUP
DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)
Case $SB_LINEDOWN
DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)
Case $SB_PAGEUP
DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
Case $SB_PAGEDOWN
DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
Case $SB_THUMBTRACK
DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
EndSwitch
DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
_GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
_GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
$Pos = DllStructGetData($tSCROLLINFO, "nPos")
If ($Pos <> $yPos) Then
_GUIScrollBars_ScrollWindow($hWnd, 0, $yChar * ($yPos - $Pos))
$yPos = $Pos
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_VSCROLL
---------------------------------
实际上本问题产生的原因,就是无法正确判断带滚动条的GUI上面,控件的具体位置。
解决方法:
第一种方法,参照4楼设计GUI。
第二种方法,计算控件的实际位置。参照http://www.autoitx.com/thread-25941-1-1.html
UDF请参考http://www.autoitx.com/thread-25955-1-1.html
注意,与afan版主的“_ControlGetPos() [获取指定控件的屏幕坐标及大小]”是有区别的。 |