从系统托盘唯一的应用程序创建工具提示 [英] Creating a tooltip from a system-tray only app

查看:177
本文介绍了从系统托盘唯一的应用程序创建工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图创建在屏幕上某一点提示。

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

现在的问题是,我不知道该怎么插入的窗口参数在上面。我的应用程序运行完全脱离了系统托盘中,并没有其他GUI元素。这就是所谓的将NotifyIcon1 。那就是通过 Form1中创建。插入到tip.Show()的时候,这些都不值的工作。

The problem is I don't know what to insert as the window parameter in the above. My app runs entirely out of the system tray, and has no other GUI elements. It's called notifyIcon1. That is created through Form1. Neither of these values work when plugged in to tip.Show().

我如何可以生成一个提示我的屏幕上的任何地方只使用系统盘?

How can I generate a tooltip anywhere on my screen using only the system tray?

感谢。

推荐答案

该IWin32Window接口是一个简单的接口,仅提供了一个的IntPtr 命名属性处理。切实这样的事情应该工作:

The IWin32Window interface is a simple interface that only provides a IntPtr property named Handle. Feasibly 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; }
    }
}



但事实并非如此。它抱怨一个NullReferenceException,我还没有进一步的调试。这并不工作:

But it doesn't. It complains about a NullReferenceException and I haven't debugged further. This does work:

...
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 is relative to the current form. Maybe that will get you going in the right direction.

修改均匀,这并不工作,所以我不知道这是否是一个问题与WindowWrapper还是什么(如何?):

Even this doesn't work so I'm not sure if it's an issue 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);
}
...






在这里,你走了,使用透明,最大化表单,您 BringToFront()显示工具提示之前


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

Form 1代码:

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



Form1的设计器代码所以,你可以看到表格属性:

Form1 Designer Code: So you can see the forms properties:

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;

    }
}

这篇关于从系统托盘唯一的应用程序创建工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