Very Poor Performance w/ Framework 1.1
Status: Beta
Brought to you by:
prilling
Prior to .NET Framework 1.1, performance was
excellent. The BalloonWindow followed the bound
control very quickly, and all was good. Upgrade the
source project to VS.NET 2k3, and you will see a
substantial performance degradation. We are using this
component in a commercial application and will likely
write our own version if we can't resolve this. Thanks.
Logged In: YES
user_id=969007
Replace all occurances of Screen.GetBounds with
Screen2.GetBounds. No, it is not the cleanest looking code
ever. Yes, it will do a great job of getting the screen
bounds, provided the user doesn't change their resolution
while your app is running. Even then, it should correct itself.
public class Screen2
{
private static Rectangle[] mBounds;
static Screen2()
{
RefreshScreens();
}
private static void RefreshScreens()
{
try
{
mBounds = new Rectangle
[Screen.AllScreens.Length];
for (int i = 0; i <
mBounds.Length; i++)
mBounds[i] =
Screen.AllScreens[i].Bounds;
}
catch {}
}
public static Rectangle GetBounds(Point pt)
{
try
{
foreach (Rectangle
screenBounds in mBounds)
if
(screenBounds.Contains(pt))
return
screenBounds;
RefreshScreens();
foreach (Rectangle
screenBounds in mBounds)
if
(screenBounds.Contains(pt))
return
screenBounds;
return mBounds[0];
}
catch
{
RefreshScreens();
return Screen.GetBounds(pt);
}
}
public static Rectangle GetBounds(Rectangle rect)
{
try
{
Size size = Size.Empty;
Rectangle bounds =
Rectangle.Empty;
foreach (Rectangle
screenBounds in mBounds)
{
Rectangle copy =
screenBounds;
copy.Intersect(rect);
if (EvalSize(copy.Size)
> EvalSize(size))
{
size =
copy.Size;
bounds =
screenBounds;
}
}
if (bounds == Rectangle.Empty)
{
RefreshScreens();
foreach (Rectangle
screenBounds in mBounds)
{
Rectangle
copy = screenBounds;
copy.Intersect
(rect);
if (EvalSize
(copy.Size) > EvalSize(size))
bounds = screenBounds;
}
if (bounds !=
Rectangle.Empty)
return bounds;
}
else
{
return bounds;
}
return mBounds[0];
}
catch
{
RefreshScreens();
return Screen.GetBounds(rect);
}
}
private static long EvalSize(Size size)
{
return size.Height * size.Width;
}
public static Rectangle GetBounds(Control ctl)
{
try
{
Rectangle rect =
ctl.RectangleToScreen(ctl.ClientRectangle);
Control parent = ctl.Parent;
Rectangle parentRect = parent
== null ? Rectangle.Empty : parent.RectangleToScreen
(parent.ClientRectangle);
while (parent != null && !
parentRect.IsEmpty)
{
parentRect.Intersect
(rect);
if (!parentRect.IsEmpty)
rect =
parentRect;
parent = parent.Parent;
}
return GetBounds(rect);
}
catch
{
RefreshScreens();
return Screen.GetBounds(ctl);
}
}
}