borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincani
:: Create Controls at RunTime

Author  

Mancubus and Andrea Tincani

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
API Declarations

'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)

Module

'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
   
'OnLeftButtonUp
    If uMsg = WM_LBUTTONUP Then
       
'Make a sound
        Beep
    End If
   
'Call the old WindowProc for the button
    MyWindowProc = CallWindowProc(myOldhWndProc, hWnd, uMsg, wParam, lParam)
End Function

Usage

'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 :))

:: Navigation

Home

Miscellaneous API Tips

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 14-02-2007 Re: property pages of user controls by JLRodgers
icon 13-04-2005 Re: Running an app off the server by steve_w
icon 10-05-2004 creating forms at runtime. whoa. by paulaa
icon 17-12-2003 Re: Working of a Professional Form by fabulous
icon 17-09-2003 Dynamically Creating Controlls in VB.net by Chris_871
:: Sponsored Links



Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder