=====Limiting the size of a window=====
//by Richard Russell, October 2007//\\ \\ Normally windows are either fully resizable by the user (by dragging a corner or edge), which is the default, or are a fixed size (see the Help documentation under [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#fixwindowsize|Fixing the window size]]). However, occasionally you may want to allow the user to resize the window, but only within certain limits. You can do that by handling the **WM_SIZING** Windows message, which you can do using the **SUBCLASS** library.\\ \\ The code below illustrates how to restrict the maximum size of the window to 800 x 600 pixels. Firstly some initialisation:
INSTALL @lib$+"SUBCLASS"
INSTALL @lib$+"NOWAIT"
MAXW% = 800
MAXH% = 600
WM_SIZING = 532
PROC_subclass(WM_SIZING, FN_wmsizing())
ON ERROR PROC_unsubclass : SYS "MessageBox", @hwnd%, REPORT$, 0, 48 : QUIT
You should ensure that you are using version 1.3 or later of **SUBCLASS.BBC**.\\ \\ Now the program can perform whatever task it is designed for; for the purposes of the example this is just an empty loop:
PRINT "Window size is limited to ";MAXW%;" by ";MAXH%
REPEAT
PROCwait(1)
UNTIL FALSE
END
Note that while subclassing is in effect you must avoid statements which may take a long time to execute, such as **GET**, **INKEY**, **INPUT** and **WAIT** (WAIT should be avoided even if the delay is short). You should also be careful not to use **SOUND** when the sound queue is already full. You can find suitable replacements for these functions in the [[/Libraries|NOWAIT library]].\\ \\ Finally here is the routine for handling the **WM_SIZING** messages:
DEF FN_wmsizing(msg%, side%, lprc%)
LOCAL rc{}
DIM rc{l%,t%,r%,b%}
!(^rc{}+4) = lprc%
IF rc.r%-rc.l% > MAXW% THEN
CASE side% OF
WHEN 1,4,7: rc.l% = rc.r%-MAXW%
WHEN 2,5,8: rc.r% = rc.l%+MAXW%
ENDCASE
ENDIF
IF rc.b%-rc.t% > MAXH% THEN
CASE side% OF
WHEN 3,4,5: rc.t% = rc.b%-MAXH%
WHEN 6,7,8: rc.b% = rc.t%+MAXH%
ENDCASE
ENDIF
= 1
If you want to limit the //minimum// size of the window instead the //maximum// size the routine would be:
DEF FN_wmsizing(msg%, side%, lprc%)
LOCAL rc{}
DIM rc{l%,t%,r%,b%}
!(^rc{}+4) = lprc%
IF rc.r%-rc.l% < MINW% THEN
CASE side% OF
WHEN 1,4,7: rc.l% = rc.r%-MINW%
WHEN 2,5,8: rc.r% = rc.l%+MINW%
ENDCASE
ENDIF
IF rc.b%-rc.t% < MINH% THEN
CASE side% OF
WHEN 3,4,5: rc.t% = rc.b%-MINH%
WHEN 6,7,8: rc.b% = rc.t%+MINH%
ENDCASE
ENDIF
= 1
I'm sure you can see how to limit both the minimum and maximum sizes, if desired.