如何在UserControl中创建部分客户区?

时间:2014-03-08 09:49:38

标签: c# vb.net winforms user-controls

在我之前的问题中,我已经解决了如何创建容器相关控件。现在,我在控制中遇到问题。控制设计有点混乱。设计看起来像这样。

enter image description here

在此设计中,开发人员无法在页面控件之外添加控件。 PageControl将自动添加到客户区。我想阻止用户在PageControl之外添加控件。我不会再混淆了。所以,我的问题是我该怎么做?

enter image description here

您可以在我之前的question

中查看课程声明

如果您有任何其他想法,请建议我。

更新:

控件应如下所示。

enter image description here

但是,在分配非客户区域后,放置的控件进入客户区域。我想阻止用户在该部分添加控件。 enter image description here

1 个答案:

答案 0 :(得分:0)

简单方法

最简单的方法是在父项大小发生变化时重新计算XWizardPage的范围。

Public Class XWizardPage
    Inherits DevExpress.XtraEditors.XtraPanel
    Implements IComparable(Of XWizardPage)

    Protected Overrides Sub OnResize(e As System.EventArgs)
        MyBase.OnResize(e)
        If (Not Me.positioningInternal) Then Me.Position()
    End Sub

    Protected Overrides Sub SetBoundsCore(x As Integer, y As Integer, width As Integer, height As Integer, specified As System.Windows.Forms.BoundsSpecified)
        MyBase.SetBoundsCore(x, y, width, height, specified)
        If (Not Me.positioningInternal) Then Me.Position()
    End Sub

    Protected Overrides Sub OnParentChanged(e As System.EventArgs)
        MyBase.OnParentChanged(e)
        If (Not Me.cachedParent Is Nothing) Then
            RemoveHandler Me.cachedParent.SizeChanged, AddressOf Me.OnParentSizeChanged
        End If
        Me.cachedParent = If(((Not Me.Parent Is Nothing) AndAlso (TypeOf Me.Parent Is XWizardControl)), Me.Parent, Nothing)
        If (Not Me.cachedParent Is Nothing) Then
            AddHandler Me.cachedParent.SizeChanged, AddressOf Me.OnParentSizeChanged
        End If
    End Sub

    Private Sub OnParentSizeChanged(sender As Object, e As EventArgs)
        Me.Position()
    End Sub

    Private Sub Position()
        If (Not Me.cachedParent Is Nothing) Then
            Dim r As Rectangle = Me.cachedParent.ClientRectangle
            r.Y += 10
            r.Height -= (10 * 2)
            If (Me.Bounds <> r) Then
                Me.positioningInternal = True
                Me.Bounds = r
                Me.positioningInternal = False
            End If
        End If
    End Sub

    Private cachedParent As Control
    Private positioningInternal As Boolean

End Class

艰难的方式

困难的方式需要原生呼叫。我无法附加一个完整的解决方案,因为这很复杂,尽管可行。您正在寻找的术语是nonclient area。网上有很多教程,你应该进行搜索。

以下代码只是一个示例,向您展示从哪里开始,并且不是最终解决方案。为了使其更加完美,您需要处理Window redrawMouse events等。

Public Class XWizardControl
    Inherits DevExpress.XtraEditors.XtraUserControl

    <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
    Private Shared Function GetDCEx(ByVal hWnd As IntPtr, ByVal hrgnClip As IntPtr, ByVal flags As Integer) As IntPtr
    End Function

    Private Sub WmNcCalcSize(ByRef m As Message)
        If (m.WParam.ToInt32() = 0) Then
            Dim ncRect As RECT = DirectCast(m.GetLParam(GetType(RECT)), RECT)
            ncRect.top += 10
            ncRect.bottom -= 10
            Marshal.StructureToPtr(ncRect, m.LParam, False)
            m.Result = IntPtr.Zero
        ElseIf (m.WParam.ToInt32() = 1) Then
            Dim ncParams As NCCALCSIZE_PARAMS = DirectCast(m.GetLParam(GetType(NCCALCSIZE_PARAMS)), NCCALCSIZE_PARAMS)
            ncParams.rectProposed.top += 10
            ncParams.rectProposed.bottom -= 10
            Marshal.StructureToPtr(ncParams, m.LParam, False)
            m.Result = IntPtr.Zero
        Else
            MyBase.WndProc(m)
        End If
    End Sub

    Private Sub WmNcPaint(ByRef m As Message)
        Dim hDC As IntPtr = GetDCEx(m.HWnd, m.WParam, (DCX.WINDOW Or DCX.INTERSECTRGN Or DCX.CACHE Or DCX.CLIPSIBLINGS))
        If (hDC.ToInt32() <> 0) Then
            Using g As Graphics = Graphics.FromHdc(hDC)
                g.Clear(Color.Red)
            End Using
            m.Result = IntPtr.Zero
        Else
            MyBase.WndProc(m)
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCCALCSIZE : Me.WmNcCalcSize(m) : Exit Select
            Case WM_NCPAINT : Me.WmNcPaint(m) : Exit Select
            Case Else : MyBase.WndProc(m) : Exit Select
        End Select
    End Sub

    Public Const WM_NCCALCSIZE As Integer = 131
    Public Const WM_NCPAINT As Integer = 133

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure NCCALCSIZE_PARAMS
        Public rectProposed As RECT
        Public rectBeforeMove As RECT
        Public rectClientBeforeMove As RECT
        Public lppos As WINDOWPOS
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure WINDOWPOS
        Public hwnd As IntPtr
        Public hWndInsertAfter As IntPtr
        Public x As Integer
        Public y As Integer
        Public cx As Integer
        Public cy As Integer
        Public flags As UInteger
    End Structure

    Private Enum DCX As Integer
        CACHE = &H2
        CLIPCHILDREN = &H8
        CLIPSIBLINGS = &H10
        EXCLUDERGN = &H40
        EXCLUDEUPDATE = &H100
        INTERSECTRGN = &H80
        INTERSECTUPDATE = &H200
        LOCKWINDOWUPDATE = &H400
        NORECOMPUTE = &H100000
        NORESETATTRS = &H4
        PARENTCLIP = &H20
        VALIDATE = &H200000
        WINDOW = &H1
    End Enum

End Class