SomeT wrote:...explain to me the following:
Code: Select all
OpenWindow_0()
Repeat
event = WaitWindowEvent()
result = Window_0_Events(event)
Until result = #False
- What does that part of the code do exactly above? Is it meant to close the window?
- Do I need this bit of code in there, is it necessary?
- In future if making more complex programs would I have to put a piece of code like this in, in order to shut down a program correctly?
Hello again SomeT. That portion, known as the
message processing loop or
events loop, is a very important and integral part of any program.
In order to understand that, we'd have to take a quick look at how operating systems work. Most major OSes, including Windows, Linux and OSX, are event-based, which means that they operate through sending and receiving
messages to and from the programs that run on them. For example, when a program opens a window, it sends a bunch of messages to the OS to tell it what type of window it wants to open; the size, position, title, color, etc. The OS in turn responds with another bunch of messages, confirming that it has received the program's messages, and ultimately giving the program the details of the newly created window. Thereafter, the exchange of messages between the OS and the program will continue, which has to be processed and executed. And this is exactly what the
message processing loop does.
These messages are also referred to as events, because essentially, almost all messages are raised by some event, like the click of a button on a window, the movement or resizing of a window, or keyboard input from the user. But these events are not captured by the program itself, but rather by the OS, which will then dispatch them to the program for processing.
To illustrate this, imagine a user clicking on the button in your
Hello World! program. The button click is actually simply a mouse click; the OS captures the exact location of that click, determines if there is any window or gadget at the point of that click, and then sends the click event to the program that owns the window or gadget
(a gadget is technically also just a window, but that's another story). Then, it is up to the program to process and handle that event; in the case of your
Hello World! example, it displays a message box.
Clearly oversimplified, but in a nutshell, that's it.
Here's how PureBasic does it:
Code: Select all
window = OpenWindow(#PB_Any, 100, 100, 200, 200, "Window Title")
;PureBasic sends a request to create a window at position 100,100
;with a size of 200,200 and title it "Window Title".
;The OS sends a success or failure response into the variable "window".
;If successfully created, the variable will contain the handle (identifier)
;to the newly created window, otherwise it will be zero (failed to create).
button = ButtonGadget(#PB_Any, 50, 80, 100, 30, "Click Me")
;PureBasic sends a request to create a gadget (actually a window) at position 50,80
;inside the previously created window, with a size of 100,30 and title it "Click Me".
;The OS sends a success or failure response into the variable "button", as before.
Repeat ;continuously run these set of instructions
event = WaitWindowEvent()
;listen for messages from the OS and take the current message and place it
;into the variable named "event" - then start evaluating the current message.
If event = #PB_Event_CloseWindow
;if the message is to close the window, then end the program.
End
ElseIf event = #PB_Event_Gadget
;if the message belongs to a gadget (child window), check which one.
gadget = EventGadget()
;re-process the current message to get the handle for the gadget (child window)
If gadget = button
;if the message is for the gadget with the handle "button", display this message box
MessageRequester("PureBasic", "Hello World!")
EndIf
EndIf
ForEver ;continue listening for messages
I hope that this answers your first question, about what that part of the program does. And to answer the other three;
yes, it is meant to close the window
(among other things), and
yes, you do need this bit of code
because it is necessary. And finally, regardless of how simple or complex your programs may be, the message processing loop is absolutely indispensable.
Sorry if the reply verges on pedantic, but I just wanted to be clear.
