【已解决】请教两个窗口消息问题
本帖最后由 wangms 于 2015-1-31 15:51 编辑#include <IE.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Example()
Func Example()
; 创建 GUI
$hDateGUI = GUICreate("主GUI", 400, 200)
$idLabel = GUICtrlCreateLabel('Label控件', 2, 2, 350, 196, BitOR($SS_CENTER,$WS_BORDER), $GUI_WS_EX_PARENTDRAG);$SS_CENTER 文本居中,设置$GUI_WS_EX_PARENTDRAG,按住本控件可拖动窗口
GUICtrlSetFont(-1, 16, 500, Default, '微软雅黑') ; 设置上面控件的字体.
GUICtrlSetBkColor(-1, 0x000000) ;控件的背景色
GUICtrlSetColor(-1, 0x00FF00) ;控件的文本颜色
GUISetState(@SW_SHOW)
; 注册消息处理程序
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>Example
; 处理 WM_COMMAND 消息
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch $wParam
Case $e_idOpen
;_WinAPI_ShowMsg("你点击了 [打开] 菜单")
_Date() ;显示万年历
Case $e_idSave
_WinAPI_ShowMsg("你点击了 [保存]菜单")
Case $e_idInfo
_WinAPI_ShowMsg("你点击了 [信息] 菜单")
EndSwitch
EndFunc ;==>WM_COMMAND
; 处理 WM_CONTEXTMENU 消息
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Local $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "打开万年历", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "保存", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "信息", $e_idInfo)
;_GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd) ;谢谢 A大出手指点!
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU
Func _Date()
$oIE = _IECreateEmbedded()
$hDateGUI = GUICreate("万年历", 622, 450, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$GUIActiveX = GUICtrlCreateObj($oIE, 0, 0, 622, 450)
GUISetState(@SW_SHOW)
_IENavigate ($oIE,"http://www.jxteacher.com/userfiles/plyt2011/flash/2014061413444395.swf")
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
MsgBox(0,0,'测试$GUI_EVENT_CLOSE消息')
ExitLoop
EndSwitch
WEnd
$oIE = 0
GUISetState(@SW_HIDE, $hDateGUI)
GUIDelete($hDateGUI)
EndFunc ;==>_Date
现在问题是,关闭“万年历”窗口,就带着主GUI也关闭了,74# 行 MsgBox(0,0,'测试$GUI_EVENT_CLOSE消息') 语句根本不起作用,请教老师们帮忙指点!! 自己先顶一顶。。。。。。。。。。。。 回复 1# wangms
是不是这样?
#include <IE.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Global $hDateGUI,$hDateGUIa
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; 创建 GUI
$hDateGUI = GUICreate("主GUI", 400, 200)
$idLabel = GUICtrlCreateLabel('Label控件', 2, 2, 350, 196, BitOR($SS_CENTER,$WS_BORDER), $GUI_WS_EX_PARENTDRAG);$SS_CENTER 文本居中,设置$GUI_WS_EX_PARENTDRAG,按住本控件可拖动窗口
GUICtrlSetFont(-1, 16, 500, Default, '微软雅黑') ; 设置上面控件的字体.
GUICtrlSetBkColor(-1, 0x000000) ;控件的背景色
GUICtrlSetColor(-1, 0x00FF00) ;控件的文本颜色
GUISetOnEvent($GUI_EVENT_CLOSE, "GForm1Close")
GUISetState(@SW_SHOW)
; 注册消息处理程序
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>Example
; 处理 WM_COMMAND 消息
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch $wParam
Case $e_idOpen
;_WinAPI_ShowMsg("你点击了 [打开] 菜单")
_Date() ;显示万年历
Case $e_idSave
_WinAPI_ShowMsg("你点击了 [保存]菜单")
Case $e_idInfo
_WinAPI_ShowMsg("你点击了 [信息] 菜单")
EndSwitch
EndFunc ;==>WM_COMMAND
; 处理 WM_CONTEXTMENU 消息
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Local $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "打开万年历", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "保存", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "信息", $e_idInfo)
;_GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd) ;谢谢 A大出手指点!
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU
Func _Date()
$oIE = _IECreateEmbedded()
$hDateGUIa = GUICreate("万年历", 622, 450, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$GUIActiveX = GUICtrlCreateObj($oIE, 0, 0, 622, 450)
GUISetOnEvent($GUI_EVENT_CLOSE, "hDateGUIClose")
GUISetState(@SW_SHOW)
_IENavigate ($oIE,"http://www.jxteacher.com/userfiles/plyt2011/flash/2014061413444395.swf")
$oIE = 0
EndFunc ;==>_Date
Func GForm1Close()
Exit
EndFunc ;==>GForm1Close
Func hDateGUIClose()
MsgBox(0,0,'测试$GUI_EVENT_CLOSE消息')
GUISetState(@SW_HIDE, $hDateGUIa)
;GUIDelete($hDateGUI)
GUISetState(@SW_SHOW,$hDateGUI)
EndFunc ;==>GForm1Close 诸如 WM_COMMAND 消息里面是不能使用或调用耗时过程代码的,何况还是个无限循环,造成界面僵持,也无法再响应外部消息事件。改用如上 chzj589所用的OnEvent事件模式比较简单。如果仍想继续使用消息循环模式,创建日历窗口后应及时返回,然后在主循环中使用 GUIGetMsg(1) 高级模式处理来自各个窗口的消息。 谢谢上面两位老师指点,由于我的主程序是采用消息模式,我只能按照A大指点再摆弄摆弄。 请教A大,‘创建日历窗口后应及时返回’这句该如何实现?是不是加一句 return 1 这样的语句? 楼上正解,感谢楼上,学习下,非常感谢 本帖最后由 wangms 于 2015-1-31 15:50 编辑
终于在A大指点下,自己还是用消息模式搞定:两个GUI窗口的退出显示互不干扰。#include <IE.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Opt("GUICloseOnESC", 0);取消esc退出
Opt("TrayMenuMode", 3) ; 默认托盘菜单项目将不会显示, 当选定项目时也不检查. TrayMenuMode 的其它选项为 1, 2.
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Local $hMainGUI, $hDateGUI
Local $idExit = TrayCreateItem("退出")
MainGUI()
; 注册消息处理程序
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
; 循环到用户退出.
Do
Switch TrayGetMsg()
Case $idExit ; 退出循环.
ExitLoop
EndSwitch
Until 0
Func MainGUI()
; 创建 GUI
$hMainGUI = GUICreate("主GUI", 400, 200)
$idLabel = GUICtrlCreateLabel('Label控件', 2, 2, 350, 196, BitOR($SS_CENTER,$WS_BORDER), $GUI_WS_EX_PARENTDRAG);$SS_CENTER 文本居中,设置$GUI_WS_EX_PARENTDRAG,按住本控件可拖动窗口
GUICtrlSetFont(-1, 16, 500, Default, '微软雅黑') ; 设置上面控件的字体.
GUICtrlSetBkColor(-1, 0x000000) ;控件的背景色
GUICtrlSetColor(-1, 0x00FF00) ;控件的文本颜色
GUISetState(@SW_SHOW)
EndFunc ;==>MainGUI
Func _Date()
$oIE = _IECreateEmbedded()
$hDateGUI = GUICreate("万年历", 622, 450, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$GUIActiveX = GUICtrlCreateObj($oIE, 0, 0, 622, 450)
GUISetState(@SW_SHOW)
_IENavigate ($oIE,"http://www.jxteacher.com/userfiles/plyt2011/flash/2014061413444395.swf")
#cs
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
MsgBox(0,0,'测试$GUI_EVENT_CLOSE消息')
ExitLoop
EndSwitch
WEnd
$oIE = 0
GUISetState(@SW_HIDE, $hDateGUI)
GUIDelete($hDateGUI)
#ce
EndFunc ;==>_Date
; 处理 WM_COMMAND 消息
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch $wParam
Case $e_idOpen
;_WinAPI_ShowMsg("你点击了 [打开] 菜单")
_Date() ;显示万年历
Case $e_idSave
_WinAPI_ShowMsg("你点击了 [保存]菜单")
Case $e_idInfo
_WinAPI_ShowMsg("你点击了 [信息] 菜单")
EndSwitch
EndFunc ;==>WM_COMMAND
; 处理 WM_CONTEXTMENU 消息
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Local $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "打开万年历", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "保存", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "信息", $e_idInfo)
;_GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd) ;谢谢 A大出手指点!
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU
Func WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
Local $nID = BitAND($wParam, 0x0000FFFF)
Switch $nID
Case $SC_CLOSE
;If $hWnd = $hMainGUI Then
; Exit
;Else
GUIDelete($hWnd)
;EndIf
EndSwitch
EndFunc ;==>WM_SYSCOMMAND
新手正在学习中~~~ 又学到了一个新知识啊
页:
[1]