Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Import ""
CompilerElse
ImportC ""
CompilerEndIf
PB_Window_Objects.i
PB_Object_Count( Object )
EndImport
Procedure __IsWindow( Window = #PB_All )
If Window = #PB_All
ProcedureReturn PB_Object_Count( PB_Window_Objects.i )
EndIf
ProcedureReturn IsWindow( Window )
EndProcedure :Macro IsWindow( Window = #PB_All ) : __IsWindow( Window ) : EndMacro
Procedure WaitWindowClose( Window = #PB_All )
While IsWindow( Window )
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
CloseWindow( EventWindow() )
EndSelect
Wend
CloseWindow( #PB_All )
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
OpenWindow(1, 0, 0, 322, 150, "window 1", #PB_Window_SystemMenu )
OpenWindow(2, 0, 230, 322, 150, "window 2", #PB_Window_SystemMenu )
OpenWindow(3, 0, 460, 322, 150, "window 3", #PB_Window_SystemMenu )
OpenWindow(4, 0, 0, 322, 150, "window 4", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
; WaitWindowClose( 4 ) ; uncomment to close the application, by closing specified window
WaitWindowClose( )
CompilerEndIf
Code: Select all
Enumeration #PB_Event_FirstCustomValue
#PB_Event_Window
#PB_Event_Quit
EndEnumeration
Structure EventStruct
Event.i
Window.i
CallBack.i
MainWindow.i
EndStructure
Global NewList WindowEventList.EventStruct()
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Import ""
CompilerElse
ImportC ""
CompilerEndIf
PB_Object_EnumerateStart(Object)
PB_Object_EnumerateNext(Object,*ID.Integer)
PB_Object_EnumerateAbort(Object)
PB_Object_Count( Objects )
PB_Window_Objects.l
EndImport
Procedure CountWindow( )
ProcedureReturn PB_Object_Count( PB_Window_Objects )
EndProcedure
Procedure.i CurrentWindow()
Protected Window =- 1
If PB_Window_Objects
PB_Object_EnumerateStart(PB_Window_Objects)
While PB_Object_EnumerateNext(PB_Window_Objects, @Window)
If WindowID(Window) = UseGadgetList(0)
ProcedureReturn Window
EndIf
Wend
PB_Object_EnumerateAbort(PB_Window_Objects)
EndIf
EndProcedure
; Предлогаю изменить на следующее поведение
Procedure CloseWindowAll()
CloseWindow(#PB_All)
EndProcedure
Procedure __CloseWindow( Window )
If Window = #PB_All
PostEvent( #PB_Event_Quit, EventWindow(), #PB_Ignore ) ; - 1 bug
BindEvent( #PB_Event_Quit, @CloseWindowAll(), EventWindow())
Else
If EventWindow() = Window
ProcedureReturn CloseWindow( Window )
Else
PostEvent( #PB_Event_CloseWindow, Window, #PB_Ignore ) ; - 1 bug
ProcedureReturn WindowID(Window)
EndIf
EndIf
EndProcedure
Macro CloseWindow( Window ) : __CloseWindow( Window ) : EndMacro
;
Procedure Event( )
If ListSize(WindowEventList())
FirstElement( WindowEventList() )
If WindowEventList()\Window = EventWindow()
ProcedureReturn WindowEventList()\Event
EndIf
EndIf
EndProcedure
Procedure IsBindEvent( Window )
Protected Result
If IsWindow(Window)
If ListSize(WindowEventList())
ForEach WindowEventList()
If ListIndex(WindowEventList()) ; Исключаем первый элемент
If WindowEventList()\Window = Window
Result = WindowEventList()\Event
Debug "IsBindEvent "+Result
ProcedureReturn Result
EndIf
EndIf
Next
EndIf
EndIf
EndProcedure
Declare WindowEvents( Event )
Procedure Event_Menu_Window() : WindowEvents( #PB_Event_Menu ) : EndProcedure
Procedure Event_Move_Window() : WindowEvents( #PB_Event_MoveWindow ) : EndProcedure
Procedure Event_Size_Window() : WindowEvents( #PB_Event_SizeWindow ) : EndProcedure
Procedure Event_Timer_Window() : WindowEvents( #PB_Event_Timer ) : EndProcedure
Procedure Event_Close_Window() : WindowEvents( #PB_Event_CloseWindow ) : EndProcedure
Procedure Event_Gadget_Window() : WindowEvents( #PB_Event_Gadget ) : EndProcedure
Procedure Event_SysTray_Window() : WindowEvents( #PB_Event_SysTray ) : EndProcedure
Procedure Event_Restore_Window() : WindowEvents( #PB_Event_RestoreWindow ) : EndProcedure
Procedure Event_Repaint_Window() : WindowEvents( #PB_Event_Repaint ) : EndProcedure
Procedure Event_Maximize_Window() : WindowEvents( #PB_Event_MaximizeWindow ) : EndProcedure
Procedure Event_Minimize_Window() : WindowEvents( #PB_Event_MinimizeWindow ) : EndProcedure
Procedure Event_Activate_Window() : WindowEvents( #PB_Event_ActivateWindow ) : EndProcedure
Procedure Event_LeftClick_Window() : WindowEvents( #PB_Event_LeftClick ) : EndProcedure
Procedure Event_GadgetDrop_Window() : WindowEvents( #PB_Event_GadgetDrop ) : EndProcedure
Procedure Event_WindowDrop_Window() : WindowEvents( #PB_Event_WindowDrop ) : EndProcedure
Procedure Event_Deactivate_Window() : WindowEvents( #PB_Event_DeactivateWindow ) : EndProcedure
Procedure Event_RightClick_Window() : WindowEvents( #PB_Event_RightClick ) : EndProcedure
Procedure Event_LeftDoubleClick_Window() : WindowEvents( #PB_Event_LeftDoubleClick ) : EndProcedure
Procedure Event_FileReceived_Window() : WindowEvents( #PB_Event_FileReceived ) : EndProcedure
Procedure Event_ClientConnected_Window() : WindowEvents( #PB_Event_ClientConnected ) : EndProcedure
Procedure Event_ClientDisconnected_Window() : WindowEvents( #PB_Event_ClientDisconnected ) : EndProcedure
Procedure Event_Quit_Window() : WindowEvents( #PB_Event_Quit ) : EndProcedure
Procedure EventsWindow( Window ) ;
BindEvent( #PB_Event_Gadget, @Event_Gadget_Window(), Window)
BindEvent( #PB_Event_DeactivateWindow, @Event_Deactivate_Window(), Window)
BindEvent( #PB_Event_ActivateWindow, @Event_Activate_Window(), Window)
BindEvent( #PB_Event_CloseWindow, @Event_Close_Window(), Window)
BindEvent( #PB_Event_GadgetDrop, @Event_GadgetDrop_Window(), Window)
BindEvent( #PB_Event_LeftClick, @Event_LeftClick_Window(), Window)
BindEvent( #PB_Event_LeftDoubleClick, @Event_LeftDoubleClick_Window(), Window)
BindEvent( #PB_Event_MaximizeWindow, @Event_Maximize_Window(), Window)
BindEvent( #PB_Event_Menu, @Event_Menu_Window(), Window)
BindEvent( #PB_Event_MinimizeWindow, @Event_Minimize_Window(), Window)
BindEvent( #PB_Event_MoveWindow, @Event_Move_Window(), Window)
BindEvent( #PB_Event_Repaint, @Event_Repaint_Window(), Window)
BindEvent( #PB_Event_RestoreWindow, @Event_Restore_Window(), Window)
BindEvent( #PB_Event_RightClick, @Event_RightClick_Window(), Window)
BindEvent( #PB_Event_SizeWindow, @Event_Size_Window(), Window)
BindEvent( #PB_Event_SysTray, @Event_SysTray_Window(), Window)
BindEvent( #PB_Event_Timer, @Event_Timer_Window(), Window)
BindEvent( #PB_Event_WindowDrop, @Event_WindowDrop_Window(), Window)
; CompilerIf #PB_Compiler_OS = #PB_OS_Windows ; антивирус ругается
; BindEvent( #PB_Event_ClientConnected, @Event_ClientConnected_Window(), Window)
; BindEvent( #PB_Event_ClientDisconnected, @Event_ClientDisconnected_Window(), Window)
; BindEvent( #PB_Event_FileReceived, @Event_FileReceived_Window(), Window)
; CompilerEndIf
BindEvent( #PB_Event_Quit, @Event_Quit_Window(), Window)
EndProcedure
Procedure UnEventsWindow( Window ) ;
UnbindEvent( #PB_Event_Gadget, @Event_Gadget_Window(), Window)
UnbindEvent( #PB_Event_DeactivateWindow, @Event_Deactivate_Window(), Window)
UnbindEvent( #PB_Event_ActivateWindow, @Event_Activate_Window(), Window)
UnbindEvent( #PB_Event_CloseWindow, @Event_Close_Window(), Window)
UnbindEvent( #PB_Event_GadgetDrop, @Event_GadgetDrop_Window(), Window)
UnbindEvent( #PB_Event_LeftClick, @Event_LeftClick_Window(), Window)
UnbindEvent( #PB_Event_LeftDoubleClick, @Event_LeftDoubleClick_Window(), Window)
UnbindEvent( #PB_Event_MaximizeWindow, @Event_Maximize_Window(), Window)
UnbindEvent( #PB_Event_Menu, @Event_Menu_Window(), Window)
UnbindEvent( #PB_Event_MinimizeWindow, @Event_Minimize_Window(), Window)
UnbindEvent( #PB_Event_MoveWindow, @Event_Move_Window(), Window)
UnbindEvent( #PB_Event_Repaint, @Event_Repaint_Window(), Window)
UnbindEvent( #PB_Event_RestoreWindow, @Event_Restore_Window(), Window)
UnbindEvent( #PB_Event_RightClick, @Event_RightClick_Window(), Window)
UnbindEvent( #PB_Event_SizeWindow, @Event_Size_Window(), Window)
UnbindEvent( #PB_Event_SysTray, @Event_SysTray_Window(), Window)
UnbindEvent( #PB_Event_Timer, @Event_Timer_Window(), Window)
UnbindEvent( #PB_Event_WindowDrop, @Event_WindowDrop_Window(), Window)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
UnbindEvent( #PB_Event_ClientConnected, @Event_ClientConnected_Window(), Window)
UnbindEvent( #PB_Event_ClientDisconnected, @Event_ClientDisconnected_Window(), Window)
UnbindEvent( #PB_Event_FileReceived, @Event_FileReceived_Window(), Window)
CompilerEndIf
UnbindEvent( #PB_Event_Quit, @Event_Quit_Window(), Window)
EndProcedure
Procedure InitEvents()
Protected Window =- 1
PB_Object_EnumerateStart(PB_Window_Objects)
If PB_Window_Objects
While PB_Object_EnumerateNext(PB_Window_Objects, @Window)
EventsWindow( Window )
Wend
PB_Object_EnumerateAbort(PB_Window_Objects)
EndIf
EventsWindow( #PB_All )
EndProcedure
Procedure WindowEvents( Event )
Protected Window =- 1
If IsWindow(EventWindow())
FirstElement(WindowEventList())
WindowEventList()\Event = Event
WindowEventList()\Window = EventWindow()
EndIf
Select Event
Case #PB_Event_Quit
UnEventsWindow( #PB_All )
FreeList(WindowEventList())
EndSelect
EndProcedure
; Если вышли из петли посылаем сообшение "#PB_Event_Quit"
Procedure QuitCallBack();
Static Window =- 1
If Window =- 1
Window = OpenWindow( #PB_Any, 0,0,0,0, "", #PB_Window_Invisible )
BindEvent( #PB_Event_Quit, @QuitCallBack() )
While IsWindow( Window ) : WaitWindowEvent() : Wend
Else
CloseWindow(Window)
EndIf
EndProcedure
Procedure InitQuit( Window = #PB_All );
PostEvent( #PB_Event_Quit, Window, #PB_Ignore ) ; - 1 bug
QuitCallBack( )
EndProcedure
Procedure WaitWindowClose( Window = #PB_All )
AddElement( WindowEventList() )
WindowEventList()\Window =- 1
WindowEventList()\MainWindow = Window
MoveElement( WindowEventList(), #PB_List_First )
InitEvents()
While CountWindow()
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
If IsWindow(EventWindow())
If IsBindEvent(EventWindow()) : Continue : EndIf
If ( EventWindow() = Window Or Window =- CountWindow() )
CloseWindow( #PB_All )
Else
CloseWindow( EventWindow() )
EndIf
EndIf
EndSelect
Wend
InitQuit( WindowEventList()\Window )
EndProcedure
Procedure BindEventAll( Event, *EventCallBack, Window, Object, EventType ) ;Returns TRUE is purebasic event
If Event = #PB_All
BindEvent( #PB_Event_DeactivateWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_ActivateWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_CloseWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_FirstCustomValue, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_Gadget, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_GadgetDrop, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_LeftClick, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_LeftDoubleClick, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_MaximizeWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_Menu, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_MinimizeWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_MoveWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_Repaint, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_RestoreWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_RightClick, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_SizeWindow, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_SysTray, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_Timer, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_WindowDrop, *EventCallBack, Window, Object, EventType)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
BindEvent( #PB_Event_ClientConnected, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_ClientDisconnected, *EventCallBack, Window, Object, EventType)
BindEvent( #PB_Event_FileReceived, *EventCallBack, Window, Object, EventType)
CompilerEndIf
Else
BindEvent( Event, *EventCallBack, Window, Object, EventType)
EndIf
If ListSize(WindowEventList())
ForEach WindowEventList()
If WindowEventList()\Window = Window
Window =- 1
Break
EndIf
Next
EndIf
If IsWindow(Window)
AddElement(WindowEventList())
WindowEventList()\Event = Event
WindowEventList()\Window = Window
WindowEventList()\CallBack = *EventCallBack
EndIf
EndProcedure
Macro BindEvent( Event, EventCallBack, Window =-1, Object =-1, EventType =-1) :BindEventAll( Event, EventCallBack, Window, Object, EventType ) :EndMacro
CompilerIf #PB_Compiler_IsMainFile
;Debug UseGadgetList(0)
OpenWindow(1, 0, 0, 322, 150, "window 1", #PB_Window_SystemMenu )
ButtonGadget(-1,10,10,100,30,"Button_window_1")
OpenWindow(2, 0, 200, 322, 150, "Window 2", #PB_Window_SystemMenu )
ButtonGadget(-1,10,10,100,30,"Button_window_2")
OpenWindow(4, 0, 0, 322, 150, "window 4", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(-1,10,10,100,30,"Close a window 1")
;ContainerGadget(-1,10,10,100,30) : CloseGadgetList()
;Debug UseGadgetList(0)
;Debug CurrentWindow()
Procedure Event_Window_1()
Debug "Close a window"
If MessageRequester("Warning!!!","Close a window "+Str(EventWindow())+"?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes
CloseWindow( EventWindow() )
EndIf
EndProcedure
Procedure Event_Window_4()
Protected Event = Event()
Select Event
Case #PB_Event_CloseWindow
If MessageRequester("Warning!!!","Close application "+Str(EventWindow())+"?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes
CloseWindow( #PB_All )
EndIf
Case #PB_Event_Gadget
Debug "Gadget"
;UnEventWindows( )
Debug CloseWindow(1)
EndSelect
If Event
Debug #PB_Compiler_Procedure+" - "+EventWindow() +" - "+Event
EndIf
EndProcedure
Procedure Event_Windows()
Protected Event = Event()
Select Event
Case #PB_Event_Gadget
Debug "Gadget555"
EndSelect
If Event
Debug #PB_Compiler_Procedure+" - "+EventWindow() +" - "+Event
EndIf
EndProcedure
; BindEvent( #PB_Event_CloseWindow, @Event_Window_1(), 1)
BindEvent( #PB_Event_CloseWindow, @Event_Window_4(), 4)
BindEvent( #PB_Event_Gadget, @Event_Window_4(), 4)
; ; ;
; ; ; ; BindEvent( #PB_Event_ActivateWindow, @Event_Windows())
; ; ; ; BindEvent( #PB_Event_DeactivateWindow, @Event_Windows())
; ; ; ; BindEvent( #PB_Event_CloseWindow, @Event_Windows())
; ; ; ; BindEvent( #PB_Event_Gadget, @Event_Windows())
; BindEvent( #PB_All, @Event_Window_4(), 4)
; BindEvent( #PB_All, @Event_Windows(),4)
;
Procedure Quit()
Debug "Quit "+CountWindow()+" Window"+EventWindow()
End
EndProcedure
BindEvent( #PB_Event_Quit, @Quit())
WaitWindowClose( 4 )
CompilerEndIf
; IDE Options = PureBasic 5.31 (Windows - x86)
; CursorPosition = 241
; FirstLine = 229
; Folding = -------------
; EnableUnicode
; EnableXP