65.9K
CodeProject 正在变化。 阅读更多。
Home

带有标题、多行内容和图像的工具提示

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (50投票s)

2010 年 8 月 4 日

CPOL

3分钟阅读

viewsIcon

164068

downloadIcon

11510

一个工具提示,用于显示每个控件的单独标题、内容和图像,并支持自定义形状

使用标题、文本和图像的自定义工具提示。

alltooltip.PNG

仅使用文本的自定义工具提示。

contentonlytooltip.PNG

仅使用标题的自定义工具提示。

titleonlytooltip.PNG

仅使用图像的自定义工具提示。

imageonlytooltip.PNG

引言

有很多方法可以显示关于控件的附加信息,包括工具提示。 当我们使用标准工具提示时,工具提示显示为一个带有文本的矩形窗口,并且工具提示经常覆盖主要对象,这会干扰我们的工作,除非您调用工具提示的 Show 方法并提供其相对于主要对象的位置。 主要问题是工具提示始终是一个矩形框,除非工具提示的 IsBallon 属性设置为 true,但我们仍然无法创建具有自定义形状的工具提示。 所以,我需要一个工具提示,它可以显示提供的标题、内容、图像,并默认避免主要对象的区域,可以显示在相对于主要对象的位置,或者在屏幕上的绝对位置,并且可以使用自定义形状创建。

特点

此工具提示将为每个可用的 ControlToolStripItem 提供 ToolTipToolTipTitleToolTipImage 属性。 默认情况下,此工具提示将显示为圆角矩形,包含提供的属性(工具提示(作为主要内容)、标题和图像),避免主要对象的区域,在右侧和底部显示一个小的阴影,并使用淡入淡出效果。

此工具提示提供的属性。

providedproperties.PNG

以下是此工具提示的几个自定义属性

  • AnimationSpeed 属性,用于确定淡入淡出效果所需的时间,以毫秒为单位。
  • AutoClose 属性,确定工具提示将显示的持续时间。 当 EnableAutoClose 属性设置为 True 时,将使用此属性。
  • EnableAutoClose 属性,如果为 True,则在经过 AutoClose 属性提供的时间段后,工具提示将自动关闭;否则,当发生 MouseLeaveMouseDown 事件时,工具提示将关闭。
  • Location 属性,确定工具提示将如何定位。
  • CustomLocation 属性,自定义工具提示的显示位置,当 Location 属性设置为 CustomScreenCustomClient 时使用。
  • OwnerDraw 属性,如果为 true,则工具提示的表面将由您的代码绘制,将引发 PopupDraw 事件;否则,工具提示表面将自行绘制。
  • OwnerDrawBackground 属性,如果为 true,则工具提示的背景和表面都将由您的代码绘制,将引发 PopupDrawBackgroundDraw 事件;否则,工具提示背景将自行绘制。

工具提示属性

tooltipproperties.PNG

ToolTipLocation 枚举,这是 Location 属性的值。

  • Auto,工具提示位置会自动计算在合适的位置并避免主要对象区域。
  • MousePointer,工具提示将位于鼠标指针周围。
  • CustomScreen,工具提示将位于屏幕上 CustomLocation 属性指定的特定位置。
  • CustomClient,工具提示将位于相对于主要对象客户端区域的 CustomLocation 属性指定的特定位置。

它是如何工作的

为了显示工具提示窗口,我创建了一个从 Form 继承的窗口。 此窗口在其扩展窗口样式上具有 WS_EX_LAYERED

' This is how to create a layered window.
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
	Get
		Dim cp As CreateParams = MyBase.CreateParams
		cp.ExStyle = cp.ExStyle Or WS_EX_LAYERED
		Return cp
	End Get
End Property

创建窗口后,我使用 ShowWindowSetWindowPos 函数来显示窗口。

' Prevent window to activate itself.
ShowWindow(Me.Handle, SW_NOACTIVATE)
' Show window at the top most location.
SetWindowPos(Me.Handle, HWND_TOPMOST, Me.Left, Me.Top, Me.Width, Me.Height, _
	SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE)

创建并显示窗口后,现在绘制窗口。 我使用位图作为画布来绘制工具提示,并将位图绘制到屏幕上的窗口边界。

Private Sub setBitmap(ByVal aBmp As Bitmap)
	Dim screenDC As IntPtr = GetDC(IntPtr.Zero)
	Dim memDC As IntPtr = CreateCompatibleDC(screenDC)
	Dim hBitmap As IntPtr = IntPtr.Zero
	Dim oldBitmap As IntPtr = IntPtr.Zero
	Try
		hBitmap = aBmp.GetHbitmap(Color.FromArgb(0))
		oldBitmap = SelectObject(memDC, hBitmap)

		Dim size As Size = New Size(aBmp.Width, aBmp.Height)
		Dim pointSource As Point = New Point(0, 0)
		Dim topPos As Point = New Point(Me.Left, Me.Top)
		Dim blend As BLENDFUNCTION = New BLENDFUNCTION
		blend.BlendOp = AC_SRC_OVER
		blend.BlendFlags = 0
		blend.SourceConstantAlpha = 255
		blend.AlphaFormat = AC_SRC_ALPHA
		UpdateLayeredWindow(Me.Handle, screenDC, topPos, _
			size, memDC, pointSource, 0, blend, ULW_ALPHA)
	Catch ex As Exception
	Finally
		ReleaseDC(IntPtr.Zero, screenDC)
		If hBitmap <> IntPtr.Zero Then
			SelectObject(memDC, oldBitmap)
			DeleteObject(hBitmap)
		End If
		DeleteDC(memDC)
	End Try
End Sub

创建自定义形状的工具提示

如果要创建自定义形状的工具提示,需要将工具提示的 OwnerDrawBackground 属性设置为 True,然后处理工具提示引发的 PopupDrawDrawBackground 事件。

' Assumes you have created a ToolTip object named ToolTip1
' This event fired when the tooltip needs to draw its surface.
' Called when the OwnerDraw or OwnerDrawBackground property is set to true.
Private Sub ToolTip1_Draw(ByVal sender As Object, ByVal e As Ai.Control.DrawEventArgs) _
	Handles ToolTip1.Draw
	Dim strFormat As StringFormat = New StringFormat
	strFormat.LineAlignment = StringAlignment.Center
	strFormat.Alignment = StringAlignment.Center
	e.Graphics.DrawString("This is when OwnerDrawnBackground of _
	the ToolTip is set to True", Me.Font, Brushes.Black, e.Rectangle, strFormat)
End Sub
' This event fired when the tooltip needs to draw its background.
' Called only when OwnerDrawBackground property is set to true.
Private Sub ToolTip1_DrawBackground(ByVal sender As Object, _
	ByVal e As Ai.Control.DrawEventArgs) Handles ToolTip1.DrawBackground
	e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
	e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
	e.Graphics.FillEllipse(Brushes.White, e.Rectangle)
	e.Graphics.DrawEllipse(Pens.Black, e.Rectangle)
End Sub
' This event fired before tooltip is displayed, mainly to provide the tooltip's size.
' Called when the OwnerDraw or OwnerDrawBackground property is set to true.
Private Sub ToolTip1_Popup(ByVal sender As Object, _
	ByVal e As Ai.Control.PopupEventArgs) Handles ToolTip1.Popup
	e.Size = New Size(100, 100)
End Sub

此代码将生成如下工具提示

通过将 ToolTip 的 OwnerDrawBackground 属性设置为 True 来自定义形状的工具提示。

ownerdrawbackground.PNG

历史

  • 2010 年 8 月 3 日:首次发布
© . All rights reserved.