internal static string Get(TipInfo tip, Language lang) { if (dic.ContainsKey(tip) && dic[tip].ContainsKey(lang)) return dic[tip][lang]; Type _enumType = typeof(TipInfo); FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, tip)); LanguageAttribute[] ds = (LanguageAttribute[])fi.GetCustomAttributes(typeof(LanguageAttribute), false); if (ds == null || ds.Length==0) return string.Empty; else { string str = string.Empty; LanguageAttribute la = ds[0]; switch (lang) { case Language.ZH_CN: str = la.ZH_CN; break; case Language.ZH_TW: str = la.ZH_TW; break; case Language.EN: str = la.EN; break; default: break; } if (!dic.ContainsKey(tip)) dic[tip] = new Dictionary<Language, string>(); dic[tip][lang] = str; return str; } }
private void SetTool(IWin32Window win, string text, TipInfo.Type type, Point position) { Control tool = win as Control; if (tool != null && tools.ContainsKey(tool)) { bool allocatedString = false; NativeMethods.TOOLINFO_TOOLTIP ti = new NativeMethods.TOOLINFO_TOOLTIP(); try { ti.cbSize = Marshal.SizeOf(typeof(NativeMethods.TOOLINFO_TOOLTIP)); ti.hwnd = tool.Handle; ti.uId = tool.Handle; int ret = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TTM_GETTOOLINFO, 0, ti); if (ret != 0) { ti.uFlags |= NativeMethods.TTF_TRACK; if (type == TipInfo.Type.Absolute || type == TipInfo.Type.SemiAbsolute) { ti.uFlags |= NativeMethods.TTF_ABSOLUTE; } ti.lpszText = Marshal.StringToHGlobalAuto(text); allocatedString = true; } TipInfo tt = (TipInfo)tools[tool]; if (tt == null) { tt = new TipInfo(text, type); } else { tt.TipType |= type; tt.Caption = text; } tt.Position = position; tools[tool] = tt; UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TTM_SETTOOLINFO, 0, ti); UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TTM_TRACKACTIVATE, 1, ti); } finally { if(allocatedString && IntPtr.Zero != ti.lpszText) { Marshal.FreeHGlobal(ti.lpszText); } } } else { Hide(win); // Need to do this BEFORE we call GetWinTOOLINFO, since it relies on the tools array to be populated // in order to find the toplevelparent. TipInfo tt = (TipInfo)tools[tool]; if (tt == null) { tt = new TipInfo(text, type); } else { tt.TipType |= type; tt.Caption = text; } tt.Position = position; tools[tool] = tt; IntPtr hWnd = Control.GetSafeHandle(win); owners[hWnd] = win; NativeMethods.TOOLINFO_TOOLTIP toolInfo = GetWinTOOLINFO(hWnd); toolInfo.uFlags |= NativeMethods.TTF_TRACK; if (type == TipInfo.Type.Absolute || type == TipInfo.Type.SemiAbsolute) { toolInfo.uFlags |= NativeMethods.TTF_ABSOLUTE; } try { toolInfo.lpszText = Marshal.StringToHGlobalAuto(text); UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TTM_ADDTOOL, 0, toolInfo); UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TTM_TRACKACTIVATE, 1, toolInfo); } finally { if(IntPtr.Zero != toolInfo.lpszText) { Marshal.FreeHGlobal(toolInfo.lpszText); } } } if (tool != null) { // Lets find the Form for associated Control ... // and hook up to the Deactivated event to Hide the Shown tooltip Form baseFrom = tool.FindFormInternal(); if (baseFrom != null) { baseFrom.Deactivate += new EventHandler(this.BaseFormDeactivate); } } }
/// <include file='doc\ToolTip.uex' path='docs/doc[@for="ToolTip.ShowTooltip"]/*' /> /// <devdoc> /// Shows a tooltip for specified text, window, and hotspot /// </devdoc> /// <internalonly/> private void ShowTooltip(string text, IWin32Window win, int duration) { if (win == null) { throw new ArgumentNullException("win"); } Control associatedControl = win as Control; if (associatedControl != null) { NativeMethods.RECT r = new NativeMethods.RECT(); UnsafeNativeMethods.GetWindowRect(new HandleRef(associatedControl, associatedControl.Handle), ref r); Cursor currentCursor = Cursor.CurrentInternal; Point cursorLocation = Cursor.Position; Point p = cursorLocation; Screen screen = Screen.FromPoint(cursorLocation); // Place the tool tip on the associated control if its not already there if ( cursorLocation.X < r.left || cursorLocation.X > r.right || cursorLocation.Y < r.top || cursorLocation.Y > r.bottom ) { // calculate the dimensions of the visible rectangle which // is used to estimate the upper x,y of the tooltip placement NativeMethods.RECT visibleRect = new NativeMethods.RECT(); visibleRect.left = (r.left < screen.WorkingArea.Left) ? screen.WorkingArea.Left:r.left; visibleRect.top = (r.top < screen.WorkingArea.Top) ? screen.WorkingArea.Top:r.top; visibleRect.right = (r.right > screen.WorkingArea.Right) ? screen.WorkingArea.Right:r.right; visibleRect.bottom = (r.bottom > screen.WorkingArea.Bottom) ? screen.WorkingArea.Bottom:r.bottom; p.X = visibleRect.left + (visibleRect.right - visibleRect.left)/2; p.Y = visibleRect.top + (visibleRect.bottom - visibleRect.top)/2; associatedControl.PointToClientInternal(p); SetTrackPosition(p.X, p.Y); SetTool(win, text, TipInfo.Type.SemiAbsolute, p); if (duration > 0) { StartTimer(window, duration); } } else { TipInfo tt = (TipInfo)tools[associatedControl]; if (tt == null) { tt = new TipInfo(text, TipInfo.Type.SemiAbsolute); } else { tt.TipType |= TipInfo.Type.SemiAbsolute; tt.Caption = text; } tt.Position = p; if (duration > 0) { if (this.originalPopupDelay == 0) { this.originalPopupDelay = AutoPopDelay; } AutoPopDelay = duration; } SetToolTipInternal(associatedControl, tt); } } }
/// <include file='doc\ToolTip.uex' path='docs/doc[@for="ToolTip.SetToolTipInternal"]/*' /> /// <devdoc> /// <para> /// Associates <see cref="System.Windows..Forms.ToolTip'/> text with the specified information /// </para> /// </devdoc> private void SetToolTipInternal(Control control, TipInfo info) { // Sanity check the function parameters if (control == null) { throw new ArgumentNullException("control"); } bool exists = false; bool empty = false; if (tools.ContainsKey(control)) { exists = true; } if (info == null || String.IsNullOrEmpty(info.Caption)) { empty = true; } if (exists && empty) { tools.Remove(control); } else if (!empty) { tools[control] = info; } if (!empty && !exists) { control.HandleCreated += new EventHandler(this.HandleCreated); control.HandleDestroyed += new EventHandler(this.HandleDestroyed); if (control.IsHandleCreated) { HandleCreated(control, EventArgs.Empty); } } else { bool handlesCreated = control.IsHandleCreated && TopLevelControl != null && TopLevelControl.IsHandleCreated; if (exists && !empty && handlesCreated && !DesignMode) { bool allocatedString; NativeMethods.TOOLINFO_TOOLTIP toolInfo = GetTOOLINFO(control, info.Caption, out allocatedString); try { UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TTM_SETTOOLINFO, 0, toolInfo); } finally { if(allocatedString && IntPtr.Zero != toolInfo.lpszText) { Marshal.FreeHGlobal(toolInfo.lpszText); } } CheckNativeToolTip(control); CheckCompositeControls(control); } else if (empty && exists && !DesignMode) { control.HandleCreated -= new EventHandler(this.HandleCreated); control.HandleDestroyed -= new EventHandler(this.HandleDestroyed); if (control.IsHandleCreated) { HandleDestroyed(control, EventArgs.Empty); } created.Remove(control); } } }
/// <include file='doc\ToolTip.uex' path='docs/doc[@for="ToolTip.SetToolTip"]/*' /> /// <devdoc> /// <para> /// Associates <see cref='System.Windows.Forms.ToolTip'/> text with the specified control. /// </para> /// </devdoc> public void SetToolTip(Control control, string caption) { TipInfo info = new TipInfo(caption, TipInfo.Type.Auto); SetToolTipInternal(control, info); }
private void SetToolTipInternal(Control control, TipInfo info) { if (control == null) { throw new ArgumentNullException("control"); } bool flag = false; bool flag2 = false; if (this.tools.ContainsKey(control)) { flag = true; } if ((info == null) || string.IsNullOrEmpty(info.Caption)) { flag2 = true; } if (flag && flag2) { this.tools.Remove(control); } else if (!flag2) { this.tools[control] = info; } if (!flag2 && !flag) { control.HandleCreated += new EventHandler(this.HandleCreated); control.HandleDestroyed += new EventHandler(this.HandleDestroyed); if (control.IsHandleCreated) { this.HandleCreated(control, EventArgs.Empty); } } else { bool flag3 = (control.IsHandleCreated && (this.TopLevelControl != null)) && this.TopLevelControl.IsHandleCreated; if ((flag && !flag2) && (flag3 && !base.DesignMode)) { bool flag4; System.Windows.Forms.NativeMethods.TOOLINFO_TOOLTIP lParam = this.GetTOOLINFO(control, info.Caption, out flag4); try { System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), System.Windows.Forms.NativeMethods.TTM_SETTOOLINFO, 0, lParam); } finally { if (flag4 && (IntPtr.Zero != lParam.lpszText)) { Marshal.FreeHGlobal(lParam.lpszText); } } this.CheckNativeToolTip(control); this.CheckCompositeControls(control); } else if ((flag2 && flag) && !base.DesignMode) { control.HandleCreated -= new EventHandler(this.HandleCreated); control.HandleDestroyed -= new EventHandler(this.HandleDestroyed); if (control.IsHandleCreated) { this.HandleDestroyed(control, EventArgs.Empty); } this.created.Remove(control); } } }
private void SetTool(IWin32Window win, string text, TipInfo.Type type, Point position) { Control key = win as Control; if ((key != null) && this.tools.ContainsKey(key)) { bool flag = false; System.Windows.Forms.NativeMethods.TOOLINFO_TOOLTIP lParam = new System.Windows.Forms.NativeMethods.TOOLINFO_TOOLTIP(); try { lParam.cbSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.TOOLINFO_TOOLTIP)); lParam.hwnd = key.Handle; lParam.uId = key.Handle; if (((int) System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), System.Windows.Forms.NativeMethods.TTM_GETTOOLINFO, 0, lParam)) != 0) { lParam.uFlags |= 0x20; if ((type == TipInfo.Type.Absolute) || (type == TipInfo.Type.SemiAbsolute)) { lParam.uFlags |= 0x80; } lParam.lpszText = Marshal.StringToHGlobalAuto(text); flag = true; } TipInfo info = (TipInfo) this.tools[key]; if (info == null) { info = new TipInfo(text, type); } else { info.TipType |= type; info.Caption = text; } info.Position = position; this.tools[key] = info; System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), System.Windows.Forms.NativeMethods.TTM_SETTOOLINFO, 0, lParam); System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), 0x411, 1, lParam); } finally { if (flag && (IntPtr.Zero != lParam.lpszText)) { Marshal.FreeHGlobal(lParam.lpszText); } } } else { this.Hide(win); TipInfo info2 = (TipInfo) this.tools[key]; if (info2 == null) { info2 = new TipInfo(text, type); } else { info2.TipType |= type; info2.Caption = text; } info2.Position = position; this.tools[key] = info2; IntPtr safeHandle = Control.GetSafeHandle(win); this.owners[safeHandle] = win; System.Windows.Forms.NativeMethods.TOOLINFO_TOOLTIP winTOOLINFO = this.GetWinTOOLINFO(safeHandle); winTOOLINFO.uFlags |= 0x20; if ((type == TipInfo.Type.Absolute) || (type == TipInfo.Type.SemiAbsolute)) { winTOOLINFO.uFlags |= 0x80; } try { winTOOLINFO.lpszText = Marshal.StringToHGlobalAuto(text); System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), System.Windows.Forms.NativeMethods.TTM_ADDTOOL, 0, winTOOLINFO); System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), 0x411, 1, winTOOLINFO); } finally { if (IntPtr.Zero != winTOOLINFO.lpszText) { Marshal.FreeHGlobal(winTOOLINFO.lpszText); } } } if (key != null) { Form form = key.FindFormInternal(); if (form != null) { form.Deactivate += new EventHandler(this.BaseFormDeactivate); } } }
private void ShowTooltip(string text, IWin32Window win, int duration) { if (win == null) { throw new ArgumentNullException("win"); } Control wrapper = win as Control; if (wrapper != null) { System.Windows.Forms.NativeMethods.RECT rect = new System.Windows.Forms.NativeMethods.RECT(); System.Windows.Forms.UnsafeNativeMethods.GetWindowRect(new HandleRef(wrapper, wrapper.Handle), ref rect); Cursor currentInternal = Cursor.CurrentInternal; Point position = Cursor.Position; Point p = position; Screen screen = Screen.FromPoint(position); if (((position.X < rect.left) || (position.X > rect.right)) || ((position.Y < rect.top) || (position.Y > rect.bottom))) { System.Windows.Forms.NativeMethods.RECT rect2 = new System.Windows.Forms.NativeMethods.RECT { left = (rect.left < screen.WorkingArea.Left) ? screen.WorkingArea.Left : rect.left, top = (rect.top < screen.WorkingArea.Top) ? screen.WorkingArea.Top : rect.top, right = (rect.right > screen.WorkingArea.Right) ? screen.WorkingArea.Right : rect.right, bottom = (rect.bottom > screen.WorkingArea.Bottom) ? screen.WorkingArea.Bottom : rect.bottom }; p.X = rect2.left + ((rect2.right - rect2.left) / 2); p.Y = rect2.top + ((rect2.bottom - rect2.top) / 2); wrapper.PointToClientInternal(p); this.SetTrackPosition(p.X, p.Y); this.SetTool(win, text, TipInfo.Type.SemiAbsolute, p); if (duration > 0) { this.StartTimer(this.window, duration); } } else { TipInfo info = (TipInfo) this.tools[wrapper]; if (info == null) { info = new TipInfo(text, TipInfo.Type.SemiAbsolute); } else { info.TipType |= TipInfo.Type.SemiAbsolute; info.Caption = text; } info.Position = p; if (duration > 0) { if (this.originalPopupDelay == 0) { this.originalPopupDelay = this.AutoPopDelay; } this.AutoPopDelay = duration; } this.SetToolTipInternal(wrapper, info); } } }