home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / system / bug_de / buggy.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-01-10  |  11.4 KB  |  267 lines

  1. VERSION 2.00
  2. Begin Form frmBuggy 
  3.    Caption         =   "Buggy Form"
  4.    ClientHeight    =   4392
  5.    ClientLeft      =   864
  6.    ClientTop       =   1536
  7.    ClientWidth     =   6060
  8.    Height          =   4812
  9.    Left            =   816
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4392
  12.    ScaleWidth      =   6060
  13.    Top             =   1164
  14.    Width           =   6156
  15.    Begin ListBox lstChanges 
  16.       Height          =   1368
  17.       Left            =   1800
  18.       TabIndex        =   6
  19.       Top             =   2820
  20.       Width           =   2892
  21.    End
  22.    Begin Frame Frame1 
  23.       Caption         =   "Memory and Resources"
  24.       Height          =   1272
  25.       Left            =   600
  26.       TabIndex        =   3
  27.       Top             =   1080
  28.       Width           =   5172
  29.       Begin Label lblResources 
  30.          Caption         =   "lblResources"
  31.          Height          =   372
  32.          Left            =   240
  33.          TabIndex        =   0
  34.          Top             =   780
  35.          Width           =   4812
  36.       End
  37.       Begin Label lblMemory 
  38.          Caption         =   "lblMemory"
  39.          Height          =   312
  40.          Left            =   240
  41.          TabIndex        =   1
  42.          Top             =   360
  43.          Width           =   4812
  44.       End
  45.    End
  46.    Begin Label Label2 
  47.       Caption         =   "Memory changes in Form_Resize:"
  48.       Height          =   252
  49.       Left            =   1800
  50.       TabIndex        =   5
  51.       Top             =   2520
  52.       Width           =   3012
  53.    End
  54.    Begin Label Label1 
  55.       Alignment       =   2  'Center
  56.       BorderStyle     =   1  'Fixed Single
  57.       Caption         =   "Resize me and watch the memory go!"
  58.       Height          =   372
  59.       Left            =   840
  60.       TabIndex        =   4
  61.       Top             =   120
  62.       Width           =   4320
  63.    End
  64.    Begin Label lblMode 
  65.       Alignment       =   2  'Center
  66.       BorderStyle     =   1  'Fixed Single
  67.       Caption         =   "lblMode"
  68.       Height          =   252
  69.       Left            =   600
  70.       TabIndex        =   2
  71.       Top             =   600
  72.       Width           =   5052
  73.    End
  74. ' Buggy.frm
  75. ' Purpose:
  76. ' Sample program to demonstrate a memory leak when we
  77. ' use Controls(i).Parent.ScaleHeight to get the form/frame size
  78. ' in order to proportionally move controls on a form.
  79. ' If you keep resizing it, you'll get an "out of memory" error.
  80. ' Date:         Author:
  81. ' 01/10/94      Elliott Whitticar (71221.1413@CompuServe.com)
  82. Option Explicit
  83. '=============================================
  84. ' Module Level Variable Declaration Section
  85. '=============================================
  86. Dim CtlDelta() As SizeStruct    ' Stores size and location of controls
  87. Dim mlFreeSpace As Long         ' Free memory at end of loop in Form_Resize
  88. Dim mlOldSpace As Long          ' Free memory at start of loop in Form_Resize
  89. Sub Form_Load ()
  90. ' Description:
  91. '   This code to get the relative position of all the controls
  92. '   on the form is from the "Elastic Controls Resize Proportionally"
  93. '   article by Bill Wilkey in the August/September issue of
  94. '   "Visual Basic Programmer's Journal."
  95.     Dim i As Integer                ' For-variable
  96.     'NOTE: For Screen, Form, and Printer objects the Width
  97.     'And Height properties are always measured in TWIPS.
  98.     'For the (non-MDI) Form and Printer objects the internal
  99.     'client area coordinate system can be changed to other
  100.     'scalings. For simplicity you may want to leave the
  101.     'coordinate system set to the VB default of TWIPS.
  102.     'However, some API routines require Pixels so you'll need
  103.     'to do conversion.
  104.     ' Begin UI Initialization
  105.     ' Perform any desired startup sizing of the
  106.     ' form here, before processing coordinates.
  107.     WindowState = NORMAL
  108.     ' Set desired scale mode here. Use the SCALEMODE property
  109.     ' for pre-defined scalings, use the
  110.     ' SCALETop,Left,Width,Height properties directly or the
  111.     ' SCALE method to define a custom coordinate system.
  112.     ' ScaleMode = TWIPS
  113.     ' Size the control dimension proportion array
  114.     ReDim CtlDelta(0 To Controls.Count - 1)
  115.     ' Loop through each control on the form and capture its
  116.     ' dimensions as a percentage of the scalable dimensions of
  117.     ' the form. (won't work for the line control)
  118.     For i = 0 To Controls.Count - 1
  119.         If TypeOf Controls(i) Is Line Then
  120.             CtlDelta(i).Top = Controls(i).Y1 / Controls(i).Parent.ScaleHeight
  121.             CtlDelta(i).Left = Controls(i).X1 / Controls(i).Parent.ScaleWidth
  122.             CtlDelta(i).Width = Controls(i).X2 / Controls(i).Parent.ScaleWidth
  123.             CtlDelta(i).Height = Controls(i).Y2 / Controls(i).Parent.ScaleHeight
  124.         Else
  125.             CtlDelta(i).Top = Controls(i).Top / Controls(i).Parent.ScaleHeight
  126.             CtlDelta(i).Left = Controls(i).Left / Controls(i).Parent.ScaleWidth
  127.             CtlDelta(i).Width = Controls(i).Width / Controls(i).Parent.ScaleWidth
  128.             CtlDelta(i).Height = Controls(i).Height / Controls(i).Parent.ScaleHeight
  129.         End If
  130.     Next
  131. End Sub
  132. Sub Form_Resize ()
  133. 'Description:   Proportionally resize and move controls based on
  134. '               relative positions in CtlDelta() array.
  135. 'Arguments:     N/A
  136. 'Return:        N/A
  137. '-----------------------------------------------------------------
  138. 'Date        Developer           Comments
  139. '01/10/95    E. Whitticar        Initial creation
  140. '*****************************************************************
  141.     '=============================================
  142.     'Local Constant/Variable Declaration Section
  143.     '=============================================
  144.     Dim i As Integer                ' For-variable
  145.     Dim lfParHeight As Single       ' ScaleHeight of parent control
  146.     Dim lfParWidth As Single        ' ScaleWidth of parent control
  147.     Dim lfLeft As Single            ' Left coordinate
  148.     Dim lfTop As Single             ' Top coordinate
  149.     Dim lfWidth As Single           ' Width
  150.     Dim lfHeight As Single          ' Height
  151.     ' Don't bother if we're just an icon
  152.     If WindowState = MINIMIZED Then Exit Sub
  153.     ' This will go faster if the form is not visible
  154.     Me.Hide
  155.     ' Save the amount of free memory before
  156.     ' using Controls(i).Parent.ScaleWidth
  157.     mlOldSpace = GetFreeSpace(0)       ' Original Free Memory
  158.     ' Loop through each control on the form and reset the dimensions
  159.     ' as a percentage of the scalable dimensions of the form.
  160.     For i = 0 To Controls.Count - 1
  161.         ' Get ScaleHeight and ScaleWidth of parent control or form
  162.         lfParHeight = Controls(i).Parent.ScaleHeight
  163.         lfParWidth = Controls(i).Parent.ScaleWidth
  164.         If TypeOf Controls(i) Is Line Then
  165.             Controls(i).Y1 = lfParHeight * CtlDelta(i).Top
  166.             Controls(i).X1 = lfParWidth * CtlDelta(i).Left
  167.             Controls(i).X2 = lfParWidth * CtlDelta(i).Width
  168.             Controls(i).Y2 = lfParHeight * CtlDelta(i).Height
  169.         
  170.         ElseIf TypeOf Controls(i) Is Menu Then
  171.             ' Skip it
  172.         ElseIf TypeOf Controls(i) Is Timer Then
  173.             ' Skip it
  174.         Else
  175.             ' Get all the size and postion properties
  176.             lfTop = lfParHeight * CtlDelta(i).Top
  177.             lfLeft = lfParWidth * CtlDelta(i).Left
  178.             lfWidth = lfParWidth * CtlDelta(i).Width
  179.             lfHeight = lfParHeight * CtlDelta(i).Height
  180.             
  181.             If TypeOf Controls(i) Is TextBox Then
  182.                 ' Set all but the height, unless it's multiline
  183.                 If Controls(i).MultiLine = True Then
  184.                     Controls(i).Move lfLeft, lfTop, lfWidth, lfHeight
  185.                 Else
  186.                     Controls(i).Move lfLeft, lfTop, lfWidth
  187.                 End If
  188.             
  189.             ElseIf TypeOf Controls(i) Is Label Then
  190.                 ' Set only the position, don't change dimensions
  191.                 Controls(i).Move lfLeft, lfTop
  192.             
  193.             ElseIf TypeOf Controls(i) Is CommandButton Then
  194.                 ' Set only the position, don't change dimensions
  195.                 Controls(i).Move lfLeft, lfTop
  196.             
  197.             ElseIf TypeOf Controls(i) Is ComboBox Then
  198.                 ' Set all but the height, unless it's style 1
  199.                 If Controls(i).Style = 1 Then
  200.                     Controls(i).Move lfLeft, lfTop, lfWidth, lfHeight
  201.                 Else
  202.                     Controls(i).Move lfLeft, lfTop, lfWidth
  203.                 End If
  204.             
  205.             ElseIf TypeOf Controls(i) Is HScrollBar Then
  206.                 ' Set all but the height
  207.                 Controls(i).Move lfLeft, lfTop, lfWidth
  208.             ElseIf TypeOf Controls(i) Is VScrollBar Then
  209.                 ' Set all but the width
  210.                 Controls(i).Move lfLeft, lfTop
  211.                 Controls(i).Height = lfParHeight * CtlDelta(i).Height
  212. ' **********
  213. ' Put special code for any controls you're using here.
  214.             ' ElseIf TypeOf Controls(i) Is SSPanel Then
  215.             '     ' If it doesn't automatically align to the top or bottom, move it.
  216.             '     If Controls(i).Align = 0 Then
  217.             '         Controls(i).Move lfLeft, lfTop, lfWidth, lfHeight
  218.             '     Else
  219.             '         ' Skip it -- it aligns itself automatically
  220.             '     End If
  221.             ' ElseIf TypeOf Controls(i) Is MhDateInput Then
  222.             '     ' Set all but the height
  223.             '     Controls(i).Move lfLeft, lfTop, lfWidth
  224.             ' ElseIf TypeOf Controls(i) Is MaskEdBox Then
  225.             '     ' Set all but the height
  226.             '     Controls(i).Move lfLeft, lfTop, lfWidth
  227. ' **********
  228.             Else
  229.                 ' For any other control, move it.
  230.                 Controls(i).Move lfLeft, lfTop, lfWidth, lfHeight
  231.             End If
  232.         End If
  233.     Next
  234.     mlFreeSpace = GetFreeSpace(0)       ' Free Memory
  235.     ' Perform any desired custom sizing/updates
  236.     ' of the controls here.
  237.     ' Restore visibility to the form
  238.     Me.Show
  239.     ShowResources
  240. End Sub
  241. Sub ShowResources ()
  242. ' Description:  Displays mode, memory and resources
  243.     ' Local variables
  244.     Dim llWinFlags As Long      ' Returned from GetWinFlags()
  245.     Dim llVerNum As Long        ' Returned from GetVersion(): DOS and Windows Version Nos.
  246.     Dim lsVersion As String     ' Windows version string
  247.     Dim lsFreeSpace As String   ' KB of free memory (formatted)
  248.     Dim lwVerWord As Integer    ' Low word of version
  249.     ' Set the label captions to indicate the windows mode and free memory
  250.     llVerNum = GetVersion()
  251.     lwVerWord = CInt(llVerNum And &HFFFF&)
  252.     lsVersion = "Windows " & CStr(lwVerWord And &HFF) & "." & CStr(lwVerWord \ 256)
  253.     llWinFlags = GetWinFlags()
  254.     If llWinFlags And WF_ENHANCED Then
  255.         lblMode.Caption = lsVersion & ", 386-enhanced mode"
  256.     ElseIf llWinFlags And WF_PMODE Then
  257.         lblMode.Caption = lsVersion & ", protected mode"
  258.     Else
  259.         lblMode.Caption = lsVersion & ", standard mode"
  260.     End If
  261.     lsFreeSpace = Format$(mlFreeSpace, "###,###,###")
  262.     lblMemory.Caption = "Free Memory:       " & lsFreeSpace & " Bytes"
  263.     lblResources.Caption = "Free Resources:  " & GetFreeSystemResources(GFSR_SYSTEMRESOURCES) & "% (system); " & GetFreeSystemResources(GFSR_GDIRESOURCES) & "% (GDI); " & GetFreeSystemResources(GFSR_USERRESOURCES) & "% (user)"
  264.     lstChanges.AddItem CStr(mlFreeSpace - mlOldSpace)
  265.     lstChanges.ListIndex = lstChanges.ListCount - 1
  266. End Sub
  267.