本文介绍了将按钮添加到VB.NET&标题栏中的标题栏(如Firefox 4) XP或Vista / 7上的WPF或WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以在vb.net的标题栏中添加一个按钮 - 我找到了一种方法来做到这一点,但它可以做到这两个?
下面是禁用Aero时的工作代码(导入System.RunTime.InteropServices):
Private Const WM_NCPAINT As Integer = 133
$ p $但是,当Aero被禁用时,按钮不会出现,尽管如果点击该区域它仍然会触发一个事件。
Private Const WM_NCMOUSEMOVE As Integer = 160
Private Const WM_NCLBUTTONDOWN As Integer = 161
Private Const WM_NCLBUTTONUP As Integer = 162
Private Const WM_NCLBUTTONDBLCLK As Integer = 163
Private Const WM_NCACTIVATE As Integer = 134
Private Const WM_NCMOUSELEAVE As Integer = 674
Private TitleBarButtonRectangle As Rectangle
Private TitleBarButtonState As ButtonState
私人TitleBarMouseIsDown为布尔
私人枚举ButtonState作为字节
正常
热
按
结束枚举
受保护的覆盖子WndProc(ByRef m As System.Windows.Forms.Me ssage)
'在这里处理非客户区消息。
选择大小写m.Msg
大小写WM_NCPAINT,WM_NCACTIVATE,WM_NCMOUSELEAVE
'处理消息并重置< strong class =highlight>按钮< /强>到正常状态
'并绘制它。这将绘制最初的< strong class =highlight>按钮< / strong>并重新绘制
'如果标题栏需要更新油漆。
MyBase.WndProc(m)
TitleBarButtonState = ButtonState.Normal
TitleBarMouseIsDown = False
DrawButton(TitleBarButtonState)
Case WM_NCMOUSEMOVE
'The鼠标正在移动到非客户区域。
如果TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam)))然后
'鼠标位于< strong class =highlight>按钮< / strong>
'如果鼠标已经关闭,那么就按下它,如果不是已经关闭的话
'如果鼠标没有关闭,那么把它拉如果不是已经是
If TitleBarMouseIsDown Then
如果TitleBarButtonState< > ButtonState.Pressed Then
TitleBarButtonState = ButtonState.Pressed
DrawButton(TitleBarButtonState)
End If
其他
如果TitleBarButtonState<> ButtonState.Hot然后
TitleBarButtonState = ButtonState.Hot
DrawButton(TitleBarButtonState)
结束如果
结束如果
其他
'鼠标是不在< strong class =高亮>按钮< / strong> ;.
'当鼠标移动到< strong class =highlight>按钮< / strong>时,它将被重新按下。
'否则,如果它还没有绘制正常。
如果TitleBarMouseIsDown Then
如果TitleBarButtonState<> ButtonState.Hot Then
TitleBarButtonState = ButtonState.Hot
DrawButton(TitleBarButtonState)
End If
Else
如果TitleBarButtonState<> ButtonState.Normal Then
TitleBarButtonState = ButtonState.Normal
DrawButton(TitleBarButtonState)
End If
End If
End If
'当鼠标关闭并离开窗口
'时,它将保持热或按下状态。当
'鼠标放在窗口外面并移动时,
'会收到一条消息来重画。
'处理所有的鼠标移动。
MyBase.WndProc(m)
Case WM_NCLBUTTONDOWN
'鼠标左键< strong class =highlight>按钮< / strong>已经在非客户区域被扣下
'如果鼠标位于< strong class =highlight>按钮< / strong>然后将其状态更改为按
'并重新绘制。保存鼠标关闭。
如果TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam)))然后
TitleBarButtonState = ButtonState.Pressed
DrawButton(TitleBarButtonState)
TitleBarMouseIsDown = True
否
'会处理在非客户端
'区域中点击的消息,但不会在< strong class =highlight>按钮< / strong>中进行处理。
MyBase.WndProc(m)
结束如果
案例WM_NCLBUTTONUP
鼠标在非客户区被释放
如果TitleBarMouseIsDown AndAlso TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam)))然后
'鼠标已关闭,鼠标位于< strong class =highlight>按钮< / strong> 。
'已被按下。
TitleBarButtonState = ButtonState.Hot
DrawButton(TitleBarButtonState)
MsgBox(标题栏按钮被点击)
其他
'处理消息并重置< strong class =highlight>按钮< / strong>如果还没有正常的话。
MyBase.WndProc(m)
如果TitleBarButtonState<> ButtonState.Normal然后
TitleBarButtonState = ButtonState.Normal
DrawButton(TitleBarButtonState)
结束如果
结束如果
'重置鼠标下降
TitleBarMouseIsDown = False
Case WM_NCLBUTTONDBLCLK
'鼠标在非客户区被双击。
'如果光标位于< strong class =highlight>按钮< / strong>那么不要处理双击
'如果处理了,表单将改变窗口状态。
如果不是TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam)))Then
MyBase.WndProc(m)
End If
Case else
'处理常规信息
MyBase.WndProc(m)
结束选择
结束小组
私有函数GetNonClientPoint(ByVal lParam As Integer)As
'lparam中的lo字是x坐标,hi字是y。
'坐标在屏幕坐标中,所以减去
'表单的位置以获取< / strong>的位置< strong class =highlight>标题栏。这将不得不调整。
'< / strong>< / strong>会有< strong class =highlight> mdi表单。
Dim ScreenPoint作为新的点(CInt(lParam和Short.MaxValue),CInt(lParam>> 16))
返回ScreenPoint - CType(Me.Location,Size)
End Function
Private Sub DrawButton(ByVal buttonState As ButtonState)
'获取整个窗口的图形。
'绘制视觉风格< strong class =高亮>按钮< / strong> ;.
'在这里绘制自己的图形。
'如果使用视觉样式渲染器,那么如果渲染器不是
',则需要
'任何替代绘图模式。 ControlPaint会绘制< strong class =highlight>按钮< / strong>没有
的视觉风格。
DimStrFrmt As New StringFormat
StrFrmt.LineAlignment = StringAlignment.Center
StrFrmt.Alignment = StringAlignment.Center
StrFrmt.FormatFlags = StringFormatFlags.NoWrap
StrFrmt.Trimming = StringTrimming.EllipsisCharacter
Select Case buttonState
Case Form1.ButtonState .Normal
If VisualStyles.VisualStyleRenderer.IsSupported Then
Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Normal)
VisualRenderer.DrawBackground(EntireWindowGraphics,TitleBarButtonRectangle)
另外
ControlPaint.DrawButton(EntireWindowGraphics,TitleBarButtonRectangle,Windows.Forms.ButtonState.Normal)
End If
Case Form1.ButtonState.Hot
如果VisualS tyles.VisualStyleRenderer.IsSupported Then
Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Hot)
VisualRenderer.DrawBackground(EntireWindowGraphics,TitleBarButtonRectangle)
其他
ControlPaint .DrawButton(EntireWindowGraphics,TitleBarButtonRectangle,Windows.Forms.ButtonState.Normal)
End If
Case Form1.ButtonState.Pressed
如果VisualStyles.VisualStyleRenderer.IsSupported那么
Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Pressed)
VisualRenderer.DrawBackground(EntireWindowGraphics,TitleBarButtonRectangle)
Else
ControlPaint.DrawButton(EntireWindowGraphics,TitleBarButtonRectangle,Windows.Forms .ButtonState.Pushed)
End If
End Sele ct
EntireWindowGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
EntireWindowGraphics.DrawString(Click Me,Me.Font,Brushes.Black,TitleBarButtonRectangle,StrFrmt)
EntireWindowGraphics .Dispose()
StrFrmt.Dispose()
结束Sub
Private Sub SizeButton()
'这是硬编码的一个相当大的3D窗口边界。
'systemInformation类的其他属性可以为固定边界和单边界调整
'。
'SystemInformation.BorderSize
'SystemInformation.FixedFrameBorderSize
'如果窗口被最大化,那么它的尺寸边框将不会显示
',所以y被设置为边框和< strong class =高亮>按钮的高度< / strong>
'被设置为标题高度。如果其未被最大化,则将y设置为
'0,并且将< strong class =highlight>按钮< / strong>高度设置为标题高度加边框尺寸。
如果Me.WindowState = FormWindowState.Normal然后
TitleBarButtonRectangle.Y = 0
TitleBarButtonRectangle.Height = SystemInformation.CaptionHeight + SystemInformation.HorizontalResizeBorderThickness
其他
TitleBarButtonRectangle.Y = SystemInformation.HorizontalResizeBorderThickness
TitleBarButtonRectangle.Height = SystemInformation.CaptionHeight
End If
End Sub
$ b Private Sub Form1_SizeChanged(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.SizeChanged
'更改< strong class =highlight>按钮< / strong>的大小。
'可能已被最大化或标准化
SizeButton()
End Sub
'获取整个窗口的图形。
< DllImport(user32.dll)>公共共享函数GetWindowDC(ByVal hWnd As IntPtr)作为IntPtr
结束函数
私人小组Form1_Load(BYVAL发件人作为对象,BYVAL e作为System.EventArgs)处理Me.Load
TitleBarButtonRectangle = New Rectangle(30,0,75,25)
SizeButton()
End Sub
任何人都有一个想法?
解决方案轻松使用
I was wondering if it is possible to add a button to the title bar in vb.net - I found a way to do this in one or the other but is it possible to do it on both?
Here is the code that works if Aero is disabled (imports System.RunTime.InteropServices):
Private Const WM_NCPAINT As Integer = 133 Private Const WM_NCMOUSEMOVE As Integer = 160 Private Const WM_NCLBUTTONDOWN As Integer = 161 Private Const WM_NCLBUTTONUP As Integer = 162 Private Const WM_NCLBUTTONDBLCLK As Integer = 163 Private Const WM_NCACTIVATE As Integer = 134 Private Const WM_NCMOUSELEAVE As Integer = 674 Private TitleBarButtonRectangle As Rectangle Private TitleBarButtonState As ButtonState Private TitleBarMouseIsDown As Boolean Private Enum ButtonState As Byte Normal Hot Pressed End Enum Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 'Process the non client area messages here. Select Case m.Msg Case WM_NCPAINT, WM_NCACTIVATE, WM_NCMOUSELEAVE 'process the message and reset the <strong class="highlight">button</strong> to the normal state 'and draw it. This will draw the initial <strong class="highlight">button</strong> and repaint it 'if the title bar needs a paint update. MyBase.WndProc(m) TitleBarButtonState = ButtonState.Normal TitleBarMouseIsDown = False DrawButton(TitleBarButtonState) Case WM_NCMOUSEMOVE 'The mouse is moving in the non client area. If TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam))) Then 'the mouse is in the <strong class="highlight">button</strong> 'If the mouse is down then draw it pressed if not already 'If the mouse is not down then draw it hot if not already If TitleBarMouseIsDown Then If TitleBarButtonState <> ButtonState.Pressed Then TitleBarButtonState = ButtonState.Pressed DrawButton(TitleBarButtonState) End If Else If TitleBarButtonState <> ButtonState.Hot Then TitleBarButtonState = ButtonState.Hot DrawButton(TitleBarButtonState) End If End If Else 'The mouse is not in the <strong class="highlight">button</strong>. 'If the mouse is down then draw it hot, when the 'mouse moves back into the <strong class="highlight">button</strong> it will be redrawn pressed. 'Otherwise draw it normal if its not already. If TitleBarMouseIsDown Then If TitleBarButtonState <> ButtonState.Hot Then TitleBarButtonState = ButtonState.Hot DrawButton(TitleBarButtonState) End If Else If TitleBarButtonState <> ButtonState.Normal Then TitleBarButtonState = ButtonState.Normal DrawButton(TitleBarButtonState) End If End If End If 'When the mouse is down and it leaves the window 'it will remain in its hot or pressed state. when the 'mouse is let up outside the window and moved it will 'recieve a message to repaint. 'Process all the mouse moves. MyBase.WndProc(m) Case WM_NCLBUTTONDOWN 'Left mouse <strong class="highlight">button</strong> has been held down in the non client area 'If the mouse is inside the <strong class="highlight">button</strong> then change its state to pressed 'and redraw it. Store that the mouse is down. If TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam))) Then TitleBarButtonState = ButtonState.Pressed DrawButton(TitleBarButtonState) TitleBarMouseIsDown = True Else 'process the message it was clicked somewhere in the non client 'area but not in the <strong class="highlight">button</strong>. MyBase.WndProc(m) End If Case WM_NCLBUTTONUP 'Mouse is being released in the non client area If TitleBarMouseIsDown AndAlso TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam))) Then 'The mouse was down and the mouse is in the <strong class="highlight">button</strong>. 'It has been pressed. TitleBarButtonState = ButtonState.Hot DrawButton(TitleBarButtonState) MsgBox("The Titlebar button was clicked") Else 'Process the message and reset the <strong class="highlight">button</strong> to normal if its not already. MyBase.WndProc(m) If TitleBarButtonState <> ButtonState.Normal Then TitleBarButtonState = ButtonState.Normal DrawButton(TitleBarButtonState) End If End If 'Reset that the mouse is down TitleBarMouseIsDown = False Case WM_NCLBUTTONDBLCLK 'Mouse has been double clicked in the non client area. 'If the cursor is in the <strong class="highlight">button</strong> then don't process the double click 'If processed the form will change window states. If Not TitleBarButtonRectangle.Contains(GetNonClientPoint(CInt(m.LParam))) Then MyBase.WndProc(m) End If Case Else 'Process the regular messages MyBase.WndProc(m) End Select End Sub Private Function GetNonClientPoint(ByVal lParam As Integer) As Point 'The lo word in the lparam is the x coordinate and the hi word is the y. 'The coordinates are in screen coordinates so subtract the location of the 'form to get the location <strong class="highlight">on</strong> the title bar. This will have to be adjusted. 'There will be issues <strong class="highlight">on</strong> mdi forms. Dim ScreenPoint As New Point(CInt(lParam And Short.MaxValue), CInt(lParam >> 16)) Return ScreenPoint - CType(Me.Location, Size) End Function Private Sub DrawButton(ByVal buttonState As ButtonState) 'Gets the graphics to the entire window. 'Draws a visual style <strong class="highlight">button</strong>. 'Draw your own graphics here. 'If using the visual style renderer then you will need 'any alternate drawing mode in case the renderer is not 'supported. ControlPaint will draw a <strong class="highlight">button</strong> without 'visual styles. Dim EntireWindowGraphics As Graphics = Graphics.FromHdc(GetWindowDC(Me.Handle)) Dim StrFrmt As New StringFormat StrFrmt.LineAlignment = StringAlignment.Center StrFrmt.Alignment = StringAlignment.Center StrFrmt.FormatFlags = StringFormatFlags.NoWrap StrFrmt.Trimming = StringTrimming.EllipsisCharacter Select Case buttonState Case Form1.ButtonState.Normal If VisualStyles.VisualStyleRenderer.IsSupported Then Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Normal) VisualRenderer.DrawBackground(EntireWindowGraphics, TitleBarButtonRectangle) Else ControlPaint.DrawButton(EntireWindowGraphics, TitleBarButtonRectangle, Windows.Forms.ButtonState.Normal) End If Case Form1.ButtonState.Hot If VisualStyles.VisualStyleRenderer.IsSupported Then Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Hot) VisualRenderer.DrawBackground(EntireWindowGraphics, TitleBarButtonRectangle) Else ControlPaint.DrawButton(EntireWindowGraphics, TitleBarButtonRectangle, Windows.Forms.ButtonState.Normal) End If Case Form1.ButtonState.Pressed If VisualStyles.VisualStyleRenderer.IsSupported Then Dim VisualRenderer As New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Button.PushButton.Pressed) VisualRenderer.DrawBackground(EntireWindowGraphics, TitleBarButtonRectangle) Else ControlPaint.DrawButton(EntireWindowGraphics, TitleBarButtonRectangle, Windows.Forms.ButtonState.Pushed) End If End Select EntireWindowGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit EntireWindowGraphics.DrawString("Click Me", Me.Font, Brushes.Black, TitleBarButtonRectangle, StrFrmt) EntireWindowGraphics.Dispose() StrFrmt.Dispose() End Sub Private Sub SizeButton() 'This is hard coded for a sizable 3d window border. 'Other properties of the systemInformation class can 'adjust for fixed and single borders. 'SystemInformation.BorderSize 'SystemInformation.FixedFrameBorderSize 'if the window is maximized then its sizing border will not be 'displayed so the y is set to the bordersize and the height of the <strong class="highlight">button</strong> 'is set to the caption height. If its not maximized the y is set to '0 and the <strong class="highlight">button</strong> height is set to the caption height plus the border size. If Me.WindowState = FormWindowState.Normal Then TitleBarButtonRectangle.Y = 0 TitleBarButtonRectangle.Height = SystemInformation.CaptionHeight + SystemInformation.HorizontalResizeBorderThickness Else TitleBarButtonRectangle.Y = SystemInformation.HorizontalResizeBorderThickness TitleBarButtonRectangle.Height = SystemInformation.CaptionHeight End If End Sub Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged 'Change the size of the <strong class="highlight">button</strong>. 'May have been maximized or normalized SizeButton() End Sub 'Gets the graphics to the entire window. <DllImport("user32.dll")> Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr End Function Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TitleBarButtonRectangle = New Rectangle(30, 0, 75, 25) SizeButton() End Sub
But when Aero is disabled, the button doesn't appear, although it still fires an event if the area is clicked.
Anyone have an idea?
解决方案You can handle the task easily with the WPF Shell Integration Library
这篇关于将按钮添加到VB.NET&标题栏中的标题栏(如Firefox 4) XP或Vista / 7上的WPF或WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!