|
本帖最后由 turboking 于 2012-1-3 22:11 编辑
最近在写一个小程序,需要点击工具栏按钮,弹出子窗口进行一些简单设置。在论坛里边搜索了很久,没有找到答案。请高手指点一下。
第一段代码是用消息模式写的,目标是点击工具栏new按钮,弹出窗口。出现的问题是点击后只显示子窗口,但是子窗口中的控件(比如说button、input等等)显示不了。去掉代码中星号部分,可以正常显示,但是不能关闭。而且没有循环,怎么读子窗口的数据呢。#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
Opt('MustDeclareVars', 1)
$Debug_TB = False ; Check ClassName being passed to functions, set to True and use a handle to another control to see it work
Global $hToolbar
Global $iItem ; Command identifier of the button associated with the notification.
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $hGUI
_Main()
Func _Main()
Local $aSize
; Create GUI
$hGUI = GUICreate("Toolbar", 600, 400)
$hToolbar = _GUICtrlToolbar_Create ($hGUI)
$aSize = _GUICtrlToolbar_GetMaxSize ($hToolbar)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap ($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)
; Add buttons
_GUICtrlToolbar_AddButton ($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton ($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton ($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep ($hToolbar)
_GUICtrlToolbar_AddButton ($hToolbar, $idHelp, $STD_HELP)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main
; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam
Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
Local $tNMTOOLBAR, $tNMTBHOTITEM
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$idFrom = DllStructGetData($tNMHDR, "IDFrom")
$code = DllStructGetData($tNMHDR, "Code")
Switch $hwndFrom
Case $hToolbar
Switch $code
Case $NM_LDOWN
;----------------------------------------------------------------------------------------------
Switch $iItem
Case $idNew
Local $hHostAddForm = GUICreate("Host Add", 226, 298, Default, Default, -1, -1, $hGUI)
Local $ButtonCanel = GUICtrlCreateButton("Cancel", 120, 240, 75, 25, 0)
GUISetState(@SW_SHOW)
;*****************************************************
;删除这部分就可以显示取消按钮
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
;*****************************************************
EndSwitch
;----------------------------------------------------------------------------------------------
Case $TBN_HOTITEMCHANGE
$tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
$i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
$i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
$iItem = $i_idNew
$dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
第二段代码改用事件模式,解决了窗口关闭和显示问题。但是点击子窗口的Ok按钮不能响应。#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
Opt("GUIOnEventMode", 1) ; 定义为事件模式
Opt('MustDeclareVars', 1)
$Debug_TB = False ; Check ClassName being passed to functions, set to True and use a handle to another control to see it work
Global $hToolbar
Global $iItem ; Command identifier of the button associated with the notification.
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $hGUI
Global $ButtonOk,$InputRemarks = 0
_Main()
Func _Main()
Local $aSize
; Create GUI
$hGUI = GUICreate("Toolbar", 600, 400)
$hToolbar = _GUICtrlToolbar_Create ($hGUI)
$aSize = _GUICtrlToolbar_GetMaxSize ($hToolbar)
GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvent")
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap ($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)
; Add buttons
_GUICtrlToolbar_AddButton ($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton ($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton ($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep ($hToolbar)
_GUICtrlToolbar_AddButton ($hToolbar, $idHelp, $STD_HELP)
; Loop until user exits
While 1
Sleep(1000) ; Just idle around
WEnd
EndFunc ;==>_Main
;Events Function
Func _GuiEvent()
Switch @GUI_CtrlId
; 处理窗口关闭事件
Case $GUI_EVENT_CLOSE
Switch @GUI_WinHandle
; 如果是主窗口,直接退出
Case $hGUI
Exit
; 如果是其它窗口,根据发送事件的窗口句柄,关闭该窗口
Case Else
GUIDelete(@GUI_WinHandle)
GUISetState(@SW_ENABLE, $hGUI)
WinActivate($hGUI)
EndSwitch
EndSwitch
EndFunc
; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam
Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
Local $tNMTOOLBAR, $tNMTBHOTITEM
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$idFrom = DllStructGetData($tNMHDR, "IDFrom")
$code = DllStructGetData($tNMHDR, "Code")
Switch $hwndFrom
Case $hToolbar
Switch $code
Case $NM_LDOWN
;----------------------------------------------------------------------------------------------
Switch $iItem
Case $idNew
Local $hHostAddForm = GUICreate("Host Add", 226, 298, Default, Default, -1, -1, $hGUI)
GUICtrlCreateLabel("Test:", 32, 200, 40, 20)
$InputRemarks = GUICtrlCreateInput("...", 80, 200, 129, 21)
$ButtonOk = GUICtrlCreateButton("Ok", 120, 240, 75, 25, 0)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvent")
GUISetOnEvent( $ButtonOK, "_ButtonOk")
EndSwitch
;----------------------------------------------------------------------------------------------
Case $TBN_HOTITEMCHANGE
$tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
$i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
$i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
$iItem = $i_idNew
$dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Func _ButtonOk()
MsgBox(0, "Input Test", $InputRemarks)
EndFunc
|
|