动态生成窗口不行吧,生成控件可以Private Sub Form_Load()
Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
With Form1!cmdObj1
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
End Sub注意 上面的代码例子使用 ! 作为一个语法要素。您也可以使用标准集合语法如 Form1.Controls("cmdObj1") 来引用该控件。
Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
With Form1!cmdObj1
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
End Sub注意 上面的代码例子使用 ! 作为一个语法要素。您也可以使用标准集合语法如 Form1.Controls("cmdObj1") 来引用该控件。
Option ExplicitPrivate Sub Command1_Click()
Dim a As New Form1
a.Show
End Sub生成窗体之后,用songq()说的方法增删控件 /^&&^\
--
如何在运行时动态增加控件 简单几行代码即可搞定。 Private Sub Command1_Click()
Dim Txt As TextBox
Set Tex = Controls.Add("VB.TextBox", "AddTextBox")
Txt.Visible = True
End Sub http://www.21code.com/school/?pos=view&id=1084--------------------------------------------------------------------------------
VB6.0动态加载ActiveX控件漫谈
Option Explicit
Public Type WNDCLASSEX
cbSize As Long
style As Long
lpfnWndProc As Long
cbClsExtra As Long
cbWndExtra As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
hIconSm As Long
End Type
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type PAINTSTRUCT
hdc As Long
fErase As Long
rcPaint As RECT
fRestore As Long
fIncUpdate As Long
rgbReserved(32) As Byte
End Type
Public Const CS_HREDRAW = &H2
Public Const CS_VREDRAW = &H1
Public Const COLOR_WINDOW = 5
Public Const DT_CENTER = &H1
Public Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer
Public Const WS_CAPTION = &HC00000
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_THICKFRAME = &H40000
Public Const WS_OVERLAPPED = &H0&
Public Const WS_SYSMENU = &H80000
Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Public Const CW_USEDEFAULT = &H80000000
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CreateWindowEx Lib "user32" 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 Const SW_SHOW = 5
Public Declare Function GetLastError Lib "kernel32" () As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function BeginPaint Lib "user32" (ByVal hwnd As Long, lpPaint As PAINTSTRUCT) As Long
Public Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Public Declare Function EndPaint Lib "user32" (ByVal hwnd As Long, lpPaint As PAINTSTRUCT) As Long
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Public Const WM_COMMAND = &H111
Public Const WM_PAINT = &HF
Public Const WM_DESTROY = &H2
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateAccelerator Lib "user32" Alias "TranslateAcceleratorA" (ByVal hwnd As Long, ByVal hAccTable As Long, lpMsg As MSG) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
Public Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As String) As Long
Public Const IDC_ARROW = 32512&
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const HOTKEYID = &H1234
Dim hInst As Long, szWindowClass As String, szTitle As StringSub Main()
Dim m As MSG
szWindowClass = "VBSDKWND"
szTitle = "VB Windows"
Call MyRegisterClass(App.hInstance)
Call InitInstance(App.hInstance, SW_SHOW)
While GetMessage(m, 0, 0, 0)
If Not TranslateAccelerator(m.hwnd, 0, m) Then
TranslateMessage m
DispatchMessage m
End If
Wend
'return msg.wParam;
End SubFunction MyRegisterClass(hInstance As Long) As Integer
Dim wcex As WNDCLASSEX
Dim picicon As IPictureDisp, picCur As IPictureDisp
wcex.cbSize = Len(wcex)
wcex.style = CS_HREDRAW Or CS_VREDRAW wcex.lpfnWndProc = Funptr(AddressOf WndProc)
wcex.cbClsExtra = 0
wcex.cbWndExtra = 0
wcex.hInstance = hInstance
'Set picicon = LoadResPicture(101, vbResIcon)
'Set picCur = LoadResPicture(101, vbResCursor)
wcex.hIcon = 0 ' picicon.Handle
wcex.hCursor = LoadCursor(0, IDC_ARROW) 'picCur.Handle
wcex.hbrBackground = COLOR_WINDOW + 1
wcex.lpszMenuName = vbNullString
wcex.lpszClassName = szWindowClass
wcex.hIconSm = 0 'picicon.Handle
MyRegisterClass = RegisterClassEx(wcex)End FunctionFunction WndProc(ByVal hwnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim wmId As Long, wmEvent As Long
Dim ps As PAINTSTRUCT
Dim hdc As Long
Dim szHello As String
szHello = "Hello World!"
Select Case message
Case WM_COMMAND
wmId = 0
CopyMemory VarPtr(wmId), VarPtr(wParam), 2
wmEvent = 0
CopyMemory VarPtr(wmId), VarPtr(wParam) + 2, 2
' Parse the menu selections:
Select Case wmId
Case Else
WndProc = DefWindowProc(hwnd, message, wParam, lParam)
Exit Function
End Select
Case WM_HOTKEY
MsgBox "hot key"
Case WM_PAINT
hdc = BeginPaint(hwnd, ps)
Dim rt As RECT
GetClientRect hwnd, rt
DrawText hdc, szHello, Len(szHello), rt, DT_CENTER
EndPaint hwnd, ps
Case WM_DESTROY
UnregisterHotKey hwnd, HOTKEYID
PostQuitMessage 0
Case Else
WndProc = DefWindowProc(hwnd, message, wParam, lParam)
Exit Function
End Select
WndProc = 0
End Function
Function Funptr(fun As Long) As Long
Funptr = fun
End FunctionFunction InitInstance(hInstance As Long, nCmdShow As Long) As Boolean
Dim hwnd As Long
hInst = hInstance
hwnd = CreateWindowEx(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, _
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0)
If hwnd = 0 Then
InitInstance = False
Exit Function
End If
ShowWindow hwnd, nCmdShow
UpdateWindow hwnd
'HotKey is Alt + Ctrl + O
If RegisterHotKey(hwnd, HOTKEYID, MOD_ALT Or MOD_CONTROL, &H4F) = 0 Then
MsgBox "Register Hot Key Failed"
End If
InitInstance = True
End Function