从仅限系统托盘的应用程序创建工具提示

时间:2010-02-07 03:48:44

标签: c# tooltip system-tray

所以我试图在屏幕上的某个位置创建一个工具提示。

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

问题是我不知道在上面插入window参数的内容。我的应用程序完全从系统托盘中运行,并且没有其他GUI元素。它被称为notifyIcon1。这是通过Form1创建的。当插入tip.Show()时,这些值都不起作用。

如何仅使用系统托盘在屏幕上的任何位置生成工具提示?

感谢。

2 个答案:

答案 0 :(得分:2)

IWin32Window接口是一个简单的接口,仅提供名为IntPtr的{​​{1}}属性。可能这样的事情应该有效:

Handle

但事实并非如此。它抱怨NullReferenceException并且我没有进一步调试。这确实有效:

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; }
    }
}

虽然位置是相对于当前形式的。也许这会让你朝着正确的方向前进。

编辑:即使这样也行不通,所以我不确定它是否是WindowWrapper的问题(如何?)或者是什么:

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

在这里,在显示... 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); } ...

之前,使用BringToFront()的透明,最大化的表单

Form1代码:

ToolTip

Form1 Designer代码:因此,您可以看到表单属性:

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);
        }
    }
}

答案 1 :(得分:1)

加入晚会:

如果您更喜欢/需要WPF窗口:

    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();
        }

用法:

// 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();

结果:

wpf tooltip contains "Text To Display"

我没有实现Mouse leave事件,因为我使用了短显示持续时间。