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

后端 未结 5 1749
我在风中等你
我在风中等你 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: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);
    }
    

提交回复
热议问题