如何解决在标题栏VB.NET中添加自定义按钮
| 我只是想知道是否有可能使用VB.NET将自定义按钮添加到标题栏中。我已经在Stack Overflow上看到了许多这样的问题,但是未能获得肯定的答案和有效的答案。 谁能帮我解决这个问题?我也检查了Google和其他网站,但无法呈现。我希望代码可以在Windows XP,Windows Vista和Windows 7上运行。 如果您能够提供有效的代码,并且该按钮甚至必须能够接受点击事件并将其发布到正在进行的操作中,则将不胜感激。 提前致谢解决方法
如果您指的是Winforms,我可以想到两种方法:
隐藏标题栏,并用您自己的标题栏替换,我不建议这样做。
将按钮构建为一个很小的窗体,以便每次移动窗口时都将其停靠在正确的位置。
, 正如Matthew Scharley在这里的回答中所写:
以下将在XP中工作,我没有方便测试的Vista机器
它,但我认为您的问题是由错误的hWnd引起的
不知何故。无论如何,用评论不佳的代码。
我认为这在Vista和7中不会以图形方式显示。Matthew \'s代码的翻译版本如下:
\' The state of our little button
Private _buttState As ButtonState = ButtonState.Normal
Private _buttPosition As New Rectangle()
<DllImport(\"user32.dll\")> _
Private Shared Function GetWindowDC(hWnd As IntPtr) As IntPtr
End Function
<DllImport(\"user32.dll\")> _
Private Shared Function GetWindowRect(hWnd As IntPtr,ByRef lpRect As Rectangle) As Integer
End Function
<DllImport(\"user32.dll\")> _
Private Shared Function ReleaseDC(hWnd As IntPtr,hDC As IntPtr) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
Dim x As Integer,y As Integer
Dim windowRect As New Rectangle()
GetWindowRect(m.HWnd,windowRect)
Select Case m.Msg
\' WM_NCPAINT
\' WM_PAINT
Case &H85,&Ha
MyBase.WndProc(m)
DrawButton(m.HWnd)
m.Result = IntPtr.Zero
Exit Select
\' WM_ACTIVATE
Case &H86
MyBase.WndProc(m)
DrawButton(m.HWnd)
Exit Select
\' WM_NCMOUSEMOVE
Case &Ha0
\' Extract the least significant 16 bits
x = (CInt(m.LParam) << 16) >> 16
\' Extract the most significant 16 bits
y = CInt(m.LParam) >> 16
x -= windowRect.Left
y -= windowRect.Top
MyBase.WndProc(m)
If Not _buttPosition.Contains(New Point(x,y)) AndAlso _buttState = ButtonState.Pushed Then
_buttState = ButtonState.Normal
DrawButton(m.HWnd)
End If
Exit Select
\' WM_NCLBUTTONDOWN
Case &Ha1
\' Extract the least significant 16 bits
x = (CInt(m.LParam) << 16) >> 16
\' Extract the most significant 16 bits
y = CInt(m.LParam) >> 16
x -= windowRect.Left
y -= windowRect.Top
If _buttPosition.Contains(New Point(x,y)) Then
_buttState = ButtonState.Pushed
DrawButton(m.HWnd)
Else
MyBase.WndProc(m)
End If
Exit Select
\' WM_NCLBUTTONUP
Case &Ha2
\' Extract the least significant 16 bits
x = (CInt(m.LParam) << 16) >> 16
\' Extract the most significant 16 bits
y = CInt(m.LParam) >> 16
x -= windowRect.Left
y -= windowRect.Top
If _buttPosition.Contains(New Point(x,y)) AndAlso _buttState = ButtonState.Pushed Then
_buttState = ButtonState.Normal
\' [[TODO]]: Fire a click event for your button
\' however you want to do it.
DrawButton(m.HWnd)
Else
MyBase.WndProc(m)
End If
Exit Select
\' WM_NCHITTEST
Case &H84
\' Extract the least significant 16 bits
x = (CInt(m.LParam) << 16) >> 16
\' Extract the most significant 16 bits
y = CInt(m.LParam) >> 16
x -= windowRect.Left
y -= windowRect.Top
If _buttPosition.Contains(New Point(x,y)) Then
m.Result = DirectCast(18,IntPtr)
Else
\' HTBORDER
MyBase.WndProc(m)
End If
Exit Select
Case Else
MyBase.WndProc(m)
Exit Select
End Select
End Sub
Private Sub DrawButton(hwnd As IntPtr)
Dim hDC As IntPtr = GetWindowDC(hwnd)
Dim x As Integer,y As Integer
Using g As Graphics = Graphics.FromHdc(hDC)
\' Work out size and positioning
Dim CaptionHeight As Integer = Bounds.Height - ClientRectangle.Height
Dim ButtonSize As Size = SystemInformation.CaptionButtonSize
x = Bounds.Width - 4 * ButtonSize.Width
y = (CaptionHeight - ButtonSize.Height) \\ 2
_buttPosition.Location = New Point(x,y)
\' Work out color
Dim color As Brush
If _buttState = ButtonState.Pushed Then
color = Brushes.LightGreen
Else
color = Brushes.Red
End If
\' Draw our \"button\"
g.FillRectangle(color,x,y,ButtonSize.Width,ButtonSize.Height)
End Using
ReleaseDC(hwnd,hDC)
End Sub
Private Sub Form1_Load(sender As Object,e As EventArgs)
_buttPosition.Size = SystemInformation.CaptionButtonSize
End Sub
, 这是一些工作代码的示例:
http://www.dreamincode.net/forums/topic/69215-2008-custom-title-bar/
基本上,您需要创建一个没有边框的表单,然后滚动自己的\“ Titlebar \\”,这基本上是您可以自定义顶部的区域。这是很难完全正确实施的解决方案,但这可能是最好的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。