WndProc()

Pages: 123
1
2
3
4
5
6
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}


why not just put:

1
2
3
4
5
6
7
8
int addition ()
{
int a, 
int b
int r;
r=a+b;
return (r);
}


in the first one, you can pass different numbers to addition() every time you call it. for instance, you could type int i = addition(2, 4);. the parameters tell the compiler what types of data and how many of each you can pass to the function. the purpose of this function is to add two numbers, but in the second example, there's no way to tell it what two numbers to add!

in the second example, you're going to going to call addition() and you're either going to get a complier error because you tried to pass arguments to a function with no defined parameters or you'll pass nothing and it will return to you the sum of whatever is at the memory locations of a and b
A window procedure function is a function that will handle all your messages that are sent to the program from the user. A message is a key press or a mouse click, or some other thing. Here is an example window procedure function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
LRESULT CALLBACK WindowProcedure (HWND hWnd, //Handle to the window for this function
								  unsigned int uiMsg, //The message
								  WPARAM wParam, //Basically another part of the message
								  LPARAM lParam) //Basically another part of the message
{
	switch (uiMsg) //All messages are int. So we can use a switch statement to handle them.
	{
		case WM_CLOSE: //WM_CLOSE is defined as a int, this message is sent to our window procedure function when we go to close the window
			DestroyWindow (hWnd); //Send a WM_DESTROY message to the message queue for the hWnd handle.
			//The message queue is a place where messages go and wait to be sent to this function (From the DispatchMessage(&uMsg) funcion)
			break;
		case WM_DESTROY: //This will be used it WM_DESTROY is our message
			PostQuitMessage (0); //Send WM_QUIT to the message queue, this will break out message loop and then exit the application
			break;
		case WM_KEYDOWN: //This is used if we press a key
			switch (LOWORD (wParam)) //The actual key is in the lower 16bits of the wParam, so we need to do this
			{
				case VK_RETURN: //If we pressed the Enter/Return key
					MessageBox (hWnd, "The Enter/Return key was pressed", "Message:", MB_OK);
					break;
				case VK_TAB: //If we pressed the tab key
					MessageBox (hWnd, "The Tab key was pressed", "Message", MB_OK);
					break;
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam); //Handle all the messages that we didn't
}


I added comments to try and help you better under stand it. Let me know if you have any questions.
Ah, so the whole point of parameters is so you can PASS different numbers to addition() EVERY TIME you call it, so one time it could be int addition (int a, int b), and another time it could be int addition (int c, int d),

am i right?

Thanks to both for replying,


Almost. Every time you call addition, it will still be int addition(int a, int b), only a and b will be initialized with whatever values you pass:

1
2
addition(4, 5);  // a is 4 and b is 5
addition(1, 1);  // this time a is 1 and b is 1 
Ok, so, summary of parameters...

The whole point of parameters is so you can PASS different numbers to addition() EVERY TIME you call it. If you have...

int addition (int a, int b)
{
}

...one time you could put

addition (4, 4); then both a and b would hold the value of 4...

...and another time i could have:

addition (8, 7); then a would hold 8, and b would hold 7.

And thats how parameters are used?




Was my previous post correct or not ?
Yes.
Thanks, can you tell me if im right with the parameters of the wndproc function:

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

hwnd - This tells windows which window to send the message to

imsg - This is a way for windows to identify the message, or is it the actual message

wparam and lparam - Are these just parameters for extra values, for example, if the message is that the mouse has been moved over your window then the extra parameters include the x, y screen position of the mouse.

?
???
hwnd tells you which window sent the message. imsg is the message. AFAIR, a message is just a define which resolves to an int (hexadecimal). wParam and lParam are indeed used to pass any kind of value referring to the message. To interpret them correctly, you need to look at MSDN to see what kind of data the message you want to process passes into them.
Ok, so hwnd is where the message is being sent to, wParam and lParam are indeed used to pass any kind of value referring to the message, but im a little confused on imsg, in BASIC terms, is this the actual message, or is it a way for windows to identify the message???
filipe wrote:
hwnd tells you which window sent the message

It's being sent to WndProc().

but im a little confused on imsg, in BASIC terms, is this the actual message, or is it a way for windows to identify the message

In the terms you're thinking, there is no message. The message is just a number, plus eventual wParam and lParam data. It is the programmer's job to know what that number means and deal with it. And please don't try to think in BASIC terms.
ok, so imsg IS the actualy message, and NOT a way for windows to indentify the message, i got confused because sometimes people refer to it as an identifier, which makes me think that it is a way for windows to indentify the message.

Correct?
In a way, a message is just an identifier. Example: your WndProc() function gets a WM_MOUSEMOVE message. It's just a number (0x0200), but you know it means the mouse moved. Looking at MSDN, you can understand how to handle it: http://msdn.microsoft.com/en-us/library/ms645616(VS.85).aspx . wParam carries a number that indicates whether some interesting keys were pressed at the time, while lParam carries the coordinates for the mouse cursor.

So you see, that's all there is to a message. And of course Windows can process its own messages. The real "message" is that the mouse was moved. To Windows, "the mouse was moved" simply translates to 0x0200. When it gets that number while processing a message, it knows what to do.
Last edited on
Thanks for that, that link cleared things up.
Topic archived. No new replies allowed.
Pages: 123