May 09, 2024, 07:46:50 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Set window size based on client area

Started by Parker, October 13, 2007, 11:12:12 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Parker

October 13, 2007, 11:12:12 PM Last Edit: October 13, 2007, 11:43:20 PM by Parker
I used to want this feature for IBasic and I could never figure it out, but I guess I didn't know about MSDN back then... anyway, sometimes you just know what client area you want, and you need to figure out what the full area should be in order to get that client area.

I'm not providing the declarations or type definitions here because it's a quick conversion from another language. Most of them are in windows.inc anyway, I think. It's untested in EB, but the concepts still apply.

sub SetClientSize( win:window, l as int, t as int, w as int, h as int )
dim flags as uint : flags = 0
if ( l = -1 ) or ( t = -1 ) then flags |= SWP_NOMOVE
if ( w = -1 ) or ( h = -1 ) then flags |= SWP_NOSIZE

dim wi as WINDOWINFO
wi.cbSize = len( WINDOWINFO )
GetWindowInfo( win.hwnd, wi )

dim hasMenu as int : hasMenu = 0
if GetMenu( win.hwnd ) <> NULL then hasMenu = 1
if GetParent( win.hwnd ) = NULL then hasMenu = 0

dim rc as RECT
rc.left = l
rc.top = t
rc.right = l + w
rc.bottom = t + h
AdjustWindowRectEx( rc, wi.dwStyle, hasMenu, wi.dwExStyle )

dim scrollx, scrolly as int : scrollx = 0 : scrolly = 0
if wi.dwStyle & WS_VSCROLL then scrollx = GetSystemMetrics( SM_CXVSCROLL )
if wi.dwStyle & WS_HSCROLL then scrolly = GetSystemMetrics( SM_CYHSCROLL )

SetWindowPos( win.hwnd, 0, l, t, rc.right - rc.left + scrollx, rc.bottom - rc.top + scrolly, flags )
end sub


Edit - changed to account for scroll bars in the window.

Ionic Wind Support Team

I've mentioned AdjustWindowRectEx before.  The problem with the function is it is old, and Microsoft never updated it to account for things like scrollbars, themes (XP) and changes in certain fonts.  It also won't work if you have ownerdrawn menus of course.

Windows is designed around the total size of a window, and not it's client size.  However there are ways to accomplish what you want without using AdjustWindowRectEx.

The method I have been using lately is to open a window, hidden, and get both the window size and client size using GETSIZE and GETCLIENTSIZE.   A simple subtraction can give you height of the "title area" + borders.   

Here is a simple example:


CONST WS_VISIBLE = 0x10000000

window sized

'assume we want a 300x300 window
int width,height:width=300:height=300
int l,t,w,h
int cl,ct,cw,ch

'open the window, @IDCREATE makes sure it is hidden
OPENWINDOW sized,100,100,width,height,@CAPTION|@HSCROLL|@VSCROLL|@SYSMENU,NULL,"Test Window",&sized_handler
'add a menu
BEGINMENU sized
MENUTITLE "file"
MENUITEM "Quit",0,1
ENDMENU

'resize the window
GETSIZE sized,l,t,w,h
GETCLIENTSIZE sized,cl,ct,cw,ch
SETSIZE sized, l,t,w-cw+width,h-ch+height
'Show the window
SHOWWINDOW sized,@SWRESTORE
'draw to show we are at desired size
RECT sized,0,0,width,height,RGB(255,0,0)

WAITUNTIL sized.hwnd = NULL
END

SUB sized_handler
SELECT @MESSAGE
CASE @IDCREATE
'hide the window
ModifyStyle #<WINDOW>@HITWINDOW,0,WS_VISIBLE
CASE @IDCLOSEWINDOW
CLOSEWINDOW sized
ENDSELECT
ENDSUB


Have fun!
Paul.
Ionic Wind Support Team

Parker

I didn't know that about AdjustWindowRectEx. Thanks for the other (better) method.