Windows client area / region

Just starting out? Need help? Post your questions and find answers here.
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Windows client area / region

Post by syntax error »

How do you retrieve the inner coordinates of a client area (region) in a window?

I've had a look at GetSystemMetrics() but there are lots of similar parameters like #SM_CYSIZE, #SM_CYBORDER, #SM_CYEDGE

I've also looked at GetWindowDC()

Any ideas?
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

GetClientRect_( hWnd, R )

R=RECT
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

...,

Should :

Code: Select all

        GetClientRect_(hWnd, @ClientRect.RECT)
        Debug Str(ClientRect\Left) + " " + Str(ClientRect\Top) + " " + Str(ClientRect\Right) + " " + Str(ClientRect\Bottom)
... help.

You just need the handle of the window you want to examine by using either WindowID() or GadgetID(#Gadget) ....


Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post by syntax error »

Using GetClientRect() always returns 0 for Left/Top.
Quote from Win32 prgrammers reference:
Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0)
What Im after is the inner top/left offset of the region area
Have a look at this:

Image
Since there are hundreds of themes and settings for windows all sorts of sizes will affect my app.
For example, the titlebar, menubar sizes.

I'm drawing to an Imagegadget using WindowMousex()/WindowMouseY() and need to know when the mouse is inside the gadget.

See this code example

Code: Select all

; canvas test

; LMB=Draw Brush
; RMB=Clear Screen

#gX=05 ; X offset for gadgets in window
#gY=24 ; Y offset for gadgets in window (add 20 if menus used)

#canX=20  ; canvas position/size
#canY=20
#canW=420
#canH=380

Global canvas,mouseincanvas,x,y,igad,clscolor
Declare DrawBrush(can,xpos,ypos)
Declare ClsCanvas(can,r,g,b)

