C# Code:
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using zenOn;
namespace ProjectAddin
{
public partial class frmTooltip : Form
{
private static Timer LeaveCheckTimer;
private int CheckInterval = 25;
protected static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOACTIVATE = 0x0010;
private const int WS_POPUP = unchecked((int)0x80000000);
private const int WS_EX_TOPMOST = 0x00000008;
private const int WS_EX_NOACTIVATE = 0x08000000;
private const short MaskMointorID = 0x0011;
private Point myToolTipPoint;
private Rectangle myElementRectangle;
private IntPtr myWindowsHandle;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
private enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private void LeaveCheck(object sender, EventArgs e)
{
if (Cursor.Position.X < myElementRectangle.Left || Cursor.Position.X > myElementRectangle.Right || Cursor.Position.Y < myElementRectangle.Top || Cursor.Position.Y > myElementRectangle.Bottom)
{
LeaveCheckTimer.Stop();
frmTooltip_MouseLeave(sender, e);
}
}
public frmTooltip()
{
this.SetStyle(ControlStyles.Selectable, false);
InitializeComponent();
LeaveCheckTimer = new Timer();
LeaveCheckTimer.Interval = CheckInterval;
LeaveCheckTimer.Tick += new EventHandler(LeaveCheck);
}
public void Set(IElement obElement, string toolTipText)
{
try
{
if (toolTipText.Length > 0)
{
myWindowsHandle = IntPtr.Zero;
if (!LeaveCheckTimer.Enabled) LeaveCheckTimer.Start();
string TemplateName = obElement.Parent.Parent.Template.TrimStart('_');
int myTemplateTop = obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).Top;
int myTemplateLeft = obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).Left;
int myTemplateRight = obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).Right;
int myTemplateBottom = obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).Bottom;
this.label1.Text = toolTipText;
this.Size = this.label1.Size;
myToolTipPoint = new Point();
myToolTipPoint.X = obElement.Left + myTemplateLeft;
myToolTipPoint.Y = obElement.Top + myTemplateTop;
if (obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).BorderStyle == 0x0000 || obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).BorderStyle != 0x0000 && obElement.Parent.Parent.Parent.Parent.Templates().Item(TemplateName).ShowTitle)
myWindowsHandle = GetWindow(new IntPtr(obElement.Parent.Parent.Parent.Parent.Parent.Parent.hWnd), GetWindow_Cmd.GW_CHILD);
else
myWindowsHandle = FindWindow((string)null, obElement.Parent.Parent.Name);
ClientToScreen(myWindowsHandle, ref myToolTipPoint);
myElementRectangle = new Rectangle(myToolTipPoint.X, myToolTipPoint.Y, obElement.Right - obElement.Left, obElement.Bottom - obElement.Top);
if (obElement.Right + myTemplateLeft + this.Width > myTemplateRight)
myToolTipPoint.X = myTemplateRight - this.Width;
else
myToolTipPoint.X = obElement.Right + myTemplateLeft;
if (obElement.Bottom + myTemplateTop + this.Height > myTemplateBottom)
myToolTipPoint.Y = myTemplateBottom - this.Height;
else
myToolTipPoint.Y = obElement.Bottom + myTemplateTop;
ClientToScreen(myWindowsHandle, ref myToolTipPoint);
Screen myScreen = Screen.AllScreens[obElement.Parent.Parent.MonitorId & MaskMointorID];
if (myScreen != null)
{
myToolTipPoint.X = myToolTipPoint.X + myScreen.Bounds.Left;
myToolTipPoint.Y = myToolTipPoint.Y + myScreen.Bounds.Top;
};
Location = myToolTipPoint;
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
this.Opacity = 1;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message + ex.StackTrace);
}
}
private void frmTooltip_MouseLeave(object sender, EventArgs e)
{
this.Hide();
this.Opacity = 0;
}
}
}
This document governs the use of our Community Forum. By registering and using the platform, you accept these conditions.
The COPA-DATA Community Forum serves to encourage the exchange of information and experience about the zenon software between forum users respectively zenon users.
Please mind that any published information on the Community Forum is the subjective opinion and view based on the experience and the level of knowledge of the author. COPA-DATA does not overtake any responsibility for the content and the accuracy of the shared information.
Users of the Community Forum are encouraged to share only well-founded experiences and to point out any risks associated with the implementation of proposed solutions to problems. COPA-DATA at its absolute discretion, reserves the right to moderate the forum. In this connection COPA-DATA may remove any information containing false facts, potentially dangerous solutions, bad language or content that may insult, degrade or discriminate others. COPA-DATA may block a non-complying user from forum access if the user violated this provision.
COPA-DATA reserves the right to change this document from time to time at own discretion.
Ing. Punzenberger COPA-DATA GmbH
Karolingerstraße 7b · 5020 Salzburg · Austria
www.copadata.com