5
votes

I have this Windows Forms application with a simple balloon tooltip. Depending on the application's window location on the desktop and the mouse cursor location, the balloon 'tip' (or balloon pointing arrow) may or may not be pointing to the location I want.

For instance, my app snaps to the desktop sides and when it's snapped to the right side, if the mouse cursor is below 100px of the right side, the balloon 'tip' will point to the wrong place. But if the mouse cursor is anywhere else, it will point to the right place.

In this situation I wanted to fake the mouse cursor position (without actually changing the mouse cursor position) to be somewhere else so the the problem wouldn't occur.

Is this possible? How can I achieve this?

private void noteTitleInput_KeyPress(object sender, KeyPressEventArgs e) {
    if(e.KeyChar == Convert.ToChar(Keys.Return, CultureInfo.InvariantCulture) && noteTitleInput.Text.Length > 0) {
        e.Handled = true;

        noteInputButton_Click(null, null);
    } else if(!Char.IsControl(e.KeyChar)) {
        if(Array.IndexOf(Path.GetInvalidFileNameChars(), e.KeyChar) > -1) {
            e.Handled = true;

            System.Media.SystemSounds.Beep.Play();

            noteTitleToolTip.Show("The following characters are not valid:\n\\ / : * ? < > |",
                groupNoteInput, 25, -75, 2500);

            return;
        }
    }

    noteTitleToolTip.Hide(groupNoteInput);
}
4

4 Answers

4
votes

I'm not quite sure why do you need to set cursor position, because you can set tool tip to appear where you tell it, and not necessarily where the mouse is.

For example:

tooltip1.Show("My tip", controlOnWhichToShow, 15, 15);

would display the tip at upper left corner of the controlOnWhichToShow, 15 points away from edges.

If I misunderstood you, than please specify at which point in time is the mouse position being used.

3
votes

If you sync the MouseHover event, you can create the Tooltip as veljkoz describes. In this way you can place the tooltip as you like. The code would look smething like this:

protected override void OnMouseHover(EventArgs e)
{
  ToolTip myToolTip = new ToolTip();
  myToolTip.IsBalloon = true;
  // TODO The x and y coordinates should be what ever you wish.
  myToolTip.Show("Helpful Text Also", this, 50, 50);
  base.OnMouseHover(e);
}

Hope that helps.

0
votes

In Windows Forms the mouse is captured by the control when the user presses a mouse button on a control, and the mouse is released by the control when the user releases the mouse button.

The Capture property of the Control class specifies whether a control has captured the mouse. To determine when a control loses mouse capture, handle the MouseCaptureChanged event.

Only the foreground window can capture the mouse. When a background window attempts to capture the mouse, the window receives messages only for mouse events that occur when the mouse pointer is within the visible portion of the window. Also, even if the foreground window has captured the mouse, the user can still click another window, bringing it to the foreground. When the mouse is captured, shortcut keys do not work.

More here. Mouse Capture in Windows Forms

0
votes

You can do what you say with a Class. You can do it in a very simple way.

one create class and

namespace MousLokasyonbulma

{ class benimtooltip : ToolTip { [System.Runtime.InteropServices.DllImport("User32.dll")] static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw); public benimtooltip() { this.OwnerDraw = true; this.Draw += Benimtooltip_Draw; }

    private void Benimtooltip_Draw(object sender, DrawToolTipEventArgs e)
    {
        e.DrawBackground();
        e.DrawBorder();
        e.DrawText();
        var t = (ToolTip)sender;
        var h = t.GetType().GetProperty("Handle",
          System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var handle = (IntPtr)h.GetValue(t);
        var location = new Point(650, 650);
        var ss= MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false);
    }
}

}

full Code MyGithup

Example Project image https://i.hizliresim.com/1pndZG.png https://i.hizliresim.com/Lvo3Rb.png