canvas=CreateImage(#pb_any,#canW,#canH)
brush=CreateImage(#pb_any,32,32)
UseImage(brush)
StartDrawing(ImageOutput())
Circle(16,16,14,RGB(100,255,80))
StopDrawing()

; gui
win=OpenWindow(#pb_any, 140,200,#canW+46, #canH+66, #PB_Window_SystemMenu | #PB_Window_TitleBar , "Canvas")
If CreateGadgetList(WindowID(win))
  igad=ImageGadget(#pb_any, #canX, #canY, 0, 0, UseImage(canvas),#PB_Image_Border)
EndIf
sb=CreateStatusBar(#pb_any,WindowID(win))

quit=0
clscolor=0

Repeat
  ev=WaitWindowEvent()
;;; Debug ev
    
  x=WindowMouseX() : y=WindowMouseY()
  ; check if mouse x/y is inside canvas gadget
  If (x>=#canX+#gX And x<#canX+#canW+#gX) & (y>=#canY+#gY And y<#canY+#canH+#gY)
    mouseincanvas=1
    StatusBarText(sb, 0, "Coords: "+Str(x)+","+Str(y))
  Else
    mouseincanvas=0
    StatusBarText(sb, 0, "")
  EndIf

  Select ev
    Case 513 ; LMB pressed
      If mouseincanvas
        drawmode=1 : DrawBrush(canvas,x,y)
      EndIf
    Case 514 ; LMB released
      drawmode=0 
    Case 512 ; mouse moving
      If drawmode=1
        DrawBrush(canvas,x,y)
      EndIf
    Case 516 ; RMB Pressed
      ClsCanvas(canvas,Random(255),Random(255),Random(255))
    Case #PB_EventCloseWindow
      quit=1
    Case 258 ; Key pressed
      quit=1
  EndSelect
Until quit=1

End

Procedure DrawBrush(can,xpos,ypos)
    If mouseincanvas=1
      UseImage(can)
      StartDrawing(ImageOutput())
      Circle(xpos-#canX-#gX,ypos-#canX-#gY,4,RGB(100,255,80))
      StopDrawing()
      SetGadgetState(igad,UseImage(can))
     EndIf
EndProcedure

Procedure ClsCanvas(can,r,g,b)
  Protected bkgcolor
  bkgcolor=RGB(r,g,b)
  UseImage(can)
  StartDrawing(ImageOutput())
  Box(0,0,#canW,#canH,bkgcolor) ; CLS
  StopDrawing()
  SetGadgetState(igad,UseImage(can))
EndProcedure
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hi, look at this :

Code: Select all

; ReportWindowSize()

Procedure CallBack(hWnd,Msg,wParam,lParam)
  Result = #PB_ProcessPureBasicEvents
  If Msg=#WM_MOVE Or Msg=#WM_SIZE
      ResizeGadget(0, -1, -1, WindowWidth()-10, WindowHeight()-10)
      SetGadgetItemText(0, 0, Str(WindowX()), 1)
      SetGadgetItemText(0, 1, Str(WindowY()), 1)
      SetGadgetItemText(0, 2, Str(WindowWidth()), 1)
      SetGadgetItemText(0, 3, Str(WindowHeight()), 1)
      MyRect.Rect
      GetWindowRect_(WindowID(), @MyRect)
      SetGadgetItemText(0, 4, Str(MyRect\Left), 1)
      SetGadgetItemText(0, 5, Str(MyRect\Top), 1)
      SetGadgetItemText(0, 6, Str(MyRect\Right), 1)
      SetGadgetItemText(0, 7, Str(MyRect\Bottom), 1)
      SetGadgetItemText(0, 8, Str(MyRect\Right - MyRect\Left), 1)
      SetGadgetItemText(0, 9, Str(MyRect\Bottom - MyRect\Top), 1)
      MyPoint.Point
      ClientToScreen_(WindowID(), @MyPoint)
      SetGadgetItemText(0, 10, Str(MyPoint\X), 1)
      SetGadgetItemText(0, 11, Str(MyPoint\Y), 1)
      SetGadgetItemText(0, 12, Str(MyPoint\X - MyRect\Left), 1)
      SetGadgetItemText(0, 13, Str(MyPoint\Y - MyRect\Top), 1)
  Endif
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 300 - 1, 300 - 1, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget, "ReportWindowSize")
  CreateGadgetList(WindowID())
  ListIconGadget(0, 5, 5, 0, 0, "Attributes", 115, #PB_ListIcon_GridLines)
  AddGadgetColumn(0, 1, "Value", 50)
  AddGadgetColumn(0, 2, "API", 120)
  #Eol = Chr(10)
  AddGadgetItem(0, -1, "WindowX()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowY()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowWidth()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowHeight()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "Rect\Left" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Top" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Right" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Bottom" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect Width" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect Height" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Client X" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Client Y" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Border X" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Title Height" + #Eol + #Eol + "ClientToScreen_()")
  SetWindowCallback(@CallBack())
  CallBack(WindowID(),#WM_SIZE,0,0) ; added to force refresh
  Repeat : Until WaitWindowEvent()=#WM_CLOSE
EndIf
Last edited by Flype on Mon May 03, 2004 2:48 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

and particularly "Border X" and "TitleHeight" attributes for your application :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Flype,

Just we have to move / size the window before to get results. But the tip is good ...

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

yes i know fweil. i've added one line in my example :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post by syntax error »

What about this structure?

Code: Select all

Structure WINDOWPLACEMENT
  Length.l
  flags.l
  showCmd.l
  ptMinPosition.POINT
  ptMaxPosition.POINT
  rcNormalPosition.RECT
EndStructure
I found it in the Structure Viewer.
How do I access the 'PB window' structure though?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hi, you can access the WINDOWPLACEMENT structure through the API function GetWindowPlacement_(WindowID(0),MyPlacement.WINDOWPLACEMENT)

Code: Select all

Procedure CallBack(hWnd, Msg, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  If Msg = #WM_MOVE Or Msg = #WM_SIZE
    WW = WindowWidth()
    WH = WindowHeight()
    ResizeGadget(0, -1, -1, WW - 10, WH - 10)
    SetColumnWidth(0, 1, WW - 305)
    SetGadgetItemText(0, 0, Str(WindowX()), 1)
    SetGadgetItemText(0, 1, Str(WindowY()), 1)
    SetGadgetItemText(0, 2, Str(WW), 1)
    SetGadgetItemText(0, 3, Str(WH), 1)
    MyRect.RECT
    GetWindowRect_(WindowID(), @MyRect)
    SetGadgetItemText(0, 4, Str(MyRect\Left), 1)
    SetGadgetItemText(0, 5, Str(MyRect\Top), 1)
    SetGadgetItemText(0, 6, Str(MyRect\Right), 1)
    SetGadgetItemText(0, 7, Str(MyRect\Bottom), 1)
    SetGadgetItemText(0, 8, Str(MyRect\Right - MyRect\Left), 1)
    SetGadgetItemText(0, 9, Str(MyRect\Bottom - MyRect\Top), 1)
    MyPoint.POINT
    ClientToScreen_(WindowID(), @MyPoint)
    SetGadgetItemText(0, 10, Str(MyPoint\X), 1)
    SetGadgetItemText(0, 11, Str(MyPoint\Y), 1)
    SetGadgetItemText(0, 12, Str(MyPoint\X - MyRect\Left), 1)
    SetGadgetItemText(0, 13, Str(MyPoint\Y - MyRect\Top), 1)
    MyPlacement.WINDOWPLACEMENT
    GetWindowPlacement_(WindowID(), @MyPlacement)
    SetGadgetItemText(0, 14, Str(MyPlacement\length), 1)
    SetGadgetItemText(0, 15, Str(MyPlacement\flags), 1)
    SetGadgetItemText(0, 16, Str(MyPlacement\showCmd), 1)
    SetGadgetItemText(0, 17, Str(MyPlacement\ptMinPosition\X) + " " + Str(MyPlacement\ptMinPosition\Y), 1)
    SetGadgetItemText(0, 18, Str(MyPlacement\ptMaxPosition\X) + " " + Str(MyPlacement\ptMinPosition\Y), 1)
    SetGadgetItemText(0, 19, Str(MyPlacement\rcNormalPosition\Left), 1)
    SetGadgetItemText(0, 20, Str(MyPlacement\rcNormalPosition\Top), 1)
    SetGadgetItemText(0, 21, Str(MyPlacement\rcNormalPosition\Right), 1)
    SetGadgetItemText(0, 22, Str(MyPlacement\rcNormalPosition\Bottom), 1)
  EndIf
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 350, 400, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget, "Window Sizes")
  CreateGadgetList(WindowID())
  ListIconGadget(0, 5, 5, 0, 0, "Attributes", 140, #PB_ListIcon_GridLines)
  AddGadgetColumn(0, 1, "Value", 51)
  AddGadgetColumn(0, 2, "API", 150)
  #Eol = Chr(10)
  AddGadgetItem(0, -1, "WindowX()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowY()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowWidth()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "WindowHeight()" + #Eol + #Eol + "Purebasic")
  AddGadgetItem(0, -1, "Rect\Left" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Top" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Right" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect\Bottom" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect Width" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Rect Height" + #Eol + #Eol + "GetWindowRect_()")
  AddGadgetItem(0, -1, "Client X" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Client Y" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Border X" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "Title Height" + #Eol + #Eol + "ClientToScreen_()")
  AddGadgetItem(0, -1, "length" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "flags" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "showCmd" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "ptMinPosition" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "ptMaxPosition" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "rcNormalPosition\Left" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "rcNormalPosition\Top" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "rcNormalPosition\Right" + #Eol + #Eol + "GetWindowPlacement_()")
  AddGadgetItem(0, -1, "rcNormalPosition\Bottom" + #Eol + #Eol + "GetWindowPlacement_()")
  SetWindowCallback(@CallBack())
  CallBack(WindowID(), #WM_SIZE, 0, 0)
  Repeat : Until WaitWindowEvent() = #WM_CLOSE
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post by syntax error »

Flype,

What can I say apart from thanks x 100
:!: Superb help :!:
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

thanx :wink:

when i could help...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply