Posts o[vb6] anchor .net style
Post
Cancel

o[vb6] anchor .net style

A very simple lib to automatic resize controls in forms/containers using anchor .net style based on design positions.

source - http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=63764

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//test vb6 app
Option Explicit

Private layout As New DynamicLayout

Private Sub Form_Load()
    InitializeControlsPositions
End Sub

Private Sub InitializeControlsPositions()
    With layout
        .Insert Label1
        .Insert ComboBox1, apLeft Or apTop Or apRight
        .Insert Command1, apRight
        .Insert Frame1, apAll
        .Insert Command3, apRight Or apBottom
        .Insert Command4, apRight Or apBottom
        .Insert Command2, apLeft Or apBottom
        .Insert Text1(0), apAll
        .Insert Text1(1), apLeft Or apRight Or apBottom
        .Insert Picture1, apLeft Or apRight Or apBottom
        .Insert HScroll1, apLeft Or apRight
    End With
End Sub

Private Sub Form_Resize()
    layout.Resize
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//DynamicLayout.cls
Option Explicit

Public Enum AnchorPositionsEnum
    apLeft = 1
    apTop = 2
    apRight = 4
    apBottom = 8
    apAll = 15
End Enum

Private m_objControls As DynamicLayoutControls

Private Sub Class_Initialize()
    Set m_objControls = New DynamicLayoutControls
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
    clear
    Set m_objControls = Nothing
End Sub

Public Sub clear()
    Dim c As DynamicLayoutControl
    For Each c In m_objControls
        m_objControls.Remove c.key
    Next
End Sub

Public Sub insert(ctl As Object, Optional anchor As AnchorPositionsEnum = apLeft Or apTop)
    m_objControls.Add ctl, CInt(anchor)
End Sub

Public Sub Remove(ctl As Object)
    Dim c As DynamicLayoutControl
    Dim sCtlKey As String

    sCtlKey = "k:" & ctl.Name & ":" & ctl.Index

    For Each c In m_objControls
        If c.key = sCtlKey Then
            m_objControls.Remove c.key
            Exit For
        End If
    Next
End Sub

Public Sub resize()
    Dim oControl As DynamicLayoutControl
    Dim oCurParentPos As DynamicLayoutRectangle
    Dim oNewControlPos As DynamicLayoutRectangle
    Dim sGap As Single

    For Each oControl In m_objControls

        Set oCurParentPos = New DynamicLayoutRectangle
        Set oNewControlPos = oControl.position

        With oControl.instance.Parent
            If TypeOf oControl.instance.Parent Is MDIForm Then
                oCurParentPos.X1 = .ScaleLeft
                oCurParentPos.Y1 = .ScaleTop
                oCurParentPos.X2 = .ScaleWidth
                oCurParentPos.Y2 = .ScaleHeight
            ElseIf TypeOf oControl.instance.Parent Is Form Then
                oCurParentPos.X1 = .ScaleLeft
                oCurParentPos.Y1 = .ScaleTop
                oCurParentPos.X2 = .ScaleWidth
                oCurParentPos.Y2 = .ScaleHeight
            Else
                oCurParentPos.X1 = .Left
                oCurParentPos.Y1 = .Top
                oCurParentPos.X2 = .Width
                oCurParentPos.Y2 = .Height
            End If
        End With

        If ((oControl.anchors And apRight) = apRight) Then
            If ((oControl.anchors And apLeft) = apLeft) Then
                oNewControlPos.X2 = oCurParentPos.X2 - (oControl.margin.X1 + oControl.margin.X2)
            Else
                oNewControlPos.X1 = oCurParentPos.X2 - (oControl.position.X2 + oControl.margin.X2)
            End If
        End If

        If ((oControl.anchors And apBottom) = apBottom) Then
            If ((oControl.anchors And apTop) = apTop) Then
                oNewControlPos.Y2 = oCurParentPos.Y2 - (oControl.margin.Y1 + oControl.margin.Y2)
            Else
                oNewControlPos.Y1 = oCurParentPos.Y2 - (oControl.position.Y2 + oControl.margin.Y2)
            End If
        End If

        oControl.position = oNewControlPos

    Next

End Sub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//DynamicLayoutControl.cls
Option Explicit

Private m_strKey As String
Private m_objControl As Control
Private m_intAnchors As Integer
Private m_objMargin As DynamicLayoutRectangle

Public Property Get key() As String
    key = m_strKey
End Property

Public Property Let key(ByVal strValue As String)
    m_strKey = strValue
End Property

Public Property Get instance() As Control
    Set instance = m_objControl
End Property

Public Property Set instance(ctl As Control)
    Dim oParentPos As New DynamicLayoutRectangle

    Set m_objControl = ctl

    With m_objControl.Parent
        If TypeOf m_objControl.Parent Is MDIForm Then
            oParentPos.X1 = .ScaleLeft
            oParentPos.Y1 = .ScaleTop
            oParentPos.X2 = .ScaleWidth
            oParentPos.Y2 = .ScaleHeight
        ElseIf TypeOf m_objControl.Parent Is Form Then
            oParentPos.X1 = .ScaleLeft
            oParentPos.Y1 = .ScaleTop
            oParentPos.X2 = .ScaleWidth
            oParentPos.Y2 = .ScaleHeight
        Else
            oParentPos.X1 = .Left
            oParentPos.Y1 = .Top
            oParentPos.X2 = .Width
            oParentPos.Y2 = .Height
        End If
    End With

    With m_objMargin
        .X1 = m_objControl.Left
        .Y1 = m_objControl.Top
        .X2 = oParentPos.X2 - (m_objControl.Left + m_objControl.Width)
        .Y2 = oParentPos.Y2 - (m_objControl.Top + m_objControl.Height)
    End With

End Property

Public Property Get anchors() As Integer
    anchors = m_intAnchors
End Property

Public Property Let anchors(ByVal intValue As Integer)
    m_intAnchors = intValue
End Property

Public Property Get margin() As DynamicLayoutRectangle
    Set margin = m_objMargin
End Property

Public Property Get position() As DynamicLayoutRectangle
    Dim pos As New DynamicLayoutRectangle
    pos.X1 = m_objControl.Left
    pos.Y1 = m_objControl.Top
    pos.X2 = m_objControl.Width
    pos.Y2 = m_objControl.Height
    Set position = pos
End Property

Public Property Let position(ByVal newValue As DynamicLayoutRectangle)
    On Error Resume Next
    If newValue.X2 < 0="" then="" newvalue.x2="0" if="" newvalue.y2="">< 0 then newvalue.y2 = 0
    m_objcontrol.left = newvalue.x1
    m_objcontrol.top = newvalue.y1
    m_objcontrol.width = newvalue.x2
    m_objcontrol.height = newvalue.y2
end property

private sub class_initialize()
    set m_objcontrol = nothing
    set m_objmargin = new dynamiclayoutrectangle
end sub

private sub class_terminate()
    m_strkey = ""
    m_intanchors = 0
    set m_objcontrol = nothing
    set m_objmargin = nothing
end sub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//dynamiclayoutcontrols.cls
option explicit

private mcol as collection

public function add(ctrl as control, byval anchors as integer) as dynamiclayoutcontrol
    dim objnewmember as dynamiclayoutcontrol

    set objnewmember = new dynamiclayoutcontrol

    on error goto 2
1:  objnewmember.key = "k:" & ctrl.name & ":" & ctrl.index
    goto 3

2:  on error goto 3
    objnewmember.key = "k:" & ctrl.name & ":"

3:  objnewmember.anchors = anchors
    if isobject(ctrl) then
        set objnewmember.instance = ctrl
    else
        objnewmember.instance = ctrl
    end if

    mcol.add objnewmember, objnewmember.key

    set add = objnewmember
    set objnewmember = nothing

end function

public property get item(vntindexkey as variant) as dynamiclayoutcontrol
attribute item.vb_usermemid = 0
  set item = mcol(vntindexkey)
end property

public property get count() as long
    count = mcol.count
end property

public sub remove(vntindexkey as variant)
    mcol.remove vntindexkey
end sub

public property get newenum() as iunknown
attribute newenum.vb_usermemid = -4
attribute newenum.vb_memberflags = "40"
    set newenum = mcol.[_newenum]
end property

private sub class_initialize()
    set mcol = new collection
end sub

private sub class_terminate()
    set mcol = nothing
end sub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//dynamiclayoutrectangle.cls
option explicit

private m_sglx1 as single
private m_sgly1 as single
private m_sglx2 as single
private m_sgly2 as single

public property get x1() as single
    x1 = m_sglx1
end property
public property let x1(byval newvalue as single)
    m_sglx1 = newvalue
end property

public property get y1() as single
    y1 = m_sgly1
end property
public property let y1(byval newvalue as single)
    m_sgly1 = newvalue
end property

public property get x2() as single
    x2 = m_sglx2
end property
public property let x2(byval newvalue as single)
    m_sglx2 = newvalue
end property

public property get y2() as single
    y2 = m_sgly2
end property
public property let y2(byval newvalue as single)
    m_sgly2 = newvalue
end property
``` 0="" then="" newvalue.y2="0" m_objcontrol.left="newValue.X1" m_objcontrol.top="newValue.Y1" m_objcontrol.width="newValue.X2" m_objcontrol.height="newValue.Y2" end="" property="" private="" sub="" class_initialize()="" set="" m_objcontrol="Nothing" set="" m_objmargin="New" dynamiclayoutrectangle="" end="" sub="" private="" sub="" class_terminate()="" m_strkey="" m_intanchors="0" set="" m_objcontrol="Nothing" set="" m_objmargin="Nothing" end="" sub="" ```="" ```js="" dynamiclayoutcontrols.cls="" option="" explicit="" private="" mcol="" as="" collection="" public="" function="" add(ctrl="" as="" control,="" byval="" anchors="" as="" integer)="" as="" dynamiclayoutcontrol="" dim="" objnewmember="" as="" dynamiclayoutcontrol="" set="" objnewmember="New" dynamiclayoutcontrol="" on="" error="" goto="" 2="" 1:="" objnewmember.key="k:" &="" ctrl.name="" &="" ":"="" &="" ctrl.index="" goto="" 3="" 2:="" on="" error="" goto="" 3="" objnewmember.key="k:" &="" ctrl.name="" &="" ":"="" 3:="" objnewmember.anchors="anchors" if="" isobject(ctrl)="" then="" set="" objnewmember.instance="ctrl" else="" objnewmember.instance="ctrl" end="" if="" mcol.add="" objnewmember,="" objnewmember.key="" set="" add="objNewMember" set="" objnewmember="Nothing" end="" function="" public="" property="" get="" item(vntindexkey="" as="" variant)="" as="" dynamiclayoutcontrol="" attribute="" item.vb_usermemid="0" set="" item="mCol(vntIndexKey)" end="" property="" public="" property="" get="" count()="" as="" long="" count="mCol.Count" end="" property="" public="" sub="" remove(vntindexkey="" as="" variant)="" mcol.remove="" vntindexkey="" end="" sub="" public="" property="" get="" newenum()="" as="" iunknown="" attribute="" newenum.vb_usermemid="-4" attribute="" newenum.vb_memberflags="40" set="" newenum="mCol.[_NewEnum]" end="" property="" private="" sub="" class_initialize()="" set="" mcol="New" collection="" end="" sub="" private="" sub="" class_terminate()="" set="" mcol="Nothing" end="" sub="" ```=""  ="" ```js="" dynamiclayoutrectangle.cls="" option="" explicit="" private="" m_sglx1="" as="" single="" private="" m_sgly1="" as="" single="" private="" m_sglx2="" as="" single="" private="" m_sgly2="" as="" single="" public="" property="" get="" x1()="" as="" single="" x1="m_sglX1" end="" property="" public="" property="" let="" x1(byval="" newvalue="" as="" single)="" m_sglx1="newValue" end="" property="" public="" property="" get="" y1()="" as="" single="" y1="m_sglY1" end="" property="" public="" property="" let="" y1(byval="" newvalue="" as="" single)="" m_sgly1="newValue" end="" property="" public="" property="" get="" x2()="" as="" single="" x2="m_sglX2" end="" property="" public="" property="" let="" x2(byval="" newvalue="" as="" single)="" m_sglx2="newValue" end="" property="" public="" property="" get="" y2()="" as="" single="" y2="m_sglY2" end="" property="" public="" property="" let="" y2(byval="" newvalue="" as="" single)="" m_sgly2="newValue" end="" property=""></ 0 then newvalue.y2 = 0
    m_objcontrol.left = newvalue.x1
    m_objcontrol.top = newvalue.y1
    m_objcontrol.width = newvalue.x2
    m_objcontrol.height = newvalue.y2
end property

private sub class_initialize()
    set m_objcontrol = nothing
    set m_objmargin = new dynamiclayoutrectangle
end sub

private sub class_terminate()
    m_strkey = ""
    m_intanchors = 0
    set m_objcontrol = nothing
    set m_objmargin = nothing
end sub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//dynamiclayoutcontrols.cls
option explicit

private mcol as collection

public function add(ctrl as control, byval anchors as integer) as dynamiclayoutcontrol
    dim objnewmember as dynamiclayoutcontrol

    set objnewmember = new dynamiclayoutcontrol

    on error goto 2
1:  objnewmember.key = "k:" & ctrl.name & ":" & ctrl.index
    goto 3

2:  on error goto 3
    objnewmember.key = "k:" & ctrl.name & ":"

3:  objnewmember.anchors = anchors
    if isobject(ctrl) then
        set objnewmember.instance = ctrl
    else
        objnewmember.instance = ctrl
    end if

    mcol.add objnewmember, objnewmember.key

    set add = objnewmember
    set objnewmember = nothing

end function

public property get item(vntindexkey as variant) as dynamiclayoutcontrol
attribute item.vb_usermemid = 0
  set item = mcol(vntindexkey)
end property

public property get count() as long
    count = mcol.count
end property

public sub remove(vntindexkey as variant)
    mcol.remove vntindexkey
end sub

public property get newenum() as iunknown
attribute newenum.vb_usermemid = -4
attribute newenum.vb_memberflags = "40"
    set newenum = mcol.[_newenum]
end property

private sub class_initialize()
    set mcol = new collection
end sub

private sub class_terminate()
    set mcol = nothing
end sub

```js //dynamiclayoutrectangle.cls option explicit

private m_sglx1 as single private m_sgly1 as single private m_sglx2 as single private m_sgly2 as single

public property get x1() as single x1 = m_sglx1 end property public property let x1(byval newvalue as single) m_sglx1 = newvalue end property

public property get y1() as single y1 = m_sgly1 end property public property let y1(byval newvalue as single) m_sgly1 = newvalue end property

public property get x2() as single x2 = m_sglx2 end property public property let x2(byval newvalue as single) m_sglx2 = newvalue end property

public property get y2() as single y2 = m_sgly2 end property public property let y2(byval newvalue as single) m_sgly2 = newvalue end property ```>

origin - http://www.pipiscrew.com/?p=3357 vb6-anchor-net-style

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags