Author Topic: Common mistake with mouse coordinates and control ID  (Read 6797 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2399
  • Gender: Male
    • www.zapsolution.com
Common mistake with mouse coordinates and control ID
« on: March 12, 2009, 09:56:36 PM »
I see very often people using unsigned integer (word) to split lParam to retrieve the mouse parameters from message crackers, like this:

TYPE POINTAPI
  x AS LONG
  y AS LONG
END TYPE

Code: [Select]
LOCAL p AS POINTAPI

CASE %WM_MOUSEMOVE           
     p.X = LO(WORD, lParam) '// WRONG
     p.Y = HI(WORD, lParam) '// WRONG

when they should use this

Code: [Select]
LOCAL p AS POINTAPI

CASE %WM_MOUSEMOVE           
     p.X = LO(INTEGER, lParam) '// GOOD
     p.Y = HI(INTEGER, lParam) '// GOOD

Indeed mouse coordinates can be negative, especialy when using multiple monitor display, thus you should always use signed integer to retrieve the correct X,Y mouse location.

Added:
If for any reason you use a negative ID for in a specific control, then you must also use the same LO(INTEGER, wParam) to parse the %WM_COMMAND and retrieve the correct ID.

...
« Last Edit: March 13, 2009, 09:04:22 AM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Jürgen Huhn

  • Full Member
  • ***
  • Posts: 105
  • Gender: Male
Re: Common mistake with mouse coordinates and control ID
« Reply #1 on: March 14, 2009, 02:36:33 AM »
Hi Patrice!!

That`s very interisting!!

What do you say to this?

    Case %WM_MOUSEMOVE
          pt.x = LoWrd(lParam) : pt.y = HiWrd(lParam)
.¸.•’´¯)¸.•’´¯)¸.•’´¯)¸.•’´¯)
¤ª“˜¨¨¯¯¨¨˜“ª¤....¤ ª“˜¨

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3126
  • Gender: Male
    • José Roca Software
Re: Common mistake with mouse coordinates and control ID
« Reply #2 on: March 14, 2009, 06:10:55 AM »
 
LoWrd(lParam) is the same that LO(WORD, lParam). Patrice is advising to use LO(INTEGER, lParam) because LO(WORD) or LoWrd will always return a positive number, even if the coordinates are negative. For example, if the coordinate is -1, using LoWrd will return 65535. If you use LoWrd or HiWrd, then you will need to convert the result to an integer, i.e. pt.x = CINT(LoWrd(lParam)).

Offline Jürgen Huhn

  • Full Member
  • ***
  • Posts: 105
  • Gender: Male
Re: Common mistake with mouse coordinates and control ID
« Reply #3 on: March 14, 2009, 02:20:35 PM »
 :-*

Thank you for this very important Lesson!!

I`m just learning Powerbasic and  by this, i study a lot Exaqmples with this Usage
when coordinates are negative. That`s why i thougt until now, that`s encounter no Problem
with PowerBasic and The API!

But then, i have to do it in any Case when the coordinates are negative and positive ..
Not only for the CASE %WM_MOUSEMOVE..!! 
         
It`s right to do,

for Example:
Variable = wParam

    Case %WM_COMMAND

         Select Case Long Lo(Integer, wParam)

         Case -10 to -1

        ...

              IF Variable = - 10 then 

         ...

             end if
          Case 0 to 10
              IF Variable = 10 then 

         ...

             end if
        end select

and when they are positive it`s right to do this:
i.e.
        Case %WM_SIZE
            Resize_XXX LoWrd(lParam),HiWrd(lParam)

??
..

« Last Edit: March 19, 2009, 03:52:21 AM by Jürgen Huhn »
.¸.•’´¯)¸.•’´¯)¸.•’´¯)¸.•’´¯)
¤ª“˜¨¨¯¯¨¨˜“ª¤....¤ ª“˜¨

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2399
  • Gender: Male
    • www.zapsolution.com
Re: Common mistake with mouse coordinates and control ID
« Reply #4 on: March 14, 2009, 04:49:07 PM »
Whathever positive or negative, it is better to use short integer than word, as long as the value my be smaller than 32768.

With mouse coordinates, negative values are very common when using dual display, and when the second display is located to the left of the main one.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Jürgen Huhn

  • Full Member
  • ***
  • Posts: 105
  • Gender: Male
Re: Common mistake with mouse coordinates and control ID
« Reply #5 on: March 15, 2009, 06:46:22 PM »
 :)

Yes that`s true,
when using dual Display with second monitor located on the left Side and we`re moving i.e. a Window
from right primary Display to the left secondary Display, we have to use Integer!

Think, i`ve learned one more Lesson!!

It`s better i use short integer for this Cases in the Future...

Thank you very much!!

..
.¸.•’´¯)¸.•’´¯)¸.•’´¯)¸.•’´¯)
¤ª“˜¨¨¯¯¨¨˜“ª¤....¤ ª“˜¨

Offline Jürgen Huhn

  • Full Member
  • ***
  • Posts: 105
  • Gender: Male
Clear the reason for Common mistake with mouse coordinates and control ID
« Reply #6 on: March 22, 2009, 12:24:05 AM »
To clear and explain this Confusion, i have to say to the Hiword function of Powerbasic only..

The Hiword function is not up to Date and they extracted in older Versions the most significant
(high-order) Word from a Long-integer or Double-word (DWORD) value and return it
as an unsigned Word value.

HIWRD has been superceded by the "HI Function", although HIWRD remains supported for a limited period. 
Existing code should be converted to the new syntax as soon as possible.

The "Hi Function" extract the most significant (high-order) portion of an integer-class value.

e.g.

result = HI(type, value)

The value returned by HI is unsigned if type is BYTE, WORD, or DWORD, and signed if
type is INTEGER or LONG. 
value may be up to twice the size of the data type specified by type. 
In the following example, n may be up to a 16-bit value (twice the size of a BYTE):

e.g.

b = HI(BYTE,n)

HI replaces HIBYT, HIWRD, and HIINT!!! 

Note that those functions may not be supported in future versions of PowerBASIC,
so update your code to use the new syntax.
And also by LO: 
LO replaces LOBYT, LOWRD, and LOINT
That`s it!!
 ;)
« Last Edit: March 22, 2009, 12:45:38 AM by Jürgen Huhn »
.¸.•’´¯)¸.•’´¯)¸.•’´¯)¸.•’´¯)
¤ª“˜¨¨¯¯¨¨˜“ª¤....¤ ª“˜¨

Offline Jürgen Huhn

  • Full Member
  • ***
  • Posts: 105
  • Gender: Male
Re: Common mistake with mouse coordinates and control ID
« Reply #7 on: March 22, 2009, 01:28:15 AM »
Forgot to say:

I`ve only cleared the difference of the Syntax:

HI(WORD, lParam) 

and the old

HiWrd(lParam)

The statements of Jose Roca and Patrice Terrier are very Important.
You must also use the same LO(INTEGER, wParam) by using a negative ID for a specific control
to parse the %WM_COMMAND and retrieve the correct ID.
LO(WORD) return always a positive number!!
.¸.•’´¯)¸.•’´¯)¸.•’´¯)¸.•’´¯)
¤ª“˜¨¨¯¯¨¨˜“ª¤....¤ ª“˜¨