3
Answers

Slower WPF Performance after a button gets focus

Photo of Sergey Sharikov

Sergey Sharikov

16y
3.9k
1
Hi all!
 
Could you please help me with my problem. I am writing an app that allows users to drag controls over a canvas and have met the following strange behaviour: wpf performance slowsdowns as soon as a button on a form receives focus. This is so strange to me. I've posted a code of a sample app, please try to click on a red rectangle, and move the mouse holding left button. You should see that rectangle follows the cursor pretty fast. But if you click (or even press Tab to give the button a focus) and try to drag the rectangle again - you will see that now rectangle is not as fast as before.. The recrangle will be still slow if you click Tab again and give focus to the textbox.
 
Do you know why it happens and how I can avoid it?

<Window x:Class="ButtonSpeedTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="627" Width="580">
    <Canvas Background="Wheat" MouseMove="MyMouseMove">
        <Button Canvas.Left="30" Canvas.Top="30" Height="37" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="66">Button</Button>
        <Rectangle MouseDown="MyMouseDown" MouseUp="MyMouseUp" MouseMove="MyMouseMove" Name="rect"  Canvas.Left="100" Canvas.Top="100" Fill="Red" Stroke="Black" Width="70" Height="50" />
        <TextBox Canvas.Left="246" Canvas.Top="37.52" Height="23" Name="textBox1" Width="120" />
    </Canvas>
</Window>

 

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ButtonSpeedTest
{
 public partial class Window1 : Window
 {
  bool isDragging;

  public Window1()
  {
   InitializeComponent();
  }

  private void MyMouseDown(object sender, MouseButtonEventArgs e)
  {
   isDragging = true;
  }

  private void MyMouseUp(object sender, MouseButtonEventArgs e)
  {
   isDragging = false;
  }  

  private void MyMouseMove(object sender, MouseEventArgs e)
  {
   if (isDragging)
   {
    Point position = e.GetPosition(this);

    rect.SetValue(Canvas.LeftProperty, position.X - rect.Width / 2);
    rect.SetValue(Canvas.TopProperty, position.Y - rect.Height / 4);
   }
  }
 }

Thanks,
Sergey.

Answers (3)

0
Photo of Bechir Bejaoui
NA 20.1k 5.3m 16y
May it be, anyway, my OS is XP
0
Photo of Sergey Sharikov
NA 3 0 16y

Hi,

It seems that signinficant performance slowdown can be seen only on Vista with Aero turned on. Note, that my machine should support Aero (quad 2.53, 4 Gb Ram, GeGorce 9800 GT), so the problem must be somewhere inside Aero..

Thanks,

Sergey.

 

0
Photo of Bechir Bejaoui
NA 20.1k 5.3m 16y

It's strange, I copied your code and try it and is perfect, there aren't any applications slow down!!
Is there any other code that interfers with this one? might it negatively affects it.