66703

How do I add a mouse over tooltip to an Image using .DrawImage()

Question:

Hey all, I am not sure if this is possible, but I am trying to dynamically add a tooltip to an image using the Graphics method - DrawImage. I dont see any properties or events for when the image is moused over or anything so I don't know where to begin. I am using WinForms (in C# - .NET 3.5). Any ideas or suggestions would be appreciated. Thanks.

Answer1:

I would guess that you have some sort of UserControl and you call DrawImage() in the OnPaint method.

Given that, your tooltip will have to controlled explicitly. Basically, create a Tooltip on your Form, give that to your control via a property, show the tooltip when your control received a MouseHover event and hide the tooltip when you receive a MouseLeave event.

Something like this:

public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // draw image here } public ToolTip ToolTip { get; set; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); if (this.ToolTip != null) this.ToolTip.Hide(this); } protected override void OnMouseHover(EventArgs e) { base.OnMouseHover(e); if (this.ToolTip == null) return; Point pt = this.PointToClient(Cursor.Position); String msg = this.CalculateMsgAt(pt); if (String.IsNullOrEmpty(msg)) return; pt.Y += 20; this.ToolTip.Show(msg, this, pt); } private string CalculateMsgAt(Point pt) { // Calculate the message that should be shown // when the mouse is at thegiven point return "This is a tooltip"; } }

Answer2:

Remember, you have to <strong>store bounds</strong> of the Image that you are drawing and in the mouseMove event check if the location of current Mouse cursor at that region, then display ToolTip else hide it.

ToolTip t; private void Form1_Load(object sender, EventArgs e) { t = new ToolTip(); //tooltip to control on which you are drawing your Image } Rectangle rect; //to store the bounds of your Image private void Panel1_Paint(object sender, PaintEventArgs e) { rect =new Rectangle(50,50,200,200); // setting bounds to rect to draw image e.Graphics.DrawImage(yourImage,rect); //draw your Image } private void Panel1_MouseMove(object sender, MouseEventArgs e) { if (rect.Contains(e.Location)) //checking cursor Location if inside the rect { t.SetToolTip(Panel1, "Hello");//setting tooltip to Panel1 } else { t.Hide(Panel1); //hiding tooltip if the cursor outside the rect } }

Recommend

  • How to fix this error? Invalid variance: The type parameter 'T' must be invariantly valid
  • How to upgrade from V11 to V12 SQL Azure?
  • When is an object eligible for garbage collection? [duplicate]
  • How to disable the “Optional used as field or parameter type” warning in IntelliJ? [duplicate]
  • sql server back restore - login failed
  • Jenkins Composite baseline with ClearCase
  • Test an application behind a proxy server using Robot Framework and Selenium2Library
  • How to customize marker colors and shapes in scatter plot?
  • how java graphics repaint method actually works
  • Display standard razor/mvc 3 validation messages displayed in another language
  • How to use mixed C++ & .Net dll in node.js? (Error: abort() has been called)
  • Oracle BI Publisher - How to format numbers as text so that leading zeroes don't dissappear
  • Working with codeception and laravel
  • why 'read' command in shell script is missing initial characters? [duplicate]
  • Laravel 5 - Cache remember doesn't work
  • Execute powershell script on a remote computer using C#
  • Extracting a small subset of data from XMLs
  • Use AutoIt with java applications
  • AWS RDS Parameter Group not changing MySQL encoding
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • What does “T extends Junk” mean in a generic class in Java?
  • Swift: UIView.animate works unexpectedly
  • Silverlight Event Log in Isolated Storage
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • how do i compare two rows and store the similarities of the two rows in another column
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • How to call jQuery function in HTML returned by AJAX
  • Android: Unable to detect vertical plane
  • How to warp text around image in iOS?
  • ssh remote server login script