WPF manual Tooltip or Popup takes over mouse capture

R

ronscottlangham

I am wanting to create my own custom tooltip that I have control
over. Basically, I want to tooltip how soon it comes up and how long
it stays up as long as over the control. I know that in .NET 3.5 they
finally added the TooltipService that allows you this control, but
still using .NET 3.0. On my controls of interest, I was taking over
the MouseEnter and MouseLeave and then attempting to open a Tooltip or
Popup.

StaysOpen is false, and I set the IsOpen=true to show. I have tried
using both Tooltip and Popup controls and have similar problem.
Problem is that when I set the IsOpen=true on the tooltip/popup, that
they take over the mouse capture. My original control gets the
MouseLeave event and the TT/popup gets the MouseEnter. If I move my
mouse over another control, I would like these to close, but since the
TT/popup has taken over mouse capture, then nothing I can do bug re-
click again back on the main window.

I would like to emulate how the current WPF tooltip works, or extend
it if possible. i.e.g it pops up but does not affect the mouse
capture, other controls on the form still get MouseEnter/Leave events.
 
J

Jay Dee

Why can’t you just build one based on a form, something like this?
Then if you needed it to be re-usable create a wrapper class for it
that derives from System.ComponentModel.Component.
Might not be any use it’s just a suggestion.

using System;
using System.Drawing;
using System.Windows.Forms;
namespace ToolTip
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
private Button button1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(82, 68);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.MouseLeave += new System.EventHandler
(this.button1_MouseLeave);
this.button1.MouseMove += new MouseEventHandler
(this.button1_MouseMove);
this.button1.MouseEnter += new System.EventHandler
(this.button1_MouseEnter);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
ToolTipForm toolTip = null;
private void button1_MouseEnter(object sender, EventArgs e)
{
toolTip = new ToolTipForm();
toolTip.Text = "Click Me";
toolTip.Show();
this.Focus();
toolTip.Location = new Point(Cursor.Position.X + 10,
Cursor.Position.Y);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
toolTip.Close();
toolTip = null;
}
private void button1_MouseMove(object sender, MouseEventArgs
e)
{
toolTip.Location = new Point(Cursor.Position.X + 10,
Cursor.Position.Y);
}
}
public partial class ToolTipForm : Form
{
public ToolTipForm()
{
this.InitializeComponent();
this.TopMost = true;
this.ShowInTaskbar = false;
}
public override string Text
{
get
{
if (this.label1 == null) return null;
return this.label1.Text;
}
set { this.label1.Text = value; }
}
private Label label1;
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// ToolTipForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode =
System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(293, 167);
this.Controls.Add(this.label1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.None;
this.Name = "ToolTipForm";
this.Text = "ToolTipForm";
this.ResumeLayout(false);
this.PerformLayout();

}
}
}
 
R

ronscottlangham

Why can’t you just build one based on a form, something like this?
Then if you needed it to be re-usable create a wrapper class for it

Something I considered, except using Window since WPF app. But,
figured I should be able to use existing Tooltip or Popup. I tried
something similar to what you did above by setting the focus
immediately after showing the popup, but this did not work. It
appears that the Popup takes over the mouse capture and accepts all
input regardless of focus.

Well, after some more research I found out that the TooltipService is
available under 3.0, so this solves most of my problems. Though, it
seems that the ToolTipService.BetweenShowDelay doesn't do what I
really expected. I have tooltips for items in a listbox that does
require some time to retrieve. One issue that I was having is that
when moving the mouse down over items, it would try to retrieve the
tooltips for almost each row in the list as I was just moving the
mouse down. I set the BetweenShowDelay to 0 to try to force the
tooltip to always use my InitialShowDelay, but this doesn't seem to
work.

Thanks for your input, I think I am mostly ok now, I am able to have
some more control over the tooltips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top