【VB开源代码栏目提醒】:网学会员为广大网友收集整理了,动态创建按钮.frm,希望对大家有所帮助!
VERSION 5.00
Begin VB.Form Command1
BorderStyle = 1 'Fixed Single
Caption = "动态创建按钮示例"
ClientHeight = 4455
ClientLeft = 6210
ClientTop = 8205
ClientWidth = 6165
Icon = "动态创建按钮.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 4455
ScaleWidth = 6165
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Index = 0
Left = 1320
TabIndex = 0
Top = 1080
Width = 1455
End
Begin VB.Menu CmdButtonM
Caption = "按钮控件操作"
Begin VB.Menu AddButton
Caption = "添加按钮(&A)"
Shortcut = ^A
End
Begin VB.Menu DelButton
Caption = "删除按钮(&D)"
Shortcut = ^D
End
Begin
VB.Menu ButtonArray
Caption = "-"
Index = 0
End
End
End
Attribute VB_Name = "Command1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'声明API函数
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI '定义点结构类型
X As Long
Y As Long
End Type
Dim i As Integer '定义整型变量
'添加按钮事件
Private Sub AddButton_Click()
Dim pt As POINTAPI '定义点结构类型变量
GetCursorPos pt '调用函数GetCursorPos
ScreenToClient hwnd, pt '调用函数ScreenToClient
i = Command1.UBound + 1 '获得命令按钮控件数组的边界值
Load Command1(i) '加载命令按钮
'设置加载命令按钮位置
Command1(i).Move pt.X * 50, pt.Y * 50, Command1(0).Width, Command1(0).Height
Command1(i).Caption = "Cmd" & Str$(i) '设置加载命令按钮标题
Command1(i).Visible = True '使得加载命令按钮可见
End Sub
'删除按钮事件
Private Sub DelButton_Click()
If Command1.Count > 0 Then
For i = 0 To Command1.UBound
If Command1(i).Visible = True Then
Unload Command1(i) '卸载命令按钮
End If
Next i
End If
MsgBox "已经清空所有按钮!", vbOKOnly, "提示信息!" '提示清空所有按钮
End Sub
Private Sub Form_Load()
Command1(0).Visible = False '使得第一个命令按钮不可见
End Sub