How to fix the WPF form resize - controls lagging behind and black background?

后端 未结 5 1739
我在风中等你
我在风中等你 2020-12-01 11:04

I have a very simple WPF window - the only thing in it is a right-aligned button. When I resize the window by dragging the left boundary, the button jumps around - a lot. Tr

相关标签:
5条回答
  • 2020-12-01 11:10

    I believe that the temporary black background is a WPF issue related to the fact that WPF uses DirectX as rendering mechanism, and when you resize the windows it has to synchronize drawing with the windowing system. This may also explain why the button is misplaced in relation to the window while dragging the window border. Drawing the non-client area of the window is much slower than drawing what's inside the window, and if you move the mouse fast on a slow computer the discrepancies between the border end the internals of the window will probably be more noticeable.

    Supposedly this only happens on Vista with Aero enabled, and it should have been fixed in Vista SP1. However, I just tested on SP2, and I did still see a little bit of black background, but only when Aero was enabled. My graphics card is pretty fast so it was hardly noticeable.

    If my analysis is correct the only way you can fix your problem is to getter a faster graphics card or turn off Aero.

    0 讨论(0)
  • 2020-12-01 11:14

    This is complete working code based on Wieser Software Ltd's 2nd solution.

    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
    
            //ensure win32 handle is created
            var handle = new WindowInteropHelper(this).EnsureHandle();
    
            //set window background
            var result = SetClassLong(handle, GCL_HBRBACKGROUND, GetSysColorBrush(COLOR_WINDOW));
        }
    
        public static IntPtr SetClassLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            //check for x64
            if (IntPtr.Size > 4)
                return SetClassLongPtr64(hWnd, nIndex, dwNewLong);
            else
                return new IntPtr(SetClassLongPtr32(hWnd, nIndex, unchecked((uint)dwNewLong.ToInt32())));
        }
    
        private const int GCL_HBRBACKGROUND = -10;
        private const int COLOR_WINDOW = 5;
    
        [DllImport("user32.dll", EntryPoint = "SetClassLong")]
        public static extern uint SetClassLongPtr32(IntPtr hWnd, int nIndex, uint dwNewLong);
    
        [DllImport("user32.dll", EntryPoint = "SetClassLongPtr")]
        public static extern IntPtr SetClassLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
    
        [DllImport("user32.dll")]
        static extern IntPtr GetSysColorBrush(int nIndex);
    }
    
    0 讨论(0)
  • 2020-12-01 11:15

    This doesn't seem to be possible in the current versions of WPF.

    0 讨论(0)
  • 2020-12-01 11:31

    Other answers have addressed how to fill in a different background colors to try and mitigate the effect of WPF slow drawing.

    I can't offer any magic to speed up WPF but I can offer some insight about where the black is coming from.

    The visual bug in your first screenshot is due to an extra layer of resize problem that Aero added, and there is a partial workaround.

    I was fighting the issue of ugly live-resize in native Win32 apps and I created a roundup Question/Answer which brings together 10 years of posts on resize woes and offers some new insights (too long to paste the content here in this question). Please see:

    How to smooth ugly jitter/flicker/jumping when resizing windows, especially dragging left/top border (Win 7-10; bg, bitblt and DWM)?

    0 讨论(0)
  • 2020-12-01 11:32

    There are two solutions, described here: http://wieser-software.blogspot.co.uk/2012/06/wpf-window-rendering-woes.html

    1. Hook WndProc and handle WM_ERASEBKGND and draw the system WINDOW_COLOR on the background, or another color to suit your application theme.
    2. Call SetClassLong to set the Window Class Background Brush

      SetClassLong(Handle, GCL_HBRBACKGROUND, GetSysColorBrush(COLOR_WINDOW));

    0 讨论(0)
提交回复
热议问题