Page 1 of 1

PtInRect Parameters

Posted: Fri Feb 24, 2012 10:06 am
by TI-994A
This code, by netmaestro, posted in an earlier thread, demonstrated the use of the PtInRect API:

Code: Select all

OpenWindow(0,0,0,320,240,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ListIconGadget(0,0,0,320,240,"Name",160) 
AddGadgetColumn(0,1,"Type",80) 
AddGadgetColumn(0,1,"Size",60) 
header = SendMessage_(GadgetID(0),#LVM_GETHEADER,0,0) 

Repeat 
  ev = WaitWindowEvent(1) 
  GetWindowRect_(header, @wr.rect) 
  If PtInRect_(wr, DesktopMouseX(),DesktopMouseY()) 
    ShowWindow_(header, #SW_HIDE) 
    dc = GetDC_(GadgetID(0)) 
    TextOut_(dc, 100,0, PeekS(?begin), 15) 
    ReleaseDC_(GadgetID(0),dc) 
  Else 
    ShowWindow_(header, #SW_SHOW) 
  EndIf 
Until ev = #WM_CLOSE 

DataSection
  begin:
  Data.q 7453301692552738114,9400190730641523
In the discussion, it was said that the PtInRect API required 3 parameters, the RECT, and the X & Y points. However, the code doesn't work, yielding to an error of [PtInRect_(): Incorrect number of parameters]. Further searching on the forum resulted in an alternative solution which works, where the X & Y points have somehow been merged:

Code: Select all

Define click.Point
OpenWindow(0,0,0,320,240,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ListIconGadget(0,0,0,320,240,"Name",160) 
AddGadgetColumn(0,1,"Type",80) 
AddGadgetColumn(0,1,"Size",60) 
header = SendMessage_(GadgetID(0),#LVM_GETHEADER,0,0) 
 
Repeat 
  ev = WaitWindowEvent(1) 
  GetWindowRect_(header, @wr.rect) 
  click\x = DesktopMouseX()
  click\y = DesktopMouseY()
  If PtInRect_(wr, click\y<<32+click\x) 
    ShowWindow_(header, #SW_HIDE) 
    dc = GetDC_(GadgetID(0)) 
    TextOut_(dc, 100,0, PeekS(?begin), 16) 
    ReleaseDC_(GadgetID(0),dc) 
  Else 
    ShowWindow_(header, #SW_SHOW) 
  EndIf 
Until ev = #WM_CLOSE 

DataSection
  begin:
  Data.q 7598523934101894480,2410381742072537187
Is the earlier declaration with 3 parameters no longer supported by PureBasic? And what does this equation (click\y<<32+click\x) actually do?

Thank you.

Re: PtInRect Parameters

Posted: Fri Feb 24, 2012 12:47 pm
by happer66
I might be wrong here but the PtInRect API always had 2 params, PB allowed the point structure to be specified individually and then it merged them into a point behind the scenes (as it did with other API calls that used point). This was later changed so it corresponded with MSDN. But replacing the x/y with a point structure doesn't seem to work at all(?).

I don't use much bit shifting myself (<<, >> etc.) so I'm not going to try and explain that, I'll leave that to someone else...

I use the PtInRect in one application and to make it easier to understand, imo, I passed the two consecutive longs as a quad instead:

Code: Select all

Define click.Point
OpenWindow(0,0,0,320,240,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0,0,0,320,240,"Name",160)
AddGadgetColumn(0,1,"Type",80)
AddGadgetColumn(0,1,"Size",60)
header = SendMessage_(GadgetID(0),#LVM_GETHEADER,0,0)
 
Repeat
  ev = WaitWindowEvent(1)
  GetWindowRect_(header, @wr.rect)
  click\x = DesktopMouseX()
  click\y = DesktopMouseY()
  If PtInRect_(wr, PeekQ(click)) ;click\y<<32+click\x)
    ShowWindow_(header, #SW_HIDE)
    dc = GetDC_(GadgetID(0))
    TextOut_(dc, 100,0, PeekS(?begin), 16)
    ReleaseDC_(GadgetID(0),dc)
  Else
    ShowWindow_(header, #SW_SHOW)
  EndIf
Until ev = #WM_CLOSE

DataSection
  begin:
  Data.q 7598523934101894480,2410381742072537187

Re: PtInRect Parameters

Posted: Fri Feb 24, 2012 1:39 pm
by luis
TI-994A wrote:Is the earlier declaration with 3 parameters no longer supported by PureBasic? And what does this equation (click\y<<32+click\x) actually do?

Thank you.

The 3 params are not (no longer ?) supported.
PtInRect_ api requires two params, a pointer and structure passed by value.

The structure is:

Code: Select all

  
Structure POINT
      x.l
      y.l
EndStructure

PB cannot pass structures by val, only by reference (address of) so the (click\y<<32+click\x) expression takes the two long values, shift the first one to the left by 32 bits (a full long length) and add the second one to the lowest 32 bits. The result is you have the two long values one after another in memory, exactly like the structure above, and since the result is a number and not a structure you can pass it by value as required by the api.
The api, on its end, simple decompose the the big number in the two 32 bits long and everyone is more or less happy.

Re: PtInRect Parameters

Posted: Fri Feb 24, 2012 3:24 pm
by TI-994A
happer66 wrote:...This was later changed so it corresponded with MSDN. But replacing the x/y with a point structure doesn't seem to work at all(?).

I use the PtInRect in one application and to make it easier to understand, imo, I passed the two consecutive longs as a quad instead:

Code: Select all

PtInRect_(wr, PeekQ(click))
That's quite a neat trick, using PeekQ like that. And you're right about the point structure; that's a PureBasic limitation, as explained by luis here.
luis wrote:PB cannot pass structures by val, only by reference (address of) so the (click\y<<32+click\x) expression takes the two long values, shift the first one to the left by 32 bits (a full long length) and add the second one to the lowest 32 bits. The result is you have the two long values one after another in memory, exactly like the structure above, and since the result is a number and not a structure you can pass it by value as required by the api. The api, on its end, simple decompose the the big number in the two 32 bits long and everyone is more or less happy.
Thanks for clearing that up, especially about the bit shifting. This is probably what Timo was referring to when he said in an earlier thread:
freak wrote:...For it to work there you have to put the whole POINT structure into a quad and pass that as a single parameter. It will work on both x86 and x64 if you use the quad way and a prototype instead of CallFunctionFast() (it does not support quads).
srod wrote:

Code: Select all

PtInRect_(rc,pt\y<<32+pt\x)
Works with PB x86 and x64.
Thanks for all your help and answers, and thanks to happer66 and srod for the solutions.