So I have this custom panel that I am making act like a form (I'm experimenting) and whenever I move it around the screen with the mouse it results in 20%+ CPU usage. The snippet below is the cause because if I comment it out it works fine, but I obviously can't move the panel with the mouse then.
private void MoveWithEdgeLock()
{
targetLocation = new Point(Cursor.Position.X - downLocation.X, Cursor.Position.Y - downLocation.Y);
Point placement = targetLocation;
if (targetLocation.X <= EDGELOCK && targetLocation.X >= -EDGELOCK)
{
placement = new Point(0, placement.Y);
}
else if (targetLocation.X + Width >= Screen.PrimaryScreen.Bounds.Right - EDGELOCK && targetLocation.X + Width <= Screen.PrimaryScreen.Bounds.Right + EDGELOCK)
{
placement = new Point(Screen.PrimaryScreen.Bounds.Right - Width, placement.Y);
}
if (targetLocation.Y <= EDGELOCK && targetLocation.Y >= -EDGELOCK)
{
placement = new Point(placement.X, 0);
}
else if (targetLocation.Y + Height >= Screen.PrimaryScreen.Bounds.Bottom - TASKBAR_HEIGHT - EDGELOCK && targetLocation.Y + Height <= (Screen.PrimaryScreen.Bounds.Bottom - TASKBAR_HEIGHT) + EDGELOCK)
{
placement = new Point(placement.X, Screen.PrimaryScreen.Bounds.Bottom - TASKBAR_HEIGHT - Height);
}
Location = placement;
}
Even if I simply make it this;
Location = new Point(Cursor.Position.X - downLocation.X, Cursor.Position.Y - downLocation.Y);
It still results in high CPU usage.
Oh and I call either of these like so:
protected override void OnMouseMove(MouseEventArgs e)
{
if (canMove)
MoveWithEdgeLock();
base.OnMouseMove(e);
}
CanMove simply gets set via OnMouseDown/OnMouseUp.
Sorry for long lines, I'm used to writing on one line.
EDIT
As I have said in a comment, I have a borderless form taking up my entire screen - 1920x1080. That does have a BackgroundImage and I convert/force it to use Format32bppPArgb and resize it to Clientsize.
I have tried removing the BackgroundImage (so it was just a black back color) and that made no difference.
I have also tried remove all controls on the panel and that also made no difference.
I have even tried using the P/Invoke method as noted HERE, and it still results in high CPU Usage