understanding window sizes
Posted: Sat Jun 12, 2004 12:06 pm
Code updated for 5.20+ (same information obtained with WidowWidth(), WindowHeight(), MenuHeight(), ToolBarHeight(), StatusBarHeight())
well, i was messing myself up, so i had to clarify this once and for all, if only for myself...
this should also be a lot of help to any beginner!
i would appreciate any commands and corrections...
well, i was messing myself up, so i had to clarify this once and for all, if only for myself...
this should also be a lot of help to any beginner!
i would appreciate any commands and corrections...
Code: Select all
; purebasic survival guide - pb3.90 sp1
; windows_1.pb - 12.06.2004 ejn (blueznl)
; http://www.xs4all.nl/~bluez/datatalk/purebasic.htm
;
; - information on screen and dows
;
;
; *** constants
;
; haven't found a proper function or specification for these, so i'll have to do with assumptions
;
#buttonbarheight = 24
#statusbarheight = 20
;
;
;
; *** first open a window without menus, bars. etc.
;
; w_x and w_y are screen coordinates, the identify the left upper corner of the window
; w_w and w_h specify the size of the client area of the widow
;
w_x = 300 ; top left coord of the exterior
w_y = 200
w_w = 300 ; as passed on to openwindow, this specifies the interior size!
w_h = 200
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"basic window",#PB_Window_SystemMenu)
;
; the coordinates / dimensions of the interior
;
i_x = 0
i_y = 0
i_w = w_w
i_h = w_h
;
ListViewGadget(0,i_x,i_y,i_w,i_h)
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** the same thing, but now we add a menu
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"with a menu",#PB_Window_SystemMenu)
;
CreateMenu(0,main_h)
MenuTitle("Menu")
MenuItem(1,"Entry")
;
; with a menu added, the available height decreases and the reference point (0,0) of the interior moves
; menuheight() returns the size of the menu, wich is equal to the decrease in interior space
;
i_x = 0
i_y = 0
i_w = w_w
i_h = w_h - MenuHeight() ; so minus the menu bar height
;
ListViewGadget(0,i_x,i_y,i_w,i_h)
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** again, now with a menu and buttonbar
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"and a buttonbar",#PB_Window_SystemMenu)
;
CreateMenu(0,main_h)
MenuTitle("Menu")
MenuItem(1,"Entry")
;
CreateToolBar(0,main_h)
ToolBarStandardButton(1,#PB_ToolBarIcon_New)
;
; purebasic isn't always that consistent, adding a toolbar will not change the reference point, so
; we have to take that into account ourselves, the buttonbar size is hardcoded to #buttonbarheight pixels
; with the icons themselves being 16x16 pixels
;
i_x = 0
i_y = 0 + #buttonbarheight ; add the size of the button bar
i_w = w_w
i_h = w_h - MenuHeight() - #buttonbarheight ; and subtract the size of the menu and the buttonbar
;
ListViewGadget(0,i_x,i_y,i_w,i_h)
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** one more time, adding a statusbar
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"plus a statusbar",#PB_Window_SystemMenu)
;
CreateMenu(0,main_h)
MenuTitle("Menu")
MenuItem(1,"Entry")
;
CreateToolBar(0,main_h)
ToolBarStandardButton(1,#PB_ToolBarIcon_New)
;
CreateStatusBar(0,main_h)
;
; a statusbar appears to be always #statusbarheight pixels high
;
i_x = 0
i_y = 0 + #buttonbarheight ; add the size of the button bar
i_w = w_w
i_h = w_h - MenuHeight() - #buttonbarheight - #statusbarheight ; and subtract the size of menu, button, and statusbar
;
ListViewGadget(0,i_x,i_y,i_w,i_h)
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** mouse coordinates
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"mouse coordinates",#PB_Window_SystemMenu)
;
CreateMenu(0,main_h)
MenuTitle("Menu")
MenuItem(1,"Entry")
;
CreateToolBar(0,main_h)
ToolBarStandardButton(1,#PB_ToolBarIcon_New)
;
; being the inconsistent beast purebasic is, the mouse function Menuesn't return client coordinates, but
; instead returns the mouse position relative to the left upper corner of the window (!)
;
i_x = 0
i_y = 0+#buttonbarheight ; add the size of the button bar
i_w = w_w
i_h = w_h - MenuHeight() - #buttonbarheight ; and subtract the size of the menu and the buttonbar
;
c_w = (i_w-4)/3 ; column width fir 3 columns evenly spaced
ListIconGadget(0,i_x,i_y,i_w,i_h,"window",c_w)
AddGadgetColumn(0,1,"screen",c_w)
AddGadgetColumn(0,2,"client",c_w)
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"")
;
Repeat
m_xw = WindowMouseX(0) ; these report the coordinates relative to upper left of dow
m_yw = WindowMouseY(0)
;
p.POINT ; to obtain the proper coordinates we have to use winapi calls
GetCursorPos_(@p)
m_xs = p\x ; screen coordinates of mouse pointer
m_ys = p\y
ScreenToClient_(main_h,@p)
m_x = p\x ; finally, the real client coordinates
m_y = p\y
;
If m_x <> m_x_old Or m_y <> m_y_old
SetGadgetItemText(0,0,"x "+Str(m_xw),0)
SetGadgetItemText(0,1,"y "+Str(m_yw),0)
SetGadgetItemText(0,0,"x "+Str(m_xs),1)
SetGadgetItemText(0,1,"y "+Str(m_ys),1)
SetGadgetItemText(0,0,"x "+Str(m_x),2)
SetGadgetItemText(0,1,"y "+Str(m_y),2)
m_x_old = m_x ; just so it only displays changes
m_y_old = m_y
EndIf
;
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** more information
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"more info",#PB_Window_SystemMenu)
;
; the coordinates / dimensions of the interior
;
i_x = 0
i_y = 0
i_w = w_w
i_h = w_h - #statusbarheight
;
CreateStatusBar(0,main_h)
ListViewGadget(0,i_x,i_y,i_w,i_h)
;
; using winapi we can retrieve a lot of information
; screensize...
;
s_w = GetSystemMetrics_(#SM_CXSCREEN)
s_h = GetSystemMetrics_(#SM_CYSCREEN)
;
AddGadgetItem(0,-1,"screen size")
AddGadgetItem(0,-1,"w "+Str(s_w))
AddGadgetItem(0,-1,"h "+Str(s_h))
AddGadgetItem(0,-1,"")
;
; available working area on the primary monitor
;
r.RECT
SystemParametersInfo_(#SPI_GETWORKAREA,0,@r,0)
wa_x = r\left
wa_y = r\top
wa_w = r\right - r\left
wa_h = r\bottom - r\top
;
AddGadgetItem(0,-1,"work area")
AddGadgetItem(0,-1,"x "+Str(wa_x))
AddGadgetItem(0,-1,"y "+Str(wa_y))
AddGadgetItem(0,-1,"w "+Str(wa_w))
AddGadgetItem(0,-1,"h "+Str(wa_h))
AddGadgetItem(0,-1,"")
;
; dimensions of a window border
; (note that sizes of resizable and non resizable windows are different)
;
b_x = GetSystemMetrics_(#SM_CXDLGFRAME) ; non-resizable window
b_y = GetSystemMetrics_(#SM_CXDLGFRAME)
;
AddGadgetItem(0,-1,"window border (sizable)")
AddGadgetItem(0,-1,"x "+Str(b_x))
AddGadgetItem(0,-1,"y "+Str(b_y))
AddGadgetItem(0,-1,"")
;
; dragbar or captionbar
; (note that there is a smaller style as well)
;
ca_h = GetSystemMetrics_(#SM_CYCAPTION)
;
AddGadgetItem(0,-1,"caption (dragbar)")
AddGadgetItem(0,-1,"h "+Str(ca_h))
AddGadgetItem(0,-1,"")
;
AddGadgetItem(0,-1,"window parameters")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
AddGadgetItem(0,-1,"")
;
; using the above we can calculate the windows external dimensions
;
e_x = w_x
e_y = w_y
e_w = i_w + 2*b_x
e_h = i_h + 2*b_y + ca_h
;
AddGadgetItem(0,-1,"exterior")
AddGadgetItem(0,-1,"x "+Str(e_x))
AddGadgetItem(0,-1,"y "+Str(e_y))
AddGadgetItem(0,-1,"w "+Str(e_w))
AddGadgetItem(0,-1,"h "+Str(e_h))
AddGadgetItem(0,-1,"")
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** resize to full screen
;
; ok, let's try our knowledge and resize the widow to full screen, just fitting, with borders visible
; in the previous section we figured out the available space on the desktop on the primary screen (workarea)
;
w_x = wa_x
w_y = wa_y
w_w = wa_w - 2*b_x
w_h = wa_h - 2*b_y - ca_h
;
i_x = 0
i_y = 0
i_w = w_w
i_h = w_h - #statusbarheight
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"maximized to full screen",#PB_Window_SystemMenu)
ListViewGadget(0,i_x,i_y,i_w,i_h)
CreateStatusBar(0,main_h)
;
AddGadgetItem(0,-1,"screen size")
AddGadgetItem(0,-1,"w "+Str(s_w))
AddGadgetItem(0,-1,"h "+Str(s_h))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"work area")
AddGadgetItem(0,-1,"x "+Str(wa_x))
AddGadgetItem(0,-1,"y "+Str(wa_y))
AddGadgetItem(0,-1,"w "+Str(wa_w))
AddGadgetItem(0,-1,"h "+Str(wa_h))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"window border (not sizable)")
AddGadgetItem(0,-1,"x "+Str(b_x))
AddGadgetItem(0,-1,"y "+Str(b_y))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"caption (dragbar)")
AddGadgetItem(0,-1,"h "+Str(ca_h))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"window parameters")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
AddGadgetItem(0,-1,"")
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
AddGadgetItem(0,-1,"")
;
e_x = w_x
e_y = w_y
e_w = i_w + 2*b_x
e_h = i_h + 2*b_y + ca_h
;
AddGadgetItem(0,-1,"exterior")
AddGadgetItem(0,-1,"x "+Str(e_x))
AddGadgetItem(0,-1,"y "+Str(e_y))
AddGadgetItem(0,-1,"w "+Str(e_w))
AddGadgetItem(0,-1,"h "+Str(e_h))
AddGadgetItem(0,-1,"")
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
;
;
; *** a resizable window with an updated gadget
;
w_x = 300
w_y = 200
w_w = 300
w_h = 200
i_x = 0
i_y = 0
i_w = w_w
i_h = w_h - #statusbarheight
;
main_h = OpenWindow(0,w_x,w_y,w_w,w_h,"more info",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
CreateStatusBar(0,main_h)
ListViewGadget(0,i_x,i_y,i_w,i_h)
AddGadgetItem(0,-1,"interior")
AddGadgetItem(0,-1,"x "+Str(i_x))
AddGadgetItem(0,-1,"y "+Str(i_y))
AddGadgetItem(0,-1,"w "+Str(i_w))
AddGadgetItem(0,-1,"h "+Str(i_h))
;
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_SizeWindow
w_x = WindowX(0)
w_y = WindowY(0)
w_w = WindowWidth(0)
w_h = WindowHeight(0)
i_w = w_w
i_h = w_h - #statusbarheight
ResizeGadget(0,i_x,i_y,i_w,i_h)
SetGadgetItemText(0,1,"x "+Str(i_x),0)
SetGadgetItemText(0,2,"y "+Str(i_y),0)
SetGadgetItemText(0,3,"w "+Str(i_w),0)
SetGadgetItemText(0,4,"h "+Str(i_h),0)
EndSelect
Until event = #PB_Event_CloseWindow