简体   繁体   English

Windows 8,不带标题栏的表格,覆盖边框颜色

[英]Windows 8, Form without titlebar, Override border color

I created a form and disabled the titlebar by setting the ControlBox property to false and the Text property to "". 我创建了一个表单,并通过将ControlBox属性设置为false并将Text属性设置为“”来禁用标题栏。 The FormBorderStyle property is Sizable. FormBorderStyle属性是可调整的。 This is the designer code for the form: 这是表单的设计者代码:

'
'frmParameters
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.DeepSkyBlue
Me.ClientSize = New System.Drawing.Size(284, 261)
Me.ControlBox = False
Me.Name = "frmParameters"

The form is created as an MDI-Child. 该表单被创建为MDI-Child。 The following image shows the result (the form is active): 下图显示了结果(表单处于活动状态):

在此处输入图片说明

As you can see, the form has a dark gray border color. 如您所见,表单具有深灰色边框颜色。 This doesn't fit well with my application's theme. 这与我的应用程序主题不太吻合。 I believe that this color is set by the current Windows theme. 我相信这种颜色是由当前Windows主题设置的。 When the form is inactive the border color is light blue. 表单处于非活动状态时,边框颜色为浅蓝色。 Is there any way to override these color settings? 有什么方法可以覆盖这些颜色设置? I can't really draw the border myself because it is outside the client area and I don't want to set the Borderstyle to any fixed state because the form should still be sizable. 我自己不能真正绘制边框,因为它在客户区域之外,并且我不想将Borderstyle设置为任何固定状态,因为该窗体仍应可调整大小。

Answers in both VB.NET and C# are very welcome. 非常欢迎在VB.NET和C#中都回答。

This is running in Windows 8.1. 它正在Windows 8.1中运行。 It is a WinForms application. 这是一个WinForms应用程序。

Workaround 1 解决方法1
Since the non-activated color fits quite nicely in my theme I tried disabling the activation of the child forms in the MDI application. 由于未激活的颜色非常适合我的主题,因此我尝试在MDI应用程序中禁用子窗体的激活。 I achieved this by creating a ghostform with Size 1,1 and then handled the MDIChildActivated event of the main form: 我通过创建大小为1,1的ghostform来实现此目的,然后处理了主窗体的MDIChildActivated事件:

Private Sub frmMain_MdiChildActivate(sender As Object, e As EventArgs) Handles MyBase.MdiChildActivate
    frmGhost.Activate()
End Sub

This will prevent the other forms from being active, but it's a rather dirty hack. 这将阻止其他形式的活动,但这是一个相当肮脏的技巧。 Is there a better way? 有没有更好的办法? Setting the Selectable style doesn't prevent it. 设置Selectable样式不会阻止它。

Googling shows that changing the form border colour is very difficult. 谷歌搜索显示,更改表单边框颜色非常困难。

The best I can offer you is this: 我能为您提供的最好的服务是:

  Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
  End Sub

  Private Sub Form1_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
  End Sub

The border only displays when the form is active. 仅当表单处于活动状态时才显示边框。

What I did now was going with the borderless form and adding the resize capability by hand. 我现在要做的是使用无边界表单,并手动添加了调整大小功能。

Custom Resize Handle in Border-less Form C# 无边框形式C#的自定义调整大小句柄

I won't repeat the whole resizing code, just look at the link. 我不会重复整个调整大小的代码,只看链接即可。

I then set the padding of the form to 5 on each side and draw the border myself by overriding OnPaint. 然后,我将表格的填充设置为每侧5个,并通过覆盖OnPaint自己绘制边框。 This works well enough for my needs. 这足以满足我的需求。

Important steps are to set both DoubleBuffered and ResizeRedraw properties of the form to True to get nice repainting when overriding OnPaint. 重要的步骤是将DoubleBufferedResizeRedraw属性都设置为True,以在重写OnPaint时获得很好的重绘效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM