有关C#开发窗体程序,在窗体运行后用鼠标可以拖动控件方法,具体问题在问题补充,求大神解答
具体问题是这样的,一个窗体内有好多不同的控件,设此窗体为窗体1,然后另外一个窗体,暂设为窗体2,在程序运行后点击窗体2一个布局按钮后,窗体1的一部分控件显示到窗体2中,然...
具体问题是这样的,一个窗体内有好多不同的控件,设此窗体为窗体1,然后另外一个窗体,暂设为窗体2,在程序运行后点击窗体2一个布局按钮后,窗体1的一部分控件显示到窗体2中,然后可以在窗体2中对所显示的窗体1的控件位置用鼠标进行布局拖动,拖动后,窗体1的控件位置随着拖动而变化,而单独在窗体1中却不可操作拖动,这该如何实现,而且有时需要同时移动几个不同的控件(这几个控件挨的比较近在一个水平位置)财富就剩15了,全给了,求大神解答
展开
1个回答
展开全部
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询