组合框(Combo)问题:默认文本根据焦点移除和恢复 【已解决】
本帖最后由 xiehuahere 于 2012-10-30 14:15 编辑要求:鼠标点击Combo的输入框,自动去除默认文本;鼠标点击GUI界面其他地方,恢复Combo的默认文本显示。
我试了下面这样有问题,特来求高人指导:
#include <GUIComboBox.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
$Form = GUICreate("test", 200, 150)
$Combo = GUICtrlCreateCombo("Default text", 20, 30, 130, 26)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
_GUICtrlComboBox_LimitText(-1, 15) ;限制输入15个字符
GUISetState()
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
Do
Until GUIGetMsg() = -3
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
If BitAND($wParam, 0x0000FFFF) = $Combo Then ;LOWORD - control identifier
Local $iCode = BitShift($wParam, 16) ;HIWORD - notification code
If $iCode = $CBN_SETFOCUS Then
_GUICtrlComboBox_SetEditText($Combo, "") ;Edit内容置空
ElseIf $iCode = $CBN_KILLFOCUS Then
_GUICtrlComboBox_SetEditText($Combo, "Default text") ;还原default提示
EndIf
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
界面上只有一个组合框控件,两个问题:
1)一开始就拥有输入焦点的情况下,貌似鼠标点击后不会再有$CBN_SETFOCUS消息过来。
2)如何让焦点移除,即什么时候触发$CBN_KILLFOCUS这个消息? 捕捉窗口 $GUI_EVENT_PRIMARYDOWN 消息试试~ 用GuiComboBoxEx.au3的$CBEN_BEGINEDIT试试 本帖最后由 xiehuahere 于 2012-10-30 13:01 编辑
回复 3# netegg
我拿_GUICtrlComboBoxEx_Create帮助中的例子试了下,和我上面的$CBN_SETFOCUS是一个效果。就是只有一个组合框控件的时候,控件已经是active状态了,不会再次发送$CBEN_BEGINEDIT或$CBN_SETFOCUS。只有将GUI最小化再还原的时候会发送。焦点离开是什么消息呢?
我再试试Afan的$GUI_EVENT_PRIMARYDOWN。 本帖最后由 xiehuahere 于 2012-10-30 14:29 编辑
谢谢A大和蛋蛋!
解决了,不过忒麻烦~
#include <GUIComboBox.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$Form = GUICreate("test", 200, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, "_OnExit")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_RemoveInputFocus")
$Combo = GUICtrlCreateCombo("Default text", 20, 30, 130, 26)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$Label = GUICtrlCreateLabel("Text here", 20, 90, 120, 30)
GUISetState()
GUICtrlSetState($Label, $GUI_FOCUS)
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
While 1
Sleep(100)
WEnd
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
If BitAND($wParam, 0x0000FFFF) = $Combo Then ;LOWORD - control identifier
Local $iCode = BitShift($wParam, 16) ;HIWORD - notification code
If $iCode = $CBN_SETFOCUS Then
_GUICtrlComboBox_SetEditText($Combo, "") ;Edit内容置空
ElseIf $iCode = $CBN_KILLFOCUS Then
_GUICtrlComboBox_SetEditText($Combo, "Default text") ;还原default提示
EndIf
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Func _RemoveInputFocus()
; 一开始用GUIGetCursorInfo来判断焦点是否在Combo上,发现不行:
; 点击在Combo的Edit里,判断失效;只有点击到Combo的下拉箭头上,才成功。
; 所以改用 MouseGetPos 来判断
Opt("MouseCoordMode", 2)
Local $array = MouseGetPos()
If Not IsArray($array) Then Return
If $array < 20 Or $array > 150 Or $array < 30 Or $array > 56 Then
GUICtrlSetState($Label, $GUI_FOCUS)
EndIf
EndFunc
Func _OnExit()
Exit
EndFunc 回复 5# xiehuahere #include <GUIComboBox.au3>
Global $Struct = DllStructCreate($tagPoint)
GUICreate('test', 200, 150)
$Combo = GUICtrlCreateCombo('Default text', 20, 30, 130, 26)
GUICtrlSetFont(-1, 12, 400, 0, 'Arial')
$Label = GUICtrlCreateLabel("Text here", 20, 90, 120, 30)
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case -7 ;$GUI_EVENT_PRIMARYDOWN
_Test()
EndSwitch
WEnd
Func _Test()
DllStructSetData($Struct, 'x', MouseGetPos(0))
DllStructSetData($Struct, 'y', MouseGetPos(1))
Local $tPointCast = DllStructCreate('int64', DllStructGetPtr($Struct))
Local $aResult = DllCall("user32.dll", "hwnd", "WindowFromPoint", "int64", DllStructGetData($tPointCast, 1))
If @error Then Return
Local $aC = DllCall('user32.dll', 'int', 'GetClassNameW', 'hwnd', $aResult, 'wstr', '', 'int', 4096)
If $aC == 'Edit' Or $aC == 'ComboBox' Then
If _GUICtrlComboBox_GetEditText($Combo) == 'Default text' Then GUICtrlSetData($Combo, '')
Else
_GUICtrlComboBox_SetEditText($Combo, 'Default text')
GUICtrlSetState($Label, 0x100) ;$GUI_FOCUS = 0x100
EndIf
EndFunc ;==>_Test 回复 6# afan
谢谢,好方法!!
我小小改了下,可以减少几行 ;-)
#include <GUIComboBox.au3>
#include <WinAPI.au3>
Global $Struct = DllStructCreate($tagPoint)
$hGUI = GUICreate('test', 200, 150)
$Combo = GUICtrlCreateCombo('Default text', 20, 30, 130, 26)
GUICtrlSetFont(-1, 12, 400, 0, 'Arial')
$Label = GUICtrlCreateLabel("Text here", 20, 90, 120, 30)
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case -7 ;$GUI_EVENT_PRIMARYDOWN
_Test()
EndSwitch
WEnd
Func _Test()
DllStructSetData($Struct, 'x', MouseGetPos(0))
DllStructSetData($Struct, 'y', MouseGetPos(1))
Local $aC = DllCall('user32.dll', 'int', 'GetClassNameW', 'hwnd', _WinAPI_WindowFromPoint($Struct), 'wstr', '', 'int', 4096)
If $aC == 'Edit' Or $aC == 'ComboBox' Then
If _GUICtrlComboBox_GetEditText($Combo) == 'Default text' Then GUICtrlSetData($Combo, '')
Else
_GUICtrlComboBox_SetEditText($Combo, 'Default text')
GUICtrlSetState($Label, 0x100) ;$GUI_FOCUS = 0x100
EndIf
EndFunc ;==>_Test 正好找这个。学习了 看了一下,不知道有什么用呢
页:
[1]