-
LPARAM and WPARAM
I am reading Petzold. And, of course, for the messages sent to the window procedure, there are the two data types LPARAM and WPARAM. Petzold didn't really explain them and what they are actually implemented for (in messages). Well, the problem is that I get confused in the message handling of whether or not it is LPARAM or WPARAM or HIWORD(LPARAM) or LOWORD(WPARAM).
Can anybody explain HIWORD and LOWORD, LPARAM and WPARAM? Thanks.
--Garfield the Confused
-
The message that gets generated and passed to GetMessage() from Windows is a struct that looks like this -
Code:
typedef struct tagMSG { // msg
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;
Windows then passes the first four parameters in this struct to your WndProc. WPARAM and LPARAM are both four bytes long and are used to send extra information along with the UINT message. Sometimes to squeeze extra information into these two parameters data is packed into each WORD (each 2 bytes). Therefore to extract this info you can use the HIWORD() and LOWORD() macros which look like -
Code:
#define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff))
#define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16))
I don't think there are set rules what about what type of data is sent in which parameter, so you'll either have to memorise what data is sent in each parameter for each message (if any) or look them up when you want to use them.
-
Oh, I guess I'll try to memorize. They just get so confusing sometimes and when you mix them up, they don't generate an error (well, at least no for me) so it takes a while to figure out the problem.
Thanks!
--Garfield the Programmer
-
Yeah, you just have to memorize it... I used to have some sort of memory aide for Height and Width coordinates sent with WM_SIZE but now I couldn't tell you for the life of me...
I think:
x = LOWORD(lParam);
y = HIWORD(lParam);
but I really can't remeber..haven't had to use it in a long time.
-
> but I really can't remeber..haven't had to use it in a long time.
Why not? When was the last time you had to (or wanted to) program a Windows application? I always thought that the WM_SIZE message was quite popular.
Thanks.
--Garfield the Programmer
-
So many messages, so many params, too little time. Use the help, thats what it is for. No need to spend time rote learning when it will cost MUCH less time to look up the params rather than making a hard to find mistake.
Remember a lot of controls send WM_NOTIFY mgs that are pointers to stucts. Are you going to memorise all of them?
Next time I use WM_SIZE or(WM_EXITSIZEMOVE, WM_ENTERSIZEMOVE, WM_SIZING) ect I will look it up, even though I used it yesterday.
-
well generally because most of the apps I've been doing lately have been dialog-based. Either that or I'm modifying old programs of mine and I already have the WM_SIZE completed.
Not to mention I can't find enough time lately to program much...:(
-
> Not to mention I can't find enough time lately to program much...
Ouch!!! Doesn't school cramp your style? I try to stay up for 2 extra hours to program. It is hard, but it is worthwhile.
--Garfield the Programmer