日历

« 2009-01-06  
    123
45678910
11121314151617
18192021222324
25262728293031

RSS订阅

欢迎与编程爱好者交流!

申精:运行时移动控件和调节控件大小

2008-05-28 12:01:28

有很多问了这个问题,我今天给他整理了下,顺便给出了两个控件在运行时的控制:

Option Explicit

Dim oldX As Single '保存点击时X的坐标
Dim oldY As Single '保存点击时Y的坐标
Dim PublicControl As Control

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

Private Sub Command1_GotFocus()
    Set PublicControl = Command1
    Control_Gotfocus Command1
End Sub

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Control_Move PublicControl, X, Y, Button
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Control_MoveUp PublicControl, X, Y, Button
    Command1_GotFocus
End Sub

Private Sub Pic1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'左上角
Private Sub Pic1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic1.MousePointer = 8
    Pic1_Move PublicControl, X, Y, Button
End Sub

Private Sub Pic2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'上下调整
Private Sub Pic2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic2.MousePointer = 7
    Pic2_Move PublicControl, Y, Button
End Sub

Private Sub Pic3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'左右调整
Private Sub Pic3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic3.MousePointer = 6
    Pic3_Move PublicControl, X, Y, Button
End Sub

Private Sub Pic4_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'左右调整
Private Sub Pic4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic4.MousePointer = 9
    Pic4_Move PublicControl, X, Button
End Sub

Private Sub Pic5_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'左右移动
Private Sub Pic5_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic5.MousePointer = 9
    Pic5_Move PublicControl, X, Button
End Sub

Private Sub Pic6_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'左下角
Private Sub Pic6_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic6.MousePointer = 6
    Pic6_Move PublicControl, X, Y, Button
End Sub

Private Sub Pic7_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'上下调整
Private Sub Pic7_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic7.MousePointer = 7
    Pic7_Move PublicControl, Y, Button
End Sub

Private Sub Pic8_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

'右下角
Private Sub Pic8_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Pic8.MousePointer = 8
    Pic8_Move PublicControl, X, Y, Button
End Sub

Private Sub Text1_GotFocus()
     Set PublicControl = Text1
     Control_Gotfocus Text1
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ōldX = X
    ōldY = Y
End Sub

Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Control_Move PublicControl, X, Y, Button
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Control_MoveUp PublicControl, X, Y, Button
    Text1_GotFocus
End Sub

'控件获得焦点
Private Sub Control_Gotfocus(ByVal Controlname As Control)
    Pic1.Move Controlname.Left - Pic1.Width / 2, Controlname.Top - Pic1.Height / 2
    Pic2.Move Controlname.Left + Controlname.Width / 2 - Pic2.Width / 2, Controlname.Top - Pic2.Height / 2
    Pic3.Move Controlname.Left + Controlname.Width - Pic3.Width / 2, Controlname.Top - Pic3.Height / 2
    Pic4.Move Controlname.Left - Pic4.Width / 2, Controlname.Top + Controlname.Height / 2 - Pic4.Height / 2
    Pic5.Move Controlname.Left + Controlname.Width - Pic5.Width / 2, Controlname.Top + Controlname.Height / 2 - Pic5.Height / 2
    Pic6.Move Controlname.Left - Pic6.Width / 2, Controlname.Top + Controlname.Height - Pic6.Height / 2
    Pic7.Move Controlname.Left + Controlname.Width / 2 - Pic7.Width / 2, Controlname.Top + Controlname.Height - Pic7.Height / 2
    Pic8.Move Controlname.Left + Controlname.Width - Pic8.Width / 2, Controlname.Top + Controlname.Height - Pic8.Height / 2
End Sub

Private Sub Pic1_Move(ByVal Controlname As Control, ByVal X As Single, ByVal Y As Single, Button As Integer)
    If Controlname.Height - Y + oldY <= 280 Or Controlname.Width + oldX - X <= 280 Then Exit Sub
    If Button = 1 Then
        Controlname.Move Controlname.Left + X - oldX, Controlname.Top + Y - oldY, Controlname.Width - (X - oldX), Controlname.Height - (Y - oldY)
        Pic1.Move Pic1.Left + X - oldX, Pic1.Top + Y - oldY
        Pic2.Move Pic1.Left + Controlname.Width / 2, Pic1.Top
        Pic3.Move Pic3.Left, Pic1.Top
        Pic4.Move Pic1.Left, Pic1.Top + Controlname.Height / 2
        Pic5.Move Pic5.Left, Pic4.Top
        Pic6.Move Pic1.Left, Pic6.Top
        Pic7.Move Pic2.Left, Pic7.Top
    End If
