2010-02-04 65 views
4

我正在試圖製作一個類似於拼寫檢查器的東西,該列表將在當前插入符號位置下列出可能的單詞。我認爲我會通過創建工具提示,根據插入符的位置移動它,並更改工具提示內的文本來實現此目的。沒有鏈接到任何特定控件的工具提示

我有問題。

我想,以顯示與tip.Show(form, x, y);

然而,這個程序從系統托盤運行的提示。除此之外,它沒有GUI元素?我作爲form參數使用什麼? notifyIcon1Form1等不起作用。

我會從一個顯示靜態工具提示的例子開始,這個工具提示隨着我的鼠標光標或其他東西一起移動。有人能指引我朝着正確的方向嗎?

感謝

+0

我認爲這WPF的ToolTip可能是可能的,但它似乎更難。 :-( – itsho 2014-01-07 13:57:10

回答

1

我張貼的答案在這個線程採用透明,最大化的模擬屏幕上的任意位置繪製工具提示,包括桌面。也許這將幫助:Creating a tooltip from a system-tray only app

編輯:從鏈接後在複製的代碼便於閱讀:-)

在這裏你去,使用透明,最大化的形式,你BringToFront()顯示前在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的設計器代碼:所以你可以看到表格屬性:

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; 

    } 
} 

更新:隨着ControlBox = false;Opacity = 0;形式不僅是視覺上透明的,但都難用戶輸入。即使當上面的Form1點擊它的區域中最頂部的窗口落入下一個窗口/桌面時也是如此。就好像表單不在那裏一樣。顯示工具提示之前的BringToFront()是必需的,否則工具提示可能會在其他窗口下繪製,這不是您想要的。

1

您可能能夠做到這一點,但沒有使用的工具提示類的那是相當的限制,有一個名爲VXPLib一個奇妙的工具提示幫助,使用HTML格式(我想會給你的詞彙列表邊緣 - 用不同的顏色表示)。 VXPLib是一個COM對象(用C++編寫),但是可以從.NET語言訪問,並且有一個包裝器可以爲你做的代碼示例。我已經嘗試過,他們實際上工作,並使它看起來不錯...請參閱here瞭解更多信息。

希望這會有所幫助, 最好的問候, 湯姆。

+0

謝謝,看起來真的很酷。不幸的是,我不能通過它提到的regsvr32.exe註冊.dll文件;也許我會試着讓它工作在2.0版本上。標準工具提示? – cksubs 2010-02-05 00:42:00

1

如果應用程序中沒有GUI,那麼在哪個應用程序中提供拼寫檢查程序?

如果要將應用程序與其他現有應用程序(即使是非.NET應用程序)集成,則需要獲取其他應用程序的句柄(HWND)並將其轉換爲System.Windows.Forms.IWin32Window。一旦你這樣做,你可以使用ToolTip.Show方法中的form

以下是你需要的代碼:

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

我一直在創建一個「未鏈接到任何特定控件」的工具提示,因爲我想替換使用ToolTip命令的一個AutoHotkey腳本。

我已經保存在我的代碼:https://bitbucket.org/tahir-hassan/dotnettooltip

所有你要做的是,實例化控件,設置顯示文本,設置的座標,並調用Show方法:

var tooltip = new ToolTipLib.ToolTip() 
{ 
    Text = "this is a nice toolTip", 
    LocationX = 100, 
    LocationY = 200 
}; 

tooltip.Show();