代码之家  ›  专栏  ›  技术社区  ›  Mike

从子控件中检测窗体移动事件

  •  2
  • Mike  · 技术社区  · 14 年前

    我正在创建一个用户控件,当用户单击一个按钮时,弹出窗口将显示信息。弹出窗口是由toolStripDropDown驱动的,所以当它出现时,它会做两件事

    1. 不移动窗体上的其他控件,但在其上显示
    2. 它可以在用户控件本身的边界之外显示细节,而不必提前预留空间

    这里有一些代码

    Public Class Popup
    Private treeViewHost As ToolStripControlHost
    Private Shadows dropDown As ToolStripDropDown
    
    Public Sub New()
        InitializeComponent()
        Dim treeView As New TreeView()
    
        treeView.BorderStyle = BorderStyle.None
        treeViewHost = New ToolStripControlHost(treeView)
        treeViewHost.Padding = New Padding(6)
    
        dropDown = New ToolStripDropDown()
        dropDown.AutoClose = False
        dropDown.AutoSize = True
        dropDown.BackColor = Color.LemonChiffon
        dropDown.Items.Add(treeViewHost)
    End Sub
    
    Public Sub ShowDropDown()      
        If dropDown.Visible = False Then
            dropDown.Show(Me, Button1.Left + Button1.Height + 5, Button1.Top)
        Else
            dropDown.Close()
        End If
    
    End Sub    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowDropDown()
    End Sub
    
    Private Sub Popup_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move, Button1.Move
        If (dropDown IsNot Nothing AndAlso Button1 IsNot Nothing AndAlso dropDown.Visible) Then
            dropDown.Left = Button1.Left + Button1.Height + 5
            dropDown.Top = Button1.Top
        End If
    End Sub
    End Class
    

    这是控件的初始化

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(4, 4)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(27, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Popup
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.Button1)
        Me.Name = "Popup"
        Me.Size = New System.Drawing.Size(39, 35)
        Me.ResumeLayout(False)
    
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    

    现在我的问题是,当窗体移动或调整大小时,工具下拉列表不会相对移动。我明白。当我试图捕获用户控件的移动事件时,当整个窗体移动时,该事件不会触发。必须有一些我可以捕获的东西,因为表单容器中的控件是相对移动的,是什么驱动了它?我试过wndproc,但是在移动表单的过程中,除非重新绘制表单,否则什么都不会触发。

    谢谢你

    目前的代码是在VB,但我可以处理这两个。

    4 回复  |  直到 14 年前
        1
  •  2
  •   Patrick    14 年前

    在C#:

    移动和调整事件大小:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move.aspx

    只需添加你的.Designer.cs

    this.Move +=new System.EventHandler(Form_Changed);
    this.Resize += new System.EventHandler(Form_Changed);
    

        2
  •  2
  •   x77    14 年前

    您可以使用订阅父事件

    但你是在父母改变的事情上做的。请注意,在将控件添加到其他控件或窗体之前,父级什么都不是。

    Private Sub GantCtl_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
       Dim mParent = DirectCast(Parent, Form)
       AddHandler mParent.KeyDown, AddressOf Parent_KeyDown
       AddHandler mParent.MouseWheel, AddressOf Parent_MouseWheel
       AddHandler mParent.Resize, AddressOf parent_Resize
    End Sub
    
        3
  •  1
  •   Mike    14 年前

    我想是这个干的

     AddHandler Me.ParentForm.Move, AddressOf Popup_Move
    

    在加载事件中。我不得不在load事件中这样做,因为ParentForm在此之前不可用。我也尝试了ParentChange事件,但是如果我在一个小组中,它就不起作用了

    Me.Parent would equal Panel but
    Me.Parent.Parent would equal nothing (also .ParentForm was Nothing)
    
        4
  •  1
  •   AndyPerfect    14 年前

    'Function within the form that is being moved and governs the location of the ToolStrip
    Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        'lastLocation is a global variable that is reset every time the form is moved
        Dim offsetX As Integer = lastLocationX.X - Location.X
        Dim offsetY As Integer = lastLocationX.Y - Location.Y
        ToolStrip1.show(lastXcoord - offsetX, lastYcoord - offsetY)
        lastLocation = Location
    End Sub