End Sub

Public Sub Pic2_Move(ByVal Controlname As Control, ByVal Y As Single, Button As Integer)
    If Controlname.Height - Y + oldY <= 280 Then Exit Sub
    If Button = 1 Then
        Controlname.Move Controlname.Left, Controlname.Top + Y - oldY, Controlname.Width, Controlname.Height - (Y - oldY)
        Pic2.Move Pic2.Left, Pic2.Top + Y - oldY
        Pic1.Move Pic1.Left, Pic2.Top
        Pic3.Move Pic3.Left, Pic2.Top
        Pic4.Move Pic4.Left, Controlname.Top + Controlname.Height / 2 - Pic4.Height / 2
        Pic5.Move Pic5.Left, Controlname.Top + Controlname.Height / 2 - Pic5.Height / 2
    End If
End Sub

Private Sub Pic3_Move(ByVal Controlname As Control, ByVal X As Single, ByVal Y As Single, Button As Integer)
    If Controlname.Height - Y + oldY <= 280 Or Controlname.Width - oldX + X <= 280 Then Exit Sub
    If Button = 1 Then
        Controlname.Move Controlname.Left, Controlname.Top + Y - oldY, Controlname.Width + (X - oldX), Controlname.Height - (Y - oldY)
        Pic3.Move Pic3.Left + X - oldX, Pic3.Top + Y - oldY
        Pic1.Move Pic1.Left, Pic3.Top
        Pic2.Move Pic1.Left + Controlname.Width / 2, Pic3.Top
        Pic4.Move Pic4.Left, Pic1.Top + Controlname.Height / 2
        Pic5.Move Pic3.Left, Pic4.Top
        Pic7.Move Pic2.Left
        Pic8.Move Pic3.Left
    End If
End Sub

TAG:

wuguangliang发布于2008-12-22 01:34:36
不错,
bushiniao0发布于2008-12-09 17:43:11
谢谢,
soid发布于2008-11-27 00:34:45
能不能按ctrl键按比例缩放呢?
拖动拐角时能不能按ctrl键按比例缩放呢?
soid发布于2008-11-27 00:22:33
能不能按ctrl键按比例缩放呢?
拖动拐角时能不能按ctrl键按比例缩放呢?
acai发布于2008-11-12 20:00:06
   有好东西的地方都先留个记号,以备后用
abc40发布于2008-11-01 07:02:32

xmt459111274发布于2008-10-18 18:12:44
太 弓虽 了!!!!!      


收!
lppop发布于2008-10-17 15:39:27
怎么还下不了啊。。
zss427607发布于2008-10-10 19:57:17
学习了
nzgok88发布于2008-09-16 20:57:05
看看,已经做了。
yidie的个人空间 yidie 发布于2008-09-09 14:30:35
不错,pic1~pic7改成数组更好些,代码量可大大缩小
tan126tan发布于2008-09-08 16:44:35
不错~~~  我也写了一个 就是移动的时候使用 API,不过能不使用就不使用哈~~ 其它几个也收藏了~
cq110发布于2008-09-04 11:53:02


cq110发布于2008-09-04 11:43:26

cq110发布于2008-09-04 10:25:33
[img]../
cq110发布于2008-09-03 09:50:56
好利害的人呀,
daiyufeng的个人空间 daiyufeng 发布于2008-08-30 12:18:41
9楼的只能对有句柄的控件进行拖动,像label,flash好多控件都不行的
我以前遇到过..
dyxg041发布于2008-08-28 11:17:51
多謝共享,借走了
lixingkai0的个人空间 lixingkai0 发布于2008-08-28 10:14:22
不错,偶收了.
阳光宝宝发布于2008-08-27 13:36:29
厉害,我加一个仅仅是移动的

桌面.rar
(2008-08-27 13:36:29, Size: 1.24 kB, Downloads: 78)

我来说两句

(可选)

Open Toolbar