下面的代码点击FORM1之COMMAND1可为FORM2添加一个命令按钮,该按钮可执行相应的事件:
Private WithEvents NewButton As CommandButton
Private Sub Command1_Click() '增加控件
If NewButton Is Nothing Then
Set NewButton = Form2.Controls.Add("VB.CommandButton", "cmdNew", Form2) '增加新的按钮cmdNew
NewButton.Move 200, 200, 2000, 500 '确定新增按钮cmdNew的位置
NewButton.Caption = "新增的按钮"
NewButton.Visible = True
End If
Form2.Show
End Sub
Private Sub NewButton_Click()
MsgBox "you click newbutton of form2"
End Sub
Private Sub NewButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Shift = 1 Then NewButton.Caption = "abcde": NewButton.FontItalic = True
End Sub
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20&
Const WS_CHILD = &H40000000
Const CW_USEDEFAULT = &H80000000
Const SW_NORMAL = 1
Private Type CREATESTRUCT
lpCreateParams As Long
hInstance As Long
hMenu As Long
hWndParent As Long
cy As Long
cx As Long
y As Long
x As Long
style As Long
lpszName As String
lpszClass As String
ExStyle As Long
End Type
Private 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
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Dim mWnd As Long
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Load()
Dim CS As CREATESTRUCT
mWnd = CreateWindowEx(WS_EX_STATICEDGE Or WS_EX_TRANSPARENT, "STATIC", "Hello World !", WS_CHILD, 0, 0, 300, 50, Form2.hwnd, 0, App.hInstance, CS)
Me.Caption = mWnd
ShowWindow mWnd, SW_NORMAL
End Sub
Private Sub Form_Unload(Cancel As Integer)
DestroyWindow mWnd
End Sub
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20&
Const WS_CHILD = &H40000000
Const CW_USEDEFAULT = &H80000000
Const SW_NORMAL = 1
Private Type CREATESTRUCT
lpCreateParams As Long
hInstance As Long
hMenu As Long
hWndParent As Long
cy As Long
cx As Long
y As Long
x As Long
style As Long
lpszName As String
lpszClass As String
ExStyle As Long
End Type
Private 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
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Dim mWnd As Long
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Load()
Dim CS As CREATESTRUCT
mWnd = CreateWindowEx(WS_EX_STATICEDGE Or WS_EX_TRANSPARENT, "STATIC", "Hello World !", WS_CHILD, 0, 0, 300, 50, Form2.hwnd, 0, App.hInstance, CS)
Me.Caption = mWnd
ShowWindow mWnd, SW_NORMAL
End Sub
Private Sub Form_Unload(Cancel As Integer)
DestroyWindow mWnd
End Sub