How to make a floating (tooltip) control in Windows.Forms?

后端 未结 1 870
渐次进展
渐次进展 2021-01-13 18:18

The Scene: A (smallish) Form hosting a UserControl.

The Plot: Whenever UserControl raises a hover event, display some (graphical) information in a

相关标签:
1条回答
  • 2021-01-13 18:25

    Your code has a serious problem, it adds a control to the user control every time the mouse hovers but never removes them.

    First, make very sure that the built-in ToolTip component doesn't already solve your problem. It should, it behaves the way your describe. Note that it has the OwnerDraw property, it allows you to customize its appearance.

    Creating your own is tricky. A tool tip is a fairly unusual window, it isn't a child control like all other WF controls. It is a top-level window, which allows it to overlap other windows and extend past the client area of the container window. The only class in Windows Forms that behaves this way is the Form class. Using a borderless form to implement your custom tool tip is possible.

    The trickiest part is ensuring that it moves when the parent form of your user control moves. You'll have to iterate the Parent property of the UC until you find a Form, then subscribe the LocationChanged, VisibleChanged and FormClosing events. You'll also should wire the UC's ParentChanged and HandleDestroyed events.

    0 讨论(0)
提交回复
热议问题