Create tooltip from system tray only application

So, I'm trying to create a tooltip at some point on the screen.

ToolTip tip = new ToolTip(); tip.Show("foobar", **IWin32Window window**, new Point(100, 100)) 

The problem is that I do not know what to insert as the window parameter in the above. My application completely exits the system tray and does not have other GUI elements. It is called notifyIcon1 . This is created through Form1 . None of these values ​​work when connected to tip.Show ().

How to create a tooltip anywhere on the screen using only the system tray?

Thanks.

+2
source share
2 answers

The IWin32Window interface is a simple interface that provides only the IntPtr property named Handle . Perhaps something like this should work:

 using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace SO_ToolTip { public partial class Form1 : Form { [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { WindowWrapper windowWrapper = new WindowWrapper(GetDesktopWindow()); ToolTip toolTip = new ToolTip(); toolTip.Show("Blah blah... Blah blah... Blah blah...", windowWrapper, 1, 1, 10000); } } public class WindowWrapper : IWin32Window { public WindowWrapper(IntPtr handle) { Handle = handle; } public IntPtr Handle { get; protected set; } } } 

But this is not so. It complains about a NullReferenceException, and I haven't debugged it yet. It works:

 ... private void button1_Click(object sender, EventArgs e) { ToolTip toolTip = new ToolTip(); toolTip.Show("Blah blah... Blah blah... Blah blah...", this, 1, 1, 10000); } ... 

Although the position refers to the current form. Perhaps this will help you move in the right direction.

Edit: Even this does not work, so I'm not sure if this is a problem with WindowWrapper (how?) Or what:

 ... private void button1_Click(object sender, EventArgs e) { WindowWrapper windowWrapper = new WindowWrapper(this.Handle); ToolTip toolTip = new ToolTip(); toolTip.Show("Blah blah... Blah blah... Blah blah...", windowWrapper, 1, 1, 10000); } ... 

Here you go, use the transparent, maximized form that you BringToFront() before showing the ToolTip

Form Code:

 using System; using System.Windows.Forms; namespace SO_ToolTip { public partial class Form1 : Form { Random _Random = new Random(); ToolTip _ToolTip = new ToolTip(); public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { BringToFront(); _ToolTip.Show("Blah blah... Blah blah... Blah blah...", this, _Random.Next(0, Width), _Random.Next(0, Height), 10000); } } } 

Constructor Code Form1:. You can see the properties of the forms:

 namespace SO_ToolTip { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // timer1 // this.timer1.Enabled = true; this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 264); this.ControlBox = false; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.Opacity = 0; this.ShowIcon = false; this.ShowInTaskbar = false; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Timer timer1; } } 
+2
source

Joining the party:

If you prefer / should have a WPF window:

  private class ToolTipWPFWindow : Window { private readonly TextBlock m_txtToDisplay = new TextBlock(); private readonly DispatcherTimer m_timer = new DispatcherTimer(); public ToolTipWindow(string p_strStringToDisplay, int p_intXOnScreen = 0, int p_intYOnScreen = 0, double p_dblDurationInMilliSeconds = 1500) { if (p_intXOnScreen == 0 && p_intYOnScreen == 0) { p_intXOnScreen = System.Windows.Forms.Cursor.Position.X; p_intYOnScreen = System.Windows.Forms.Cursor.Position.Y; } m_txtToDisplay.Text = p_strStringToDisplay; m_txtToDisplay.Margin = new Thickness(3); Background = new SolidColorBrush(Colors.LightGoldenrodYellow); ShowInTaskbar = false; ResizeMode = System.Windows.ResizeMode.NoResize; Topmost = true; // Location on screen - As Set WindowStartupLocation = WindowStartupLocation.Manual; Left = p_intXOnScreen; Top = p_intYOnScreen; WindowStyle = WindowStyle.None; SizeToContent = SizeToContent.WidthAndHeight; Content = m_txtToDisplay; m_timer.Interval = TimeSpan.FromMilliseconds(p_dblDurationInMilliSeconds); m_timer.Tick += timer_Tick; m_timer.Start(); } private void timer_Tick(object sender, EventArgs e) { if (m_timer != null) { m_timer.Stop(); m_timer.Tick -= timer_Tick; } Close(); } 

Using:

 // Display the ToolTip Window to the right of the Cursor int intX = Cursor.Position.X + 20; int intY = Cursor.Position.Y; ToolTipWindow wpfWindow = new ToolTipWindow("Text To Display", intX, intY, 800); wpfWindow.Show(); 

Result:

wpf tooltip contains "Text To Display"

I did not implement the Mouse leave event since I used a short display duration.

+1
source

Source: https://habr.com/ru/post/1300356/


All Articles