Visual Basic Text API Routines
SetWindowLong: Turn a Text Box into a Resizable Control
     
Posted:   Saturday January 25, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

txsize.gif (3215 bytes)I once had occasion to require a textbox that the user could size dynamically at runtime. By experimenting with the Windows APIs GetWindowLong, SetWindowLong, and SetWindowPos, I was able to create a textbox which, on command, had its fixed 3d border change into a sizeable border.
 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'windows constants  
Public Const SWP_DRAWFRAME As Long = &H20
Public Const SWP_NOMOVE As Long = &H2
Public Const SWP_NOSIZE As Long = &H1
Public Const SWP_NOZORDER As Long = &H4
Public Const SWP_FLAGS As Long = SWP_NOZORDER Or SWP_NOSIZE Or _
                                 SWP_NOMOVE Or SWP_DRAWFRAME
Public Const GWL_STYLE As Long = (-16)
Public Const WS_THICKFRAME As Long = &H40000

Public Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long

Public Declare Function SetWindowPos Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal hWndInsertAfter As Long, _
   ByVal X As Long, _
   ByVal Y As Long, _
   ByVal cx As Long, _
   ByVal cy As Long, _
   ByVal wFlags As Long) As Long
 Form Code
To a form add a text box (Text1), a command button (Command1), 2 menu items (mnuSize & mnuRestore), and 2 labels (Label1 & Label2). Add the following code to the form:

Option Explicit

  'variables initially set in Form_Load,  
  'used to reset default values
   Dim initBoxStyle As Long
   Dim initLeft As Integer
   Dim initTop As Integer
   Dim initWidth As Integer
   Dim initHeight As Integer

Private Sub Form_Load()

  'position the form
   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
 
  'set up a few variables needed later
   initBoxStyle = GetWindowLong(Text1.hwnd, GWL_STYLE)
   initLeft = Text1.Left
   initTop = Text1.Top
   initWidth = Text1.Width
   initHeight = Text1.Height

  'make sure the defaults are set
   SetControlStyle initBoxStyle, Text1
  
   Label1.Caption = "Text Width  : " & Text1.Width
   Label2.Caption = "Text Height : " & Text1.Height

End Sub


Private Sub Form_Unload(Cancel As Integer)

  'restore the control to a normal textbox
   SetControlStyle initBoxStyle, Text1

End Sub


Private Sub Form_Click()

  'restore the control to a normal textbox
   SetControlStyle initBoxStyle, Text1
  
   Label1.Caption = "Text Width  : " & Text1.Width
   Label2.Caption = "Text Height : " & Text1.Height
  
End Sub


Private Sub mnuSize_Click()

   Dim style As Long
 
  'get the current style attributes for the textbox
   style = GetWindowLong(Text1.hwnd, GWL_STYLE)
 
  'modify the style to show the sizing frame
   style = style Or WS_THICKFRAME
 
  'set the control to the chosen style
   SetControlStyle style, Text1

End Sub


Private Sub mnuRestore_Click()

  'restore the control to a normal textbox  
   SetControlStyle initBoxStyle, Text1
   
End Sub


Private Sub Command1_Click()
    Unload Me
End Sub


Private Sub SetControlStyle(style, X As Control)
   
  If style Then
    Call SetWindowLong(X.hwnd, GWL_STYLE, style)
    Call SetWindowPos(X.hwnd, Form1.hwnd, 0, 0, 0, 0, SWP_FLAGS)
  End If

End Sub
 Comments
Save the app & run. Clicking the 'Size' menu item will cause the textbox border to change into a sizeable frame. After resizing, clicking the form or selecting 'Restore' will return the textbox to the normal textbox style at the selected size and position.

This same technique can also be successfully applied to lists, listviews, treeviews, picture boxes ... pretty well most VB controls.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter