Date: 02-16-2022
Return to Index
created by gbSnippets
'Allowing a user to change an application's layout, or to change the size of various
'sections of a layout is a very common feature of GUI applications. This snippet
'shows how it can be done and includes the following features.
'1. Horizontal and vertical splitter bars, displaying up to controls.
'2. 6 different layout options (some with 2 controls, others with 3 controls).
'3. Splitter bars can have fixed position or be positioned on a percentage basis
'Primary Code;
'The code sections/functions used in the compilable example below are:
'1. %WM_SetCursor - detects mouse clicks and movement
'2. Sub MoveBarUpDown - moves horizontal bar in response to mouse action
'3. Sub MoveBarLeftRight - moves vertical bar in response to mouse action
'4. Sub ResizeWindow - sets splitterbar location/size, and control visibility/location/size
'A mouse is used to drag/move either splitter bar, which resets the
'fixed/percentage position of the splitter bar. When the dialog is resized, the
'current splitter bar positions (fixed/percentage) are maintained. Label controls
'are used as the splitter bars - one for vertical splitting and one for horizontal splitting.
'Due to the length of this example, on a description will be covered in this section.
'See the compilable example below for the source code.
'Compilable Example: (Jose Includes)
'In this example, up to 6 layout options may be selected. The splitter bars (label controls)
'are colored red/blue for visibility, but an application typically uses colors which match the
'background color of the dialog in order to render the splitter bars invisible - except as the
'mouse cursor changes when it passes over the splitter bars.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Debug Error On 'catch array/pointer errors - OFF in production
#Debug Display On 'display untrapped errors - OFF in production
#Include "Win32api.inc"
#Resource "gbsnippets.pbr"
' Equates =============================================================
%IDC_Left = 500 : %IDC_Right = 501 : %IDC_Bottom = 502
%IDC_LabelH = 503 : %IDC_LabelV = 504 : %IDC_StatusBar = 505
%IDC_Toolbar = 506 : %IDM_Fixed = 507 : %IDT_New = 508 : %ID_ImageList = 509
%IDT_X = 701 : %IDT_Y = 702 : %IDT_Z = 703
%IDM_1 = 601 : %IDM_2 = 602 : %IDM_3 = 603
%IDM_4 = 604 : %IDM_5 = 605 : %IDM_6 = 606 : %IDM_Sep = 607
' Global Variables =============================================================
Global hDlg As DWord, hTree As DWord, hList As DWord, hMenu As DWord
Global SplitVInWork As Long, SplitHInWork As Long, hMenuOptions As DWord
Global XSplit As Single, YSplit As Single, hLst As DWord
Global HBarLeft As Long, VBarTop As Long, Layout&, FixedSplitterBar&
' Main Function =======================================================================
Function PBMain()
Dialog New Pixels, 0, "Splitter Text",300,300,410,340, %WS_OverlappedWindow Or %WS_ClipChildren To hDlg
Dialog Set Icon hDlg, "aainfo"
Control Add StatusBar, hDlg, %IDC_StatusBar, "",0,0,0,0 'xy, xxyy ignored, %ccs_bottom by default
Control Add Toolbar, hDlg, %IDC_Toolbar, "Toolbar",0,0,0,0 'xy, xxyy, txt$ ignored, %ccs_top by default
'create imagelist for Toolbar
ImageList New Icon 16,16,32,3 To hLst
ImageList Add Icon hLst, "x" '1
ImageList Add Icon hLst, "y" '2
ImageList Add Icon hLst, "z" '3
'attach imagelist
Toolbar Set ImageList hDlg, %IDC_Toolbar, hLst, 0
'create buttons
Toolbar Add Button hDlg, %IDC_Toolbar, 1, %IDT_X, %TbStyle_Check, "X"
Toolbar Add Button hDlg, %IDC_Toolbar, 2, %IDT_Y, %TbStyle_Check, "Y"
Toolbar Add Button hDlg, %IDC_Toolbar, 3, %IDT_Y, %TbStyle_Check, "Z"
'create menu
Menu New Bar To hMenu
Menu New Popup To hMenuOptions
Menu Add Popup, hMenu, "&Options", hMenuOptions, %MF_Enabled
Menu Add String, hMenuOptions, "1 Left Beside Right over Bottom, Bottom Left-to-Right", %IDM_1, %MF_Enabled
Menu Add String, hMenuOptions, "2 Left over Bottom - Right Top-to-Bottom", %IDM_2, %MF_Enabled
Menu Add String, hMenuOptions, "3 Left Beside Right, No Bottom", %IDM_3, %MF_Enabled
Menu Add String, hMenuOptions, "4 Left Beside Right under Bottom, Bottom Left-to-Right", %IDM_4, %MF_Enabled
Menu Add String, hMenuOptions, "5 Right over Bottom - Left Top-to-Bottom", %IDM_5, %MF_Enabled
Menu Add String, hMenuOptions, "6 Left over Right, No Bottom", %IDM_6, %MF_Enabled
Menu Add String, hMenuOptions, "-", %IDM_Sep, %MF_Enabled
Menu Add String, hMenuOptions, "Fixed Position Splitter Bars", %IDM_Fixed, %MF_Enabled
Menu Attach hMenu, hDlg
'add the controls + splitter bars (labels)
Control Add TextBox, hDlg, %IDC_Left, "TopLeft", 20, 80, 160, 80
Control Add TextBox, hDlg, %IDC_Bottom, "Bottom", 20, 200, 370, 90
Control Add TextBox, hDlg, %IDC_Right, "TopRight", 220, 55, 170, 100
Control Add Label, hDlg, %IDC_LabelH, "", 200, 45, 6, 125, %SS_Notify , %WS_Ex_ClientEdge ' up/down - does horizontal split
Control Add Label, hDlg, %IDC_LabelV, "", 0,170, 410, 6, %SS_Notify , %WS_Ex_ClientEdge ' left/right - does vertical split
Control Set Color hDlg, %IDC_LabelV, %Black, %Red
Control Set Color hDlg, %IDC_LabelH, %Black, %Blue
'initial values
XSplit = 0.5 : YSplit = 0.5 : HBarLeft = 100 : VBarTop = 100 : Layout& = 1
Dialog Show Modal hDlg Call DlgProc()
End Function
'monitor mouse movement=================================
CallBack Function DlgProc() As Long
Local iReturn As Long, x as Long, y as Long, w as Long, h as Long
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case %IDM_1 : Layout& = 1 : ResizeWindow
Case %IDM_2 : Layout& = 2 : ResizeWindow
Case %IDM_3 : Layout& = 3 : ResizeWindow
Case %IDM_4 : Layout& = 4 : ResizeWindow
Case %IDM_5 : Layout& = 5 : ResizeWindow
Case %IDM_6 : Layout& = 6 : ResizeWindow
Case %IDM_Fixed
FixedSplitterBar& = FixedSplitterBar& XOR 1 'flip between 0 and 1
Menu Set State hMenuOptions, ByCmd %IDM_Fixed, FixedSplitterBar& * 8
Dialog Get Client hDlg To w,h
'adjusts VBarTop/HBarLeft or xSplit/ySplit when option is changed
'both are not kept current at all times - only the pair in use
If FixedSplitterBar& Then
VBarTop = h - ySplit * h
HBarLeft = xSplit * w
Else
ySplit = (h-VBarTop) / h
xSplit = HBarLeft / w
End If
ResizeWindow
Case %IDT_X, %IDT_Y, %IDT_Z
MsgBox "No action. Toolbar included just to show how splitter bars work with toolbar in place.", %MB_OK + %MB_IconInformation, "Toolbar Button Pressed"
End Select
Case %WM_Size
ResizeWindow
Case %WM_SetCursor
iReturn = GetDlgCtrlID (CB.wParam)
'identify over which label control the mouse action took place
If iReturn = %IDC_LabelH Then MousePTR 9 : Function = 1 '9 = horizontal cursor
If iReturn = %IDC_LabelV Then MousePTR 7 : Function = 1 '7 = vertical cursor
'monitors the 3 basic splitter bar mouse actions, lbuttondown, mousemose, lbuttonup
Select Case Hi(Word, CB.lParam)
Case %WM_LButtonDown
'sets flags to say splitter action has started (no actual movement yet)
If iReturn = %IDC_LabelV Then SplitVInWork = 1
If iReturn = %IDC_LabelH Then SplitHInWork = 1
Case %WM_MouseMove
'Repositions splitter bars to match mouse position (if position has changed)
If SplitVInWork Then If MoveBarUpDown Then ResizeWindow
If SplitHInWork Then If MoveBarLeftRight Then ResizeWindow
Case %WM_LButtonUp
'sets flags to say splitter action has ended
SplitHInWork = 0 : SplitVInWork = 0
End Select
End Select
End Function
Function MoveBarUpDown As Long
Local pt As Point, h As Long, w As Long
Static oldY As Long
Dialog Get Client hDlg To w,h
GetCursorPos pt 'pt has xy screen coordinates
ScreenToClient hDlg, pt 'pt now has client coordinates
' If h-pt.y < 50 Then Exit Function 'limit right motion 'optional
' If pt.y < 50 Then Exit Function 'optional
YSplit = (pt.y-3) / h 'actually, should only do one or the other
VBarTop = h - (Pt.y-3) 'actually, should only do one or the other
If pt.y <> oldY Then Function = %True : oldY = pt.y
End Function
Function MoveBarLeftRight As Long
Local pt As Point, h As Long, w As Long
Static oldX As Long
Dialog Get Client hDlg To w,h
GetCursorPos pt 'pt has xy screen coordinates
ScreenToClient hDlg, pt 'pt now has client coordinates
' If w-pt.x < 50 Then Exit Function 'limit right motion 'optional
' If pt.x < 50 Then Exit Function 'optional
XSplit = (pt.x-3) / w 'actually, should only do one or the other
HBarLeft = pt.x-3 'actually, should only do one or the other
If pt.x <> oldX Then Function = %True : oldX = pt.x
End Function
Sub ResizeWindow
Local vx As Long, vy As Long, hx As Long, hy As Long, h As Long, w As Long, HLeft&, VTop&
Local yToolbar as Long, yStatusBar as Long, M as Long, T as Long
Local yStatus as Long, yTool as Long
Control Get Size hDlg, %IDC_StatusBar To w,yStatus
Control Get Size hDlg, %IDC_Toolbar To w,yTool
Dialog Get Client hDlg To w,h
M = 5 'margin around controls
T = 6 'splitter thickness (smallest dimension)
'get the splitter bar positions - top of vertical splitter (VTop) and
'left of horizontal splitter (HLeft). These will be used in positioning
'controls on the dialog. Result depends on whether fixed or percentage
'positioning is selected by the user.
If FixedSplitterBar& Then
VTop = h - VBarTop
HLeft = HBarLeft
Else
VTop = ySplit*h
HLeft = xSplit*w
End If
Select Case Layout& 'Function Layout returns a value 1-6
Case 1 'left beside right, both above bottom
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Show
Control Show State hDlg, %IDC_LabelV, %SW_Show
Control Show State hDlg, %IDC_Bottom, %SW_Show
'H splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
Control Set Size hDlg, %IDC_LabelH, T, VTop - yTool - M
'V splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelV, M, VTop
Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
'Left size/loc
Control Set Loc hDlg, %IDC_Left, M, yTool + M
Control Set Size hDlg, %IDC_Left, HLeft - 2*M, VTop - yTool - 2*M
'Right size/loc
Control Set Loc hDlg, %IDC_Right, HLeft + T + M, yTool + M
Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, VTop - yTool - 2*M
'Bottom size/loc
Control Set Loc hDlg, %IDC_Bottom, M, VTop + T + M
Control Set Size hDlg, %IDC_Bottom, w - 2*M, h - VTop - yStatus - 2*M - T
Case 2 'left over bottom, right top-to-bottom
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Show
Control Show State hDlg, %IDC_LabelV, %SW_Show
Control Show State hDlg, %IDC_Bottom, %SW_Show
'H splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + 5
Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
'V splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelV, M, VTop
Control Set Size hDlg, %IDC_LabelV, HLeft - M, T
'Left size/loc
Control Set Loc hDlg, %IDC_Left, M, yTool + M
Control Set Size hDlg, %IDC_Left, HLeft - 2*M, VTop - yTool - 2*M
'Right size/loc
Control Set Loc hDlg, %IDC_Right, HLeft + T + M, yTool + M
Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - yTool - yStatus - 2*M
'Bottom size/loc
Control Set Loc hDlg, %IDC_Bottom, M, VTop + T + M
Control Set Size hDlg, %IDC_Bottom, HLeft - 2*M, h - VTop - yStatus - 2*M - T
Case 3 'left beside right, bottom not visible
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Show
Control Show State hDlg, %IDC_LabelV, %SW_Hide
Control Show State hDlg, %IDC_Bottom, %SW_Hide
'H splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
'V splitter bar loc/size
' none - not visible
'Left loc/size
Control Set Loc hDlg, %IDC_Left, M, yTool + M
Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - yTool - yStatus - 2*M
'Right loc/size
Control Set Loc hDlg, %IDC_Right, HLeft + T + M, yTool + M
Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - yTool - yStatus - 2*M
'Bottom loc/size
' none - not visible
Case 4 'left beside right, both below bottom
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Show
Control Show State hDlg, %IDC_LabelV, %SW_Show
Control Show State hDlg, %IDC_Bottom, %SW_Show
'H splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelH, HLeft, VTop + T
Control Set Size hDlg, %IDc_LabelH, T, h - VTop - yStatus - M - T
'V splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelV, M, VTop
Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
'Left loc/size
Control Set Loc hDlg, %IDC_Left, M, VTop + T + M
Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - VTop - yStatus - 2*M - T
'Right loc/size
Control Set Loc hDlg, %IDC_Right, HLeft + T + M, VTop + T + M
Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - VTop - yStatus - 2*M - T
'Bottom loc/size
Control Set Loc hDlg, %IDC_Bottom, M, yTool + M
Control Set Size hDlg, %IDC_Bottom, w - 2*M, VTop - yTool - 2*M
Case 5 'left top-to-bottom, right over bottom
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Show
Control Show State hDlg, %IDC_LabelV, %SW_Show
Control Show State hDlg, %IDC_Bottom, %SW_Show
'H splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
'V splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelV, HLEFT + T, VTop
Control Set Size hDlg, %IDC_LabelV, w - HLeft - T - M, T
'Left loc/size
Control Set Loc hDlg, %IDC_Left, M, yTool + M
Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - yTool - yStatus - 2*M
'Right loc/size
Control Set Loc hDlg, %IDC_Right, HLeft + T + M, yTool + M
Control Set Size hDlg, %IDC_Right, w - HLeft - T - 2*M, VTop - yTool - 2*M
'Bottom loc/size
Control Set Loc hDlg, %IDC_Bottom, HLeft + T + M, VTop + T + M
Control Set Size hDlg, %IDC_Bottom, w - HLeft - T - 2*M, h - VTop - yStatus - 2*M - T
Case 6 'left over right, bottom not visible
'visibility
Control Show State hDlg, %IDC_LabelH, %SW_Hide
Control Show State hDlg, %IDC_LabelV, %SW_Show
Control Show State hDlg, %IDC_Bottom, %SW_Hide
'H splitter bar loc/size
' none - not visible
'V splitter bar loc/size
Control Set Loc hDlg, %IDC_LabelV, M, VTop
Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
'Left loc/size
Control Set Loc hDlg, %IDC_Left, M, yTool + M
Control Set Size hDlg, %IDC_Left, w - 2*M, VTop - yTool - 2*M
'Right loc/size
Control Set Loc hDlg, %IDC_Right, M, VTop + T + M
Control Set Size hDlg, %IDC_Right, w - 2*M, h - VTop - yStatus - 2*M - T
'Bottom loc/size
' none - not visible
End Select
StatusBar Set Parts hDlg, %IDC_StatusBar, w/3, w/3, w/3
Dialog Redraw hDlg
End Sub
'gbs_00392
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm