Вы не сможете полностью избавиться от мерцания при изменении размера из-за ограничений DWM в новых версиях Windows. Собственно, наиболее известное приложение WPF, Visual Studio, тоже подвержено этой проблеме, по крайней мере по состоянию на VS 2017. Но наилучшего результата, по моим тестам, можно достичь, убрав стандартную рамку сверху с помощью обработки сообщения WM_NCCALCSIZE и реализовав изменение размера через собственный обработчик WM_NCHITTEST:
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="250" Width="400" FontSize="14" Loaded="Window_Loaded" WindowStyle="SingleBorderWindow">
<Grid Background="Black" x:Name="grid">
<Button Content="Button1" HorizontalAlignment="Left" Height="50" Margin="64,0,0,0" VerticalAlignment="Top"
Background="Green" Width="150" Click="Button1_Click" Foreground="White"/>
<Button x:Name="bMin" Content="_" HorizontalAlignment="Right" Height="40" Margin="0,0,80,00" VerticalAlignment="Top"
Background="LightBlue" Width="40" Click="bMin_Click" />
<Button x:Name="bMax" Content="□" HorizontalAlignment="Right" Height="40" Margin="0,0,40,00" VerticalAlignment="Top"
Background="LightBlue" Width="40" Click="bMax_Click" />
<Button Content="X" HorizontalAlignment="Right" Height="40" Margin="0,0,0,0" VerticalAlignment="Top"
Background="Red" Width="40" Click="ButtonClose_Click" />
<Label Content="Label" HorizontalAlignment="Left" Margin="0,0,0,0" Foreground="White"
VerticalAlignment="Top" Width="50" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace WpfTest
{
public partial class MainWindow : Window
{
IntPtr Handle;
int xborder;
int yborder;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left, top, right, bottom;
}
[StructLayout(LayoutKind.Sequential)]
struct NCCALCSIZE_PARAMS
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public RECT[] rgrc;
public IntPtr lppos;
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern bool AdjustWindowRectEx(ref RECT lpRect, uint dwStyle,
bool bMenu, uint dwExStyle);
[DllImport("dwmapi.dll")]
static extern bool DwmDefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref IntPtr plResult);
[DllImport("user32.dll")]
static extern int GetSystemMetrics(uint smIndex);
static int GET_X_LPARAM(IntPtr lp)
{
short loword = (short)((ulong)lp & 0xffff);
return loword;
}
static int GET_Y_LPARAM(IntPtr lp)
{
short hiword = (short)((((ulong)lp) >> 16) & 0xffff);
return hiword;
}
const uint WM_NCCALCSIZE = 0x0083;
const uint WM_NCHITTEST = 0x0084;
const uint WM_ACTIVATE = 0x0006;
const uint WM_NCACTIVATE = 0x0086;
const uint WM_NCPAINT = 0x85;
const uint WS_OVERLAPPED = 0x00000000;
const uint WS_CAPTION = 0x00C00000;
const uint WS_SYSMENU = 0x00080000;
const uint WS_THICKFRAME = 0x00040000;
const uint WS_MINIMIZEBOX = 0x00020000;
const uint WS_MAXIMIZEBOX = 0x00010000;
const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
const uint HTTOPLEFT = 13;
const uint HTTOPRIGHT = 14;
const uint HTTOP = 12;
const uint HTCAPTION = 2;
const uint HTLEFT = 10;
const uint HTNOWHERE = 0;
const uint HTRIGHT = 11;
const uint HTBOTTOM = 15;
const uint HTBOTTOMLEFT = 16;
const uint HTBOTTOMRIGHT = 17;
const uint SM_CXSIZEFRAME = 32;
const uint SM_CYSIZEFRAME = 33;
public MainWindow()
{
InitializeComponent();
}
//обработка координат мыши для неклиентской области
IntPtr HitTestNCA(IntPtr hWnd, IntPtr wParam, IntPtr lParam)
{
// Get the point coordinates for the hit test.
var ptMouse = new Point(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
// Get the window rectangle.
RECT rcWindow;
GetWindowRect(hWnd, out rcWindow);
// Get the frame rectangle, adjusted for the style without a caption.
RECT rcFrame = new RECT();
AdjustWindowRectEx(ref rcFrame, WS_OVERLAPPEDWINDOW & ~WS_CAPTION, false, 0);
// Determine if the hit test is for resizing. Default middle (1,1).
ushort uRow = 1;
ushort uCol = 1;
bool fOnResizeBorder = false;
// Determine if the point is at the top or bottom of the window.
if (ptMouse.Y >= rcWindow.top && ptMouse.Y < rcWindow.top + yborder)
{
fOnResizeBorder = (ptMouse.Y < (rcWindow.top - rcFrame.top));
uRow = 0;
}
else if (ptMouse.Y < rcWindow.bottom && ptMouse.Y >= rcWindow.bottom - yborder)
{
uRow = 2;
}
// Determine if the point is at the left or right of the window.
if (ptMouse.X >= rcWindow.left && ptMouse.X < rcWindow.left + xborder)
{
uCol = 0; // left side
}
else if (ptMouse.X < rcWindow.right && ptMouse.X >= rcWindow.right - xborder)
{
uCol = 2; // right side
}
// Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT)
IntPtr[,] hitTests = new IntPtr[,]
{
{ (IntPtr)HTTOPLEFT, fOnResizeBorder? (IntPtr)HTTOP : (IntPtr)HTCAPTION, (IntPtr)HTTOPRIGHT },
{ (IntPtr)HTLEFT, (IntPtr)HTNOWHERE, (IntPtr)HTRIGHT},
{ (IntPtr)HTBOTTOMLEFT, (IntPtr)HTBOTTOM, (IntPtr)HTBOTTOMRIGHT },
};
return hitTests[uRow, uCol];
}
//обработчик сообщений для окна
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
bool fCallDWP = true;
IntPtr lRet = IntPtr.Zero;
if (msg == WM_NCCALCSIZE)
{
if (wParam != (IntPtr)0)
{
//убираем стандартную рамку сверху
lRet = IntPtr.Zero;
NCCALCSIZE_PARAMS pars = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(lParam, typeof(NCCALCSIZE_PARAMS));
pars.rgrc[0].top = pars.rgrc[0].top;
pars.rgrc[0].left = pars.rgrc[0].left + xborder;
pars.rgrc[0].right = pars.rgrc[0].right - xborder * 2;
pars.rgrc[0].bottom = pars.rgrc[0].bottom - yborder;
Marshal.StructureToPtr(pars, lParam, false);
handled = true;
return lRet;
}
}
if (msg == WM_NCACTIVATE)
{
lRet = (IntPtr)1;
handled = true;
return lRet;
}
fCallDWP = !DwmDefWindowProc(hwnd, msg, wParam, lParam, ref lRet);
if (msg == WM_NCHITTEST && lRet == IntPtr.Zero)
{
//обработка нажатий мыши
lRet = HitTestNCA(hwnd, wParam, lParam);
if (lRet != (IntPtr)HTNOWHERE)
{
fCallDWP = false;
}
}
//если сообщение не обработано, передаем базовой процедуре
if (fCallDWP) handled = false;
else handled = true;
return lRet;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper h = new WindowInteropHelper(this);
HwndSource source = HwndSource.FromHwnd(h.Handle);
Handle = h.Handle;
source.AddHook(new HwndSourceHook(WndProc));//регистрируем обработчик сообщений
xborder = GetSystemMetrics(SM_CXSIZEFRAME);
yborder = GetSystemMetrics(SM_CYSIZEFRAME);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 1");
}
private void bMin_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void bMax_Click(object sender, RoutedEventArgs e)
{
if (this.WindowState == System.Windows.WindowState.Normal) this.WindowState = System.Windows.WindowState.Maximized;
else this.WindowState = System.Windows.WindowState.Normal;
}
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
Выглядит так:
Заточено под Windows 10. В Windows 7 способ тоже работает и дает неплохой результат, но стиль окна должен быть WindowStyle="None"
. При других стилях мерцание противоположного края возвращается. В Windows 10 же при использовании стиля None исчезают анимации свертывания-развертывания, поэтому для разных версий Windows понадобится выставлять разный стиль.