有关C#开发窗体程序,在窗体运行后用鼠标可以拖动控件方法,具体问题在问题补充,求大神解答
发布网友
发布时间:2022-04-26 21:53
我来回答
共1个回答
热心网友
时间:2022-04-26 23:22
C# 不太熟悉 也就是语法问题 我这里用VB.net 做的 算是给你一个思路吧 只完成了你的功能 具体的还要你自己做了
Public Class PointMove
Private FormParent As Form
Private Mycontrols As New List(Of Control)
Sub New(ByRef p As Form)
FormParent = p
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
LoadControls()
' 在 InitializeComponent() 调用之后添加任何初始化。
End Sub
Sub LoadControls()
Me.Controls.Clear()
Me.Size = FormParent.Size
For Each i As Control In FormParent.Controls
Dim t As Control
t = Activator.CreateInstance(i.GetType)
t.Visible = i.Visible
t.Location = i.Location
t.Size = i.Size
t.Text = i.Text
t.Tag = i
AddHandler t.Click, AddressOf Obj_Click
AddHandler t.MouseDown, AddressOf Obj_MouseDown
AddHandler t.MouseMove, AddressOf Obj_MouseMove
AddHandler t.MouseUp, AddressOf Obj_MouseUp
Me.Controls.Add(t)
Next
End Sub
Private Sub Obj_Click(ByVal sender As Control, ByVal e As EventArgs)
Dim tem As Control = Mycontrols.Find(Function(value As Control) sender.Equals(value))
If tem Is Nothing Then
Mycontrols.Add(sender)
sender.BackColor = Color.Blue
Else
Mycontrols.Remove(tem)
tem.BackColor = tem.Tag.BackColor
End If
End Sub
Private Sub PointMove_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
For Each i As Control In Me.Controls
i.Tag.Location = i.Location
Next
End Sub
Dim k As Control, p As Point, b As Boolean
Private Sub Obj_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
k = Mycontrols.Find(Function(value As Control) sender.Equals(value))
If k IsNot Nothing Then
p = e.Location
b = True
End If
End Sub
Private Sub Obj_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If b = True Then
For Each i As Control In Mycontrols
i.Location = New Point(i.Left + e.X - p.X, i.Top + e.Y - p.Y)
Next
End If
End Sub
Private Sub Obj_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
b = False
k = Nothing
End Sub
End Class