第一部分
对于您问题的第一部分,@RezaAghaei 先生发布的解决方法here 可能是答案。这是 VB.NET 版本:
Public Class YourParentForm
'Or drop one in the designer...
Private ReadOnly ContainerPanel As Panel
Sub New()
InitializeComponent()
IsMdiContainer = False
ContainerPanel = New Panel With {.Dock = DockStyle.Fill}
Controls.Add(ContainerPanel)
End Sub
End Class
以及创建子表单的位置:
Dim f As New ChildForm With
{
.FormBorderStyle = FormBorderStyle.None,
.TopLevel = False,
.ControlBox = False,
.Dock = DockStyle.Fill,
}
ContainerPanel.Controls.Add(f)
f.Show()
由于最后插入到集合中的控件首先停靠,因此您需要显式设置控件的顺序以避免任何可能的停靠重叠。因此,正如您在评论中注意到并提到的那样,Dock.Fill 表单与Dock.Top 标签重叠。要解决这个问题:
Dim f As New ChildForm With
{
.FormBorderStyle = FormBorderStyle.None,
.TopLevel = False,
.ControlBox = False,
.Dock = DockStyle.Fill
}
ContainerPanel.Controls.Add(f)
Controls.SetChildIndex(ContainerPanel, 0)
Controls.SetChildIndex(Label1, 1)
f.Show()
其实你可以省略Controls.SetChildIndex(Label1, 1),只是为了澄清思路而添加的。
如果您使用设计器添加ContainerPanel,选择它并右键单击,您将在上下文菜单中看到Bring to Front 和Send to Back,它们用于执行相同的操作。此外,您可以使用 Document Outline 窗口(Ctrl + T 显示它)来更改顺序控件使用上/下箭头和左/右箭头将控件移动到不同的容器。
第二部分
至于第二部分,需要新建一个继承ProfessionalColorTable的类,并覆盖ToolStripBorder属性,防止它在渲染ToolStrip时返回默认颜色:
Public Class CustomColorTable
Inherits ProfessionalColorTable
Public Overrides ReadOnly Property ToolStripBorder As Color
Get
Return Color.Empty
End Get
End Property
End Class
然后,将自定义颜色表传递给ToolStripProfessionalRenderer 类的新实例,并将其分配给ToolStrip.Renderer 或ToolStripManager.Renderer 属性。
重访父Form的构造函数:
Sub New()
InitializeComponent()
ContainerPanel = New Panel With {.Dock = DockStyle.Fill}
Controls.Add(ContainerPanel)
ToolStrip1.Renderer = New ToolStripProfessionalRenderer(New CustomColorTable) With {
.RoundedEdges = False
}
'Or
'ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomColorTable) With {
' .RoundedEdges = False
'}
End Sub