在标题栏中添加自定义按钮 VB.NET

发布于 2024-11-10 09:55:56 字数 273 浏览 7 评论 0原文

我只是想知道是否有可能使用 VB.NET 将自定义按钮添加到标题栏中。我在 Stack Overflow 上看到过很多这样的问题,但未能得到确定且有效的答案。

谁能帮我解决这个问题吗?我也检查过谷歌和其他网站,但它无法呈现。我希望代码能够在 Windows XP、Windows Vista 和 Windows 7 上运行。

如果您能够提供有效的代码,并且该按钮甚至必须能够接受点击事件并发布,我将感激将其更改为执行某些操作时所使用的形式。

提前致谢

I was just wondering if there was a possible way to add a custom button into the title bar using VB.NET. I've seen many such questions on Stack Overflow but failed to get a sure-shot and a working answer.

Can anyone help me around with this issue? I've checked on Google and other website too but it fails to render. I want the code to work on Windows XP, Windows Vista and Windows 7.

I would be grateful if you will be able to give a working code and the button must even be able to accept click events and post it to the form it is on for some action.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2024-11-17 09:55:56

如果你指的是winforms,我可以想到两种方法来做到这一点:

  • 隐藏标题栏并用你自己的标题栏替换它,我不推荐这样做。
  • 将按钮构建为一个非常小的窗体,每次窗口移动时,您都将其停靠在正确的位置。

If you mean winforms, I can think of two ways to do this:

  • Hide the titlebar and replace it with your own, which I don't recommend.
  • Build the button as a very small form that you keep docked in the correct position every time your window moves.
や莫失莫忘 2024-11-17 09:55:56

以下是一些工作代码的示例:

http://www .dreamincode.net/forums/topic/69215-2008-custom-title-bar/

基本上,您需要创建一个没有边框的表单,然后滚动您自己的“标题栏”,它基本上是位于顶部,您可以根据需要进行自定义。这是一个很难完全正确实施的解决方案,但这可能是实现这一目标的最佳方式。

Here is an example with some working code:

http://www.dreamincode.net/forums/topic/69215-2008-custom-title-bar/

Basically, you need to create a form with no border, then roll your own "Titlebar" which will basically be an area at the top that you can customize however you want. This is a difficult solution to fully implement properly, but it is probably the way that will best accomplish this.

时光是把杀猪刀 2024-11-17 09:55:56

正如 Matthew Scharley 在中所写的那样在这里回答

以下内容适用于XP,我没有方便测试Vista的机器
它,但我认为你的问题源于不正确的 hWnd
不知何故。无论如何,继续处理注释很差的代码。

我认为这在 Vista 和 7 中不会以图形方式显示。Matthew 代码的翻译版本如下:

' 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

As Matthew Scharley writes in his answer here:

The following will work in XP, I have no Vista machine handy to test
it, but I think you're issues are steming from an incorrect hWnd
somehow. Anyway, on with the poorly commented code.

I think this doesn't show up graphically in Vista and 7. The translated version of Matthew's code is as follows:

' 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文