如何在 vb.net 中為 formborder 顏色著色?我想要顏色 formborder 等于背景色 45、66、50。我在下面螢屏截圖中的黃色圓圈中是什么意思?。
注意:我使用 Visual Studio 2010 謝謝羅伊

uj5u.com熱心網友回復:
正如我在評論中提到的,這不是一件小事。它涉及處理原生視窗訊息,如WM_NCPAINT呼叫原生視窗 API 函式,以及繪制標題欄和自己處理不同的情況。
如果您使用無邊框表單并自己顯示自定義標題欄,可能會更容易。作為自定義標題欄的想法,您可以使用 MenuStrip。
stackoverflow里有很多類似的問題,但是我找不到自定義標題欄的例子(特別是在VB.NET中),所以我在這里發布了使用VB.NET的自定義標題欄。
使用 ManuStrip 自定義標題欄
在這個例子中,我創建了一個基于選單條的自定義標題欄類,它支持以下功能:
- 它有關閉、最大化/恢復和最小化按鈕
- 它顯示表單的圖示和文本
- 雙擊標題欄作業,它最大化或恢復表單
- 右鍵單擊標題欄有效并顯示系統選單
- 雙擊圖示有效并關閉表單
- 它是可移動的;您可以通過拖動標題欄來移動表單
我不能說它是一個完整的標題欄,但它是一個很好的概念證明:

您可以根據自己的要求修改外觀和行為。這是代碼:
Imports System.Runtime.InteropServices
Public Class CustomTitleBar
Inherits MenuStrip
Private CloseButton As ToolStripMenuItem
Private MaximizeButton As ToolStripMenuItem
Private MinimizeButton As ToolStripMenuItem
Private IconLabel As ToolStripLabel
Private TextLabel As ToolStripLabel
Public Sub New()
MyBase.New()
CloseButton = New ToolStripMenuItem()
MaximizeButton = New ToolStripMenuItem()
MinimizeButton = New ToolStripMenuItem()
IconLabel = New ToolStripLabel()
TextLabel = New ToolStripLabel()
'IconLabel
IconLabel.Alignment = ToolStripItemAlignment.Left
IconLabel.AutoSize = False
IconLabel.Name = "IconLabel"
IconLabel.Size = New Size(64, 64)
IconLabel.Text = ""
'TextLabel
TextLabel.Alignment = ToolStripItemAlignment.Left
TextLabel.AutoSize = True
TextLabel.Name = "TextLabel"
TextLabel.Text = ""
'CloseButton
CloseButton.Alignment = ToolStripItemAlignment.Right
CloseButton.AutoSize = False
CloseButton.Name = "CloseButton"
CloseButton.Size = New Size(64, 64)
CloseButton.Text = "?"
AddHandler CloseButton.Click, AddressOf CloseButton_Click
'MaximizeButton
MaximizeButton.Alignment = ToolStripItemAlignment.Right
MaximizeButton.AutoSize = False
MaximizeButton.Name = "MaximizeButton"
MaximizeButton.Size = New Size(64, 64)
MaximizeButton.Text = "?"
AddHandler MaximizeButton.Click, AddressOf MaximizeButton_Click
'MinimizeButton
MinimizeButton.Alignment = ToolStripItemAlignment.Right
MinimizeButton.AutoSize = False
MinimizeButton.Name = "MinimizeButton"
MinimizeButton.Size = New Size(64, 64)
MinimizeButton.Text = "―"
AddHandler MinimizeButton.Click, AddressOf MinimizeButton_Click
GripStyle = ToolStripGripStyle.Hidden
Me.Padding = New Padding(1)
ImageScalingSize = New System.Drawing.Size(32, 32)
Me.AutoSize = True
Me.Dock = DockStyle.Top
Me.TabStop=False
Items.Add(IconLabel)
Items.Add(TextLabel)
Items.Add(CloseButton)
Items.Add(MaximizeButton)
Items.Add(MinimizeButton)
End Sub
private sub MinimizeButton_Click(sender As Object, e As EventArgs)
Dim f = FindForm()
If (f Is Nothing) Then Return
f.WindowState = FormWindowState.Minimized
End sub
private sub MaximizeButton_Click(sender As Object, e As EventArgs)
Dim f = FindForm()
If (f Is Nothing) Then Return
If (f.WindowState = FormWindowState.Normal) Then
f.WindowState = FormWindowState.Maximized
ElseIf f.WindowState = FormWindowState.Maximized Then
f.WindowState = FormWindowState.Normal
End If
End sub
private sub CloseButton_Click(sender As Object, e As EventArgs)
Dim f = FindForm()
If (f Is Nothing) Then Return
f.Close()
End sub
Protected Overrides Sub onm ouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
Dim f = FindForm()
If (f Is Nothing) Then Return
If (e.Button = MouseButtons.Left) Then
If (e.Clicks = 1 AndAlso _
e.Location.X < MinimizeButton.Bounds.X AndAlso _
e.Location.X > IconLabel.Bounds.Right) Then
ReleaseCapture()
SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End If
ElseIf (e.Button = MouseButtons.Right) Then
Dim menu = GetSystemMenu(f.Handle, False)
Dim command = TrackPopupMenu(menu, _
TPM_RETURNCMD TPM_LEFTBUTTON TPM_RIGHTBUTTON, _
MousePosition.X, MousePosition.Y, IntPtr.Zero, _
f.Handle, IntPtr.Zero)
If (command > 0) Then
SendMessage(f.Handle, _
WM_SYSCOMMAND, command, IntPtr.Zero)
End If
End If
End Sub
Protected Overrides Sub onm ouseDoubleClick(e As MouseEventArgs)
MyBase.OnMouseDoubleClick(e)
Dim f = FindForm()
If (f Is Nothing) Then Return
If (e.X < IconLabel.Bounds.Right) Then
f.Close()
ElseIf (f.WindowState = FormWindowState.Normal) Then
f.WindowState = FormWindowState.Maximized
ElseIf f.WindowState = FormWindowState.Maximized Then
f.WindowState = FormWindowState.Normal
End If
End Sub
Protected Overrides Sub OnParentChanged(e As EventArgs)
MyBase.OnParentChanged(e)
Dim f = FindForm()
If (f Is Nothing) Then Return
UpdateIcon()
UpdateText()
AddHandler f.TextChanged, AddressOf Parent_TextChanged
End Sub
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
UpdateIcon()
UpdateText()
End Sub
Private sub Parent_TextChanged(sender As Object, e As EventArgs)
UpdateText()
End sub
Private sub UpdateIcon()
Dim f = FindForm()
If (f Is Nothing) Then Return
If(f.Icon IsNot Nothing)
IconLabel.Image = f.Icon.ToBitmap()
End If
End sub
Private sub UpdateText()
Dim f = FindForm()
If (f Is Nothing) Then Return
TextLabel.Text = f.Text
End sub
Private Const TPM_LEFTBUTTON As Integer = &H0
Private Const TPM_RIGHTBUTTON As Integer = &H2
Private Const TPM_RETURNCMD As Integer = &H100
Private Const WM_SYSCOMMAND As Integer = &H112
<DllImport("user32.dll")> _
Private Shared Function GetSystemMenu(hWnd As IntPtr, _
bRevert As Boolean) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function TrackPopupMenu( _
hMenu As IntPtr, uFlags As Integer, _
x As Integer, y As Integer, nReserved As Integer, _
hWnd As IntPtr, prcRect As IntPtr) As Integer
End Function
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Private Const HT_CAPTION As Integer = &H2
<DllImport("User32")> _
Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, _
wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("User32")> _
Private Shared Function ReleaseCapture() As Boolean
End Function
End Class
構建專案后,您可以將 CustomTitleBar 的實體拖放到表單中。要支持顯示系統背景關系選單,您需要在表單中添加以下代碼:
Public Class Form1
Private Const WS_SYSMENU As Integer = &H80000
Private Const WS_MINIMIZEBOX As Integer = &H20000
Private Const WS_MAXIMIZEBOX As Integer = &H10000
Protected Overrides ReadOnly Property CreateParams _
As System.Windows.Forms.CreateParams
Get
Dim p = MyBase.CreateParams
p.Style = WS_SYSMENU WS_MINIMIZEBOX WS_MAXIMIZEBOX
Return p
End Get
End Property
End Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/398333.html
上一篇:組合框Winforms
