General Grey
General Grey

Reputation: 3688

why does my Mouse gets choppy when moved over a button in WPF

I made a WPF Application, with a window a Grid and a button. In the window SizeChanged event I scaleTransform my Grid to maximize it's size but keeping the aspect ratio.

When ever I move the mouse over the button, the hottrack occurs as you would expect, but the mouse stalls for less than half a second, not a huge problem but it seems something isn't right.

EDIT I suppose I never actually asked a question. What I want to know is. is this normal behavior, or is there something wrong with how I am doing this.

    //Store the initial size of the Grid
    double GridStartWidth;
    double GridStartHeight;

    public MainWindow()
    {
        InitializeComponent();
        //Get the values for the initial size of Grid
        GridStartWidth = MainGrid.Width;
        GridStartHeight = MainGrid.Height;
    }


    private void myMainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        double min = Math.Min(this.Height / GridStartHeight, this.Width / GridStartWidth);
        Transform tr = new ScaleTransform(min, min, .5, .5);
        MainGrid.LayoutTransform = tr;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }

Not sure if you need the Xaml but here it is

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" 
Name="myMainWindow"
Width="1280" Height="1024" SizeChanged="myMainWindow_SizeChanged" AllowsTransparency="True" Background="#4FFFFFFF" WindowStyle="None" WindowState="Maximized">
<Grid Name="MainGrid"  Background="#FF8DC78D" Width="800" Height="600">

    <Button Content="Exit" Height="23" HorizontalAlignment="Left" Margin="13,12,0,0" Name="ExitButton" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

EDIT #2 I just tried to duplicate this issue starting from scratch, and testing it step by step as I added features. The problem occurs when I set the Window State as Maximized.

EDIT #3 Another Test I removed the Allow Transparency property, and set the background to a solid color, and it works fine. So the Problem has to do with Maximized window with a transparent background. Does this make sense?

Upvotes: 0

Views: 381

Answers (1)

Martin Liversage
Martin Liversage

Reputation: 106906

Updating the mouse pointer on the screen in Windows has very high priority and mouse jitter or stutter is a result of driver problems or serious performance problems.

WPF is available on Windows XP but hardware acceleration is not always available leading to performance problems. In particular there is a problem that affects transparent windows (which you use).

Dwayne Need from Microsoft has a blog post about Transparent Windows in WPF:

DirectX does provide the IDirect3DSurface9::GetDC method, which can return a DC that references the DirectX surface. Unfortunately there was a restriction in DX9c that would fail this method if it were called on a surface that contained an alpha channel. Of course, the entire point of our layered window API is to enable per-pixel transparency. This restriction was lifted for Vista, but our initial release forced WPF to use its software rendering fallback with rendering to a layered window on XP. We were able to lift this restriction for XP too, which we released as a hot fix (KB 937106). This hot fix was also included in XP SP3, so go get it! Now, on XP, we can render via DirectX and pass the results of IDirect3DSurface9::GetDC directly to UpdateLayeredWindow. On good video drivers, the resulting copy will remain entirely on the video card, leading to excellent performance. Some video drivers, however, may choose to perform this copy through system memory. The performance on such systems will not be nearly as good, but should still be reasonable for many scenarios.

Your experience may be a result of not having the hot fix on your computer (but it is included in SP3), or a result of WPF having to switch to software rendering for your particular application.

Upvotes: 1

Related Questions