1

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

Community
  • 1
  • 1
73cn0109y
  • 75
  • 1
  • 10
  • None of this code is expensive. It is what we can't see that can consume a lot of cpu cycles. Like an unoptimized BackgroundImage in the panel's Parent, one that has a crummy pixel format or needs to be resized to fit the window. Don't force us to guess. – Hans Passant Jan 17 '16 at 20:06
  • @HansPassant I do have a borderless form that takes up the whole screen - 1920x1080 - with a backgroundimage but I have code to re-size that to ClientSize and convert it to using Format32bppPArgb. That was the first thing I tested. I even tried removing the backgroundimage and just having a solid black back color but the issue still persisted. – 73cn0109y Jan 17 '16 at 20:10

0 Answers0