Search Tools Links Login

Create Controls at RunTime

Posted: 2019-09-26
By: AndreaTincani
Viewed: 243

Filed Under:

Visual Basic 6, Visual Basic 6, Visual Basic 6, Visual Basic 6, Visual Basic 6

No attachments for this post

A module that allows you to create a control during the execution of your program, on the fly. This example creates a simple button.

'API Calls
Public Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long, ByVal dwnewlong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Function DefWindowProc Lib "user32.dll" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'API Constants
Public Const WS_CHILD = &H40000000
Public Const WS_THICKFRAME = &H40000
Public Const WS_VISIBLE = &H10000000
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const GWL_WNDPROC = (-4)
'Create a Module named Module1

'Address of the original WindowProc of the Button
Public myOldhWndProc As Long

'CreateButton...Create a new button with resize capabilities and install a event handler for the button
'frm : Form that will contain the button (Parent)
'Caption : Button Caption
'Left , Top, Width, Height : Size (in pixels) of the button
'OnClick : Address of the new events handler procedure
'OldHWndProc : Address of the old events handler procedure, this parameter will be
' returned by the sub to use it in the event handler proc to call the old WindowProc
Public Sub CreateButton(frm As Form, Caption As String, Left As Long, Top As Long, Height As Long, Width As Long, OnClick As Long, ByRef OldhWndProc As Long)
    Dim hWnd As Long

    hWnd = CreateWindowEx(0&, "BUTTON", Caption, WS_VISIBLE Or WS_CHILD Or WS_THICKFRAME, Left, Top, Height, Width, frm.hWnd, 0&, App.hInstance, 0&)
    If hWnd And OnClick <> 0 Then
        'Set the new WindowProc to manage the button events
        OldhWndProc = SetWindowLong(hWnd, GWL_WNDPROC, OnClick)
    End If
End Sub

'New WindowProc to manage button's events
Public Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    If uMsg = WM_LBUTTONUP Then
        'Make a sound
    End If
    'Call the old WindowProc for the button
    MyWindowProc = CallWindowProc(myOldhWndProc, hWnd, uMsg, wParam, lParam)
End Function


'Make a form with a command button named Command1 as default. Place the following code in Form1 code window:

Private Sub Command1_Click()
    CreateButton Me, "Button", 5, 5, 300, 100, AddressOf Module1.MyWindowProc, myOldhWndProc
End Sub

This will create a resizable button.

Special Instructions

This code originally appeared on, and has been republished here with the permission of Andrea Tincani.

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.