Option Explicit Private WithEvents Text1 As TextBox 'Text1 指向最后一个添加的文本框,就是当前要调整的。'Private Sub Command1_Click() Dim i As Long i = Me.Controls.Count + 1 Set Text1 = Me.Controls.Add("VB.TextBox", "Text" & i) Text1.Move 0, 480 * i, 1440, 360 Text1.Visible = True End Sub
这个代码可以创建新的文本框Option Explicit Private WithEvents NewTextBox As TextBox'增加控件 Private Sub Command1_Click() If NewTextBox Is Nothing Then '增加新的文本框 Set NewTextBox = Controls.Add("VB.TextBox", "NewText", Me) '确定新增文本框的位置 NewTextBox.Move Command1.Left + Command1.Width + 120, Command1.Top NewTextBox.Text = "新建的文本框" NewTextBox.Visible = True End If End Sub '删除控件(注:只能删除动态增加的控件) Private Sub Command2_Click() If Not (NewTextBox Is Nothing) Then Controls.Remove NewTextBox Set NewTextBox = Nothing End If End SubPrivate Sub NewTextBox_Change() Debug.Print NewTextBox.Text End Sub
以下代码调试通过 Dim WithEvents TextBox1 As TextBox '拖动位置 Const HTCAPTION = 2 Const WM_NCLBUTTONDOWN = &HA1 Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long'改变大小 Private Const GWL_STYLE = (-16) Private Const WS_THICKFRAME = &H40000 Private Const SWP_NOSIZE = &H1 Private Const SWP_NOZORDER = &H4 Private Const SWP_NOMOVE = &H2 Private Const SWP_DRAWFRAME = &H20 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Sub Form_Load() Set TextBox1 = Form1.Controls.Add("VB.textbox", "Textbox1") With Form1!TextBox1 .Visible = True .Width = 2000 .Text = "" End With ControlSize TextBox1, True End Sub '拖动位置 Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Dim ReturnVal As Long X = ReleaseCapture() ReturnVal = SendMessage(Me.Text1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) Print Text1.Left End If End Sub'改变大小 Private Sub ControlSize(ControlName As TextBox, SetTrue As Boolean) Dim dwStyle As Long dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE) If SetTrue Then dwStyle = dwStyle Or WS_THICKFRAME Else dwStyle = dwStyle - WS_THICKFRAME End If dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle) SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME End Sub
修改了一下 Dim WithEvents TextBox1 As TextBox '拖动位置 Const HTCAPTION = 2 Const WM_NCLBUTTONDOWN = &HA1 Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long'改变大小 Private Const GWL_STYLE = (-16) Private Const WS_THICKFRAME = &H40000 Private Const SWP_NOSIZE = &H1 Private Const SWP_NOZORDER = &H4 Private Const SWP_NOMOVE = &H2 Private Const SWP_DRAWFRAME = &H20 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Sub Form_Load() Set TextBox1 = Form1.Controls.Add("VB.textbox", "Textbox1") With Form1!TextBox1 .Visible = True .Width = 2000 .Text = "" End With ControlSize TextBox1, True End Sub '拖动位置 Private Sub TextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Dim ReturnVal As Long X = ReleaseCapture() ReturnVal = SendMessage(Form1!TextBox1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) Print Form1!TextBox1.Left End If End Sub'改变大小 Private Sub ControlSize(ControlName As Control, SetTrue As Boolean) Dim dwStyle As Long dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE) If SetTrue Then dwStyle = dwStyle Or WS_THICKFRAME Else dwStyle = dwStyle - WS_THICKFRAME End If dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle) SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME End Sub
建立在你们的基础上,我自已解决了'拖动位置 Const HTCAPTION = 2 Const WM_NCLBUTTONDOWN = &HA1 Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long '改变大小 Private Const GWL_STYLE = (-16) Private Const WS_THICKFRAME = &H40000 Private Const SWP_NOSIZE = &H1 Private Const SWP_NOZORDER = &H4 Private Const SWP_NOMOVE = &H2 Private Const SWP_DRAWFRAME = &H20 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private WithEvents abc As TextBox '动态添加一个textbox控件 Public i'拖动位置 Private Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Dim ReturnVal As Long X = ReleaseCapture() ReturnVal = SendMessage(Me.Text1(Index).hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) 'Print Text1.Left End If End Sub'改变大小 Private Sub ControlSize(ControlName As Control, SetTrue As Boolean) Dim dwStyle As Long dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE) If SetTrue Then dwStyle = dwStyle Or WS_THICKFRAME Else dwStyle = dwStyle - WS_THICKFRAME End If dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle) SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME End Sub '改变大小 Private Sub Form_Load() 'ControlSize Picture1, True ' ControlSize Text1, True i = 0 End Sub'动态添加一个textbox控件 Private Sub Command2_Click() i = i + 1 Load Text1(i) Text1(i).Visible = True Text1(i).Top = Text1(i - 1).Top + 500 Text1(i).Left = Text1(i - 1).Left + 500 ControlSize Text1(i), True Text1(i) = Text1(i).Index Text1(i).ZOrder 0
窗体上事先就画好一个text 隐藏 当用户“开始”画时再在需要的位置上显示初始大小
Private WithEvents Text1 As TextBox
'Text1 指向最后一个添加的文本框,就是当前要调整的。'Private Sub Command1_Click()
Dim i As Long
i = Me.Controls.Count + 1
Set Text1 = Me.Controls.Add("VB.TextBox", "Text" & i)
Text1.Move 0, 480 * i, 1440, 360
Text1.Visible = True
End Sub
不用数组的话 难道你要写N个鼠标事件?
如果真要用数组 那何必还需重画text?
当然如何你需要不仅仅是最后添加的文本框可拖动,还是改用控件数组。又:如果仅仅是支持移动位置,控件的 DragMode 属性就支持,看我上个帖子回复中的 PictureBox
http://topic.csdn.net/u/20110216/21/c1e30022-0537-4338-aa3d-48e90edd59a6.html
Private WithEvents NewTextBox As TextBox'增加控件
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
'增加新的文本框
Set NewTextBox = Controls.Add("VB.TextBox", "NewText", Me)
'确定新增文本框的位置
NewTextBox.Move Command1.Left + Command1.Width + 120, Command1.Top
NewTextBox.Text = "新建的文本框"
NewTextBox.Visible = True
End If
End Sub
'删除控件(注:只能删除动态增加的控件)
Private Sub Command2_Click()
If Not (NewTextBox Is Nothing) Then
Controls.Remove NewTextBox
Set NewTextBox = Nothing
End If
End SubPrivate Sub NewTextBox_Change()
Debug.Print NewTextBox.Text
End Sub
Dim WithEvents TextBox1 As TextBox
'拖动位置
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long'改变大小
Private Const GWL_STYLE = (-16)
Private Const WS_THICKFRAME = &H40000
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOMOVE = &H2
Private Const SWP_DRAWFRAME = &H20
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Sub Form_Load()
Set TextBox1 = Form1.Controls.Add("VB.textbox", "Textbox1")
With Form1!TextBox1
.Visible = True
.Width = 2000
.Text = ""
End With
ControlSize TextBox1, True
End Sub
'拖动位置
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
X = ReleaseCapture()
ReturnVal = SendMessage(Me.Text1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
Print Text1.Left
End If
End Sub'改变大小
Private Sub ControlSize(ControlName As TextBox, SetTrue As Boolean)
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_THICKFRAME
Else
dwStyle = dwStyle - WS_THICKFRAME
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
Dim WithEvents TextBox1 As TextBox
'拖动位置
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long'改变大小
Private Const GWL_STYLE = (-16)
Private Const WS_THICKFRAME = &H40000
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOMOVE = &H2
Private Const SWP_DRAWFRAME = &H20
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Sub Form_Load()
Set TextBox1 = Form1.Controls.Add("VB.textbox", "Textbox1")
With Form1!TextBox1
.Visible = True
.Width = 2000
.Text = ""
End With
ControlSize TextBox1, True
End Sub
'拖动位置
Private Sub TextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
X = ReleaseCapture()
ReturnVal = SendMessage(Form1!TextBox1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
Print Form1!TextBox1.Left
End If
End Sub'改变大小
Private Sub ControlSize(ControlName As Control, SetTrue As Boolean)
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_THICKFRAME
Else
dwStyle = dwStyle - WS_THICKFRAME
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
'改变大小
Private Const GWL_STYLE = (-16)
Private Const WS_THICKFRAME = &H40000
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOMOVE = &H2
Private Const SWP_DRAWFRAME = &H20
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private WithEvents abc As TextBox '动态添加一个textbox控件
Public i'拖动位置
Private Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
X = ReleaseCapture()
ReturnVal = SendMessage(Me.Text1(Index).hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
'Print Text1.Left
End If
End Sub'改变大小
Private Sub ControlSize(ControlName As Control, SetTrue As Boolean)
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_THICKFRAME
Else
dwStyle = dwStyle - WS_THICKFRAME
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'改变大小
Private Sub Form_Load()
'ControlSize Picture1, True
' ControlSize Text1, True
i = 0
End Sub'动态添加一个textbox控件
Private Sub Command2_Click()
i = i + 1
Load Text1(i)
Text1(i).Visible = True
Text1(i).Top = Text1(i - 1).Top + 500
Text1(i).Left = Text1(i - 1).Left + 500
ControlSize Text1(i), True
Text1(i) = Text1(i).Index
Text1(i).ZOrder 0
End Sub
dbcontrols 居然得40分 严重关切是否有一腿 哼哼