• Welcome to Theos PowerBasic Museum 2017.

News:

Attachments are only available to registered users.
Please register using your full, real name.

Main Menu

Common mistake with mouse coordinates and control ID

Started by Patrice Terrier, March 12, 2009, 09:56:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

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

LOCAL p AS POINTAPI

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


when they should use this

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.

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

Jürgen Huhn

Hi Patrice!!

That`s very interisting!!

What do you say to this?

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

José Roca

 
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)).

Jürgen Huhn

#3
 :-*

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)

??
..

.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Patrice Terrier

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

Jürgen Huhn

 :)

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!!

..
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Jürgen Huhn

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!!
;)
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Jürgen Huhn

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!!
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