简体   繁体   中英

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 "". The FormBorderStyle property is Sizable. 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. 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. 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.

Answers in both VB.NET and C# are very welcome.

This is running in Windows 8.1. It is a WinForms application.

Workaround 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. I achieved this by creating a ghostform with Size 1,1 and then handled the MDIChildActivated event of the main form:

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.

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#

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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