Dark basic dude79,
When you write a program, in a language such as C++ or DBP for instance, it is compiled into machine code instructions (the ones I talked about earlier), which are fed to the CPU and "interpreted".
So for example, then you press the X button to close the window: writing the program, you write a function that gets called when the button is clicked. The code that checks when it's clicked is provided by the OS. All this code is linked up when you compile. The code that checks for the clicking of the X button, and the code that is called when the X button is clicked, and all the rest, is all compiled into a single program - the EXE, which contains, amongst other things, the compiled machine code.
So when this is fed to the CPU, it doesn't differentiate between the code provided by the OS, and the code written by the user. It's all just machine code now. So when the user presses the red X, the piece of code provided by the OS is executed (simply just because it's told to - like an if-statement), and, since you told it to call your function, the CPU will blindly jump to that function, and do what it's told. As you might expect, the code you write in this function tells the process to free any memory it's got, any files it has open, etc.
Bottom line, all the code that makes up a Windows application gets turned into very simple instructions that the CPU will run without questioning it, or without knowing it's overall purpose.
Hope that explains it.
"everyone forgets a semi-colon sometimes." - Phaelax