ResizeMode =“NoResize”和WindowStyle =“None”的WPF窗口不会掉落阴影

问题描述 投票:0回答:1

我在使用 WPF 中的窗口时遇到问题

如果我设置:

ResizeMode="NoResize" 
WindowStyle="None"

窗口看起来平坦,没有圆形边框和下方的阴影。

有办法解决这个问题吗?不使用具有 DropShadow 效果的边框

我尝试过这个解决方案: dropshadow-for-wpf-borderless-window ,但窗口将拒绝渲染内容,只显示一个带有适当阴影和圆角边框的白色矩形。

提前致谢

wpf 无边框窗口的 dropshadow

wpf window border shadow dropshadow
1个回答
0
投票

您可以使用

DwmExtendFrameIntoClientArea
GetWindowLong
GetWindowLong
Win32 API 来获取一个没有不可调整大小的窗口、没有标题栏但带有阴影和边框的窗口:

public partial class MainWindow : Window
{
    [DllImport("dwmapi.dll")]
    private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);

    [DllImport("user32.dll")]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [StructLayout(LayoutKind.Sequential)]
    private struct Margins
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    public MainWindow()
    {
        InitializeComponent();
        Title = string.Empty;
        //ResizeMode = ResizeMode.NoResize;
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);

        IntPtr hWnd = new WindowInteropHelper(this).Handle;
        Margins margins = new Margins { cxLeftWidth = -1, cxRightWidth = -1, cyTopHeight = -1, cyBottomHeight = -1 };
        DwmExtendFrameIntoClientArea(hWnd, ref margins);
        const int GWL_STYLE = -16;
        const int WS_SYSMENU = 0x80000;
        SetWindowLong(hWnd, GWL_STYLE,
            GetWindowLong(hWnd, GWL_STYLE) & ~WS_SYSMENU);

    }
}

© www.soinside.com 2019 - 2024. All rights reserved.