gpt4 book ai didi

c# - 从仅系统托盘应用程序创建工具提示

转载 作者:行者123 更新时间:2023-11-30 22:47:28 41 4
gpt4 key购买 nike

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

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

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

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

谢谢。

最佳答案

IWin32Window 接口(interface)是一个简单的接口(interface),它只提供一个名为HandleIntPtr 属性。像这样的事情应该可行:

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,我没有进一步调试。这确实有效:

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

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

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

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

给你,在显示 ToolTip

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

Form1代码:

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 设计器代码:因此您可以看到表单属性:

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;

}
}

关于c# - 从仅系统托盘应用程序创建工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2215840/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com