views:

103

answers:

3

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

+1  A: 

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.

tommieb75
Thanks, that looks really cool. Unfortunately I can't register the .dll file via regsvr32.exe that it mentions; maybe I'll try to get it working for version 2.0. Any ideas on which form to pass the standard tooltip?
cksubs
+1  A: 

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);
Gabriel McAdams
+4  A: 

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: http://stackoverflow.com/questions/2215840/creating-a-tooltip-from-a-system-tray-only-app/2215918#2215918

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.

Cory Charlton
Any comment on the downvote or are we just bounty hunting here?
Cory Charlton