March 28, 2024, 08:32:13 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Understanding constants and messages

Started by Andy, November 18, 2018, 04:23:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

November 18, 2018, 04:23:48 AM Last Edit: November 18, 2018, 04:52:13 AM by Andy
For the longest time I was totally confused over messages in windows programming, so hopefully this will help any one learning about them.

Constants:

A constant will be given a name plus a value assigned to it â€" this value does not change, it remains “constant”, hence the name constant.

CONST MyWidgit = 10

MyWigit is a constant I made up just now, and gave it a value of 10.

Now MS have thousands of constants that they have named, and each has a value they have assigned to them.

So now we have this in mind, I will move on the messages.

Messages:

What is a message in programming, and what does it have to do with constants?

All controls we make in a window (button, edit box, static etc) are all windows in their own right, and the “parent” window is the window they appear in.

These “controls” are able to accept “messages” from a program you write â€" but also, they can “send a message” to your program with some information.

We use the SendMessage command to do any of these two functions.

So lets take an example

(Example 1):

SENDMESSAGE win,LVM_DeleteAllItems,0,0,ListView

And break it down….

“win” is the parent window in our program

“LVM_DeleteAllItems” is a name of a constant that will tell the control what we want it to do â€" in this case delete all items out of a list view.

0,0, we will ignore just for now.

ListView is the name of our list view control in our program.

So we are sending a message to our list view control to delete all its items, and the list view belongs to the window “win”.

Now what about those other paramers 0,0?

These two parameters are used to send extra “information / instructions” to the control if we need to, in the above example they are not needed hence we leave them zero.

These parameters are called Wparam & Lparam â€" it is not important to know why they are named like that, just that at times we will need them.


(Example 2):

Getting information from a control.

TextLength = SendMessage(win,WM_GETTEXTLENGTH,0,0,EditBox)

With the same window “win” we want to get the length of text that is in an edit box (here called “EditBox”) control, so here we can use the constant “WM_GETTEXTLENGTH”.

The variable TextLength will hold the value (the number of characters that are typed in the edit box), which could be 0,1,2… 58 etc.

How on earth do I know what constant to use?

This is what has confused me for such a long time, and this is how I do it…..

If we take example 2, I knew I wanted to find out how many characters were in an edit box, so logically it must have something to do with “text”, “characters”, “length” etc.

Before I wrote the constants search program (link at the bottom), I would search the Internet for something like “number of characters in an edit box”, but that didn’t really help.

I was told of “msdn” which is MS’s main site for programming development.

Adding “msdn” onto the search got me closer, but still really didn’t find anything.

This is why I wrote the constants search program, it makes life so much easier.

In the program if I search for “text” and “All control types” it does appear in a list of 96 search results.

If I search “length” and “All control types” it shows just 9 results, either way you come up with WM_GETTEXTLENGTH in both.

When you read its description:

“An application sends a WM_GETTEXTLENGTH message to determine the length, in characters, of the text associated with a window” â€" here a window for us means the edit box control â€" we know that’s the one to use.

Finally, if we search the Internet like this “WM_GETTEXTLENGTH msdn” we get exactly what we are searching for â€" the answer.

msdn describes what it does, and tells you what those Wparam and Lparam values should be, here zero in both cases.

Constants search program:
https://www.ionicwind.com/forums/index.php?topic=6245.msg45848;topicseen#msg45848

Phew.

Hope this is clearer than mud!

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.