动态添加控件的模块作好了,左右高宽用数字设置,但用户要求要自己画,怎么在Form上用鼠标画方框,就象在vb环境下用鼠标画框加控件,急!!!
下面代码贴上即可:
Option Explicit
Dim Current_Start_X, Current_End_X, Current_Start_Y, Current_End_Y As Long
Dim PaintNow As Boolean
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = True ' 启动绘图。
Current_Start_X = x
Current_Start_Y = Y
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = False ' 禁止绘图。
Current_End_X = x
Current_End_Y = Y
MsgBox Current_Start_X & "{}" & Current_End_X & "{}" & Current_Start_Y & "{}" & Current_End_Y
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
If PaintNow Then
PSet (x, Y) ' 画一个点。
???????? 画框
End If
End SubPrivate Sub Form_Load()
'Me.MouseIcon = LoadPicture(App.Path & "\rect.cur")
'Me.MousePointer = 99
End Sub
下面代码贴上即可:
Option Explicit
Dim Current_Start_X, Current_End_X, Current_Start_Y, Current_End_Y As Long
Dim PaintNow As Boolean
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = True ' 启动绘图。
Current_Start_X = x
Current_Start_Y = Y
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = False ' 禁止绘图。
Current_End_X = x
Current_End_Y = Y
MsgBox Current_Start_X & "{}" & Current_End_X & "{}" & Current_Start_Y & "{}" & Current_End_Y
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
If PaintNow Then
PSet (x, Y) ' 画一个点。
???????? 画框
End If
End SubPrivate Sub Form_Load()
'Me.MouseIcon = LoadPicture(App.Path & "\rect.cur")
'Me.MousePointer = 99
End Sub
解决方案 »
- 请教关于使用Inet控件FTP上传文件的问题,请高手解答
- 会用Mapx控件的请帮忙一下
- insert语句的问题??请大家帮忙看看
- 系统托盘????????????
- 很弱霸的问题,制作一个按钮!
- 关于ACCESS:有一定的难度?在线等待解决!解决了给200分,决不食言!
- 请教各位:在VB中调用EXCEL后,可否还可以进行查询,查询后单元格的内容是否会不再规则呢?在线等候.谢谢
- VB题库随机选题,卷面生成。
- One more question about Access & VB
- vb60+sp4 sybae11 用ado连接后....
- 我用下列函数读取某个地址网页内容为什么会出现乱码,请问怎样解决 在线
- 模拟键盘按键的问题?
Option Explicit
Dim Current_Start_X, Current_End_X, Current_Start_Y, Current_End_Y As Long
Dim PaintNow, Drow_Control, MouseDown As Boolean
Dim Control As VB.Control
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = True ' 启动绘图。
MouseDown = True
Current_Start_X = x
Current_Start_Y = Y
If Drow_Control Then DrowControl Current_Start_X, Current_Start_Y, "vb.commandbutton", "Add_Command"
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
PaintNow = False ' 禁止绘图。
Drow_Control = False
'Current_End_X = x
'Current_End_Y = Y
Me.MousePointer = 0
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
If PaintNow And Drow_Control Then
Control.Width = x - Current_Start_X
Control.Height = Y - Current_Start_Y
End If
End SubPrivate Sub Form_Load()
Drow_Control = True
Me.MouseIcon = LoadPicture(App.Path & "\rect.cur")
Me.MousePointer = 99
End SubPrivate Sub DrowControl(ByVal Current_Start_X As Long, ByVal Current_Start_Y As Long, ByVal ControlType As String, ByVal ControlName As String)
Set Control = Me.Controls.Add(ControlType, ControlName)
Control.Width = 0
Control.Height = 0
Control.Left = Current_Start_X
Control.Top = Current_Start_Y
Control.Visible = True
If (Left$("VB.commandbutton", 3) = "VB.") Then
CallByName Control, "Caption", VbLet, "ddd"
Else
CallByName Control, "Caption", VbLet, "ddd"
End If
End Sub
Option ExplicitPrivate Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate m_MyRect As RECT
Private m_bBegin As Boolean
Private m_X As Single, m_Y As SinglePrivate Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 500 '鼠标按住0.5秒不动 说明开始画方形
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_X = X
m_Y = Y
Timer1.Enabled = True
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_bBegin Then
If X > m_MyRect.Left Then
m_MyRect.Right = X
Else
m_MyRect.Right = m_MyRect.Left
m_MyRect.Left = X
End If
If Y > m_MyRect.Top Then
m_MyRect.Bottom = Y
Else
m_MyRect.Bottom = m_MyRect.Top
m_MyRect.Top = Y
End If
Me.Cls
Me.Line (m_MyRect.Left, m_MyRect.Top)-(m_MyRect.Right, m_MyRect.Top)
Me.Line (m_MyRect.Left, m_MyRect.Bottom)-(m_MyRect.Right, m_MyRect.Bottom)
Me.Line (m_MyRect.Left, m_MyRect.Top)-(m_MyRect.Left, m_MyRect.Bottom)
Me.Line (m_MyRect.Right, m_MyRect.Top)-(m_MyRect.Right, m_MyRect.Bottom)
m_bBegin = False
Else
If X > m_MyRect.Left And X < m_MyRect.Right _
And Y > m_MyRect.Top And Y < m_MyRect.Bottom Then
Call DoClick
End If
Timer1.Enabled = False
End If
End SubPrivate Function DoClick()
MsgBox "点击到了"
End FunctionPrivate Sub Timer1_Timer()
m_MyRect.Left = m_X
m_MyRect.Top = m_Y
m_bBegin = True
Timer1.Enabled = False
End Sub
谢谢,看下我上面的,如何让鼠标使该控件上能自由移动位置?
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Left = X
Source.Top = Y
End Sub
能够使用callbyname的地方,不是都能够直接引用对象的方法或者属性吗?
请高手指点!
If (Left$("VB.commandbutton", 3) = "VB.") Then
CallByName Control, "Caption", VbLet, "ddd"
Else
CallByName Control, "Caption", VbLet, "ddd"
End If//
这些语句好像没什么用哦 楼主
(Left$("VB.commandbutton", 3) = "VB.") 永远=true 而且 then 跟else 是一样的语句不如直接Control.Caption="ddd"
是的,还没全部做好,要动态添加的控件有n种,这里要传参的,先用command来试,功能作到后再封装,用你的方法可以移动了,但Source.Left = X,Source.Top = Y是鼠标的位置,移动时控件的左上角对应这个位置,move的太难看了,而又不能要求用户把鼠标精确的放在控件的左上角,能否解决?
Command1.Tag = X & "," & Y
End SubPrivate Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Dim xx As Single, yy As Single, strPos() As String, strTag As String
strTag = Source.Tag
If strTag <> "" Then
strPos = Split(strTag, ",")
xx = CSng(strPos(0))
yy = CSng(strPos(1))
End If
Source.Left = X - xx
Source.Top = Y - yy
End Sub