How i can recognize system display by MonitorId

How i can recognize system display by MonitorId

We use multi-monitor project, i my VSTA code i try use MouseOver to detect on which is display Element object is located. And display there are Windows form.
I try use property DynPicture.MonitorId and always received 0x100 or 0x101. I guess last two bytes is index in Screen.AllScreens collection, but it is not.
How i can recognize system display by MonitorId?
Bellow a my code, i hope it is help better understand my problem.
PS Why Element.hWnd always return 0?
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 is a migrated post! Originally posted on 01.10.2013 by user hyper_sonic. Please be aware that information can be outdated.