I'm trying to make something like a spellchecker, that will list possible words under the current caret position. I thought I would do this by creating a tooltip, moving it according to the caret's location, and changing the text inside the tooltip.
I'm having problems.
I'm tryi开发者_开发技巧ng to show the tooltip with tip.Show(form, x, y);
However, this app is running from the systray. It has no GUI elements aside from that? What do I use as the form
parameter? the notifyIcon1
, Form1
, etc. do not work.
I would start with an example that displayed a static tooltip that moved along with my mouse cursor or something. Can someone point me in the right direction?
Thanks
You may be able to do this but not using a tooltip class as that is quite limiting, there is a fantastic tooltip helper called VXPLib, using html formatting (which I suppose would give your listing of words an edge - say in different colours). The VXPLib is a COM object (written in C++) but accessible from the .NET language and there is a wrapper that can do it for you along with code samples. I have tried them and they actually work and make it look nice...See here for more information.
Hope this helps, Best regards, Tom.
I posted an answer in this thread that uses a transparent, maximized for to simulate drawing a tooltip anywhere on the screen, including the desktop. Maybe it will help: Creating a tooltip from a system-tray only app
Edit: Copied the code over from the linked post for ease of reading :-)
Here you go, use a transparent, maximized form that you BringToFront()
before showing the ToolTip
Form1 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);
}
}
}
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;
}
}
Update: With ControlBox = false;
and Opacity = 0;
the form is not only visually transparent but is immune to user input. That is even when the Form1
above if the top most window clicking in it's area falls through to the next window/desktop. Just as if the form wasn't there. The BringToFront() before showing the tooltip is required because otherwise the tooltip could be drawn under other windows, which is not what you want.
If there's no GUI in your application, then in what application are you to providing a spell checker?
If you are integrating your application with another existing application (even non-.NET applications), then you need to obtain the handle (HWND) of the other application and convert it to a System.Windows.Forms.IWin32Window. Once you do this, you can use that handle as the form
in the ToolTip.Show method.
Here is the code you need:
using System.Diagnostics;
//...
public class MyWindow : System.Windows.Forms.IWin32Window
{
private IntPtr _hwnd;
public IntPtr Handle
{
get
{
return _hwnd;
}
}
public MyWindow(IntPtr handle)
{
_hwnd = handle;
}
//...
public static MyWindow GetWindowFromName(string processName)
{
Process[] procs = Process.GetProcessesByName(processName);
if (procs.Length != 0)
{
return new MyWindow(procs[0].MainWindowHandle);
}
else
{
throw new ApplicationException(String.Format("{0} is not running", processName));
}
}
}
//...
tip.Show("this worked...", MyWindow.GetWindowFromName("Notepad"), 0, 0, 2000);
I have worked on creating a tooltip that is "not linked to any particular control", because I wanted to replace one of my AutoHotkey scripts which uses the ToolTip
command.
I have my code stored at: https://bitbucket.org/tahir-hassan/dotnettooltip
All you do is, instantiate the control, set the text it displays, set the coordinates, and call Show
method:
var tooltip = new ToolTipLib.ToolTip()
{
Text = "this is a nice toolTip",
LocationX = 100,
LocationY = 200
};
tooltip.Show();
精彩评论