我想要一个没有标题栏并可从整个客户区域拖动的 winUI 3 窗口。
就像我以前在 Winforms 和 WPF 中所做的那样,我尝试使用 win32 调用(“sendMeaasge”)(参见下面的代码)。但它似乎在 WinUI 3 中被破坏了,因为它不会在按下指针时拖动窗口,而只会在释放指针时拖动窗口。
下面是使用“sendMessage”win32 api 调用的代码。使用此功能时,仅当释放指针时窗口才会移动。
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
//Remove the title bar
(AppWindow?.Presenter as OverlappedPresenter)?.SetBorderAndTitleBar(false, false);
Content = new Grid() { Background = new SolidColorBrush(Colors.Transparent) };
this.Content.PointerPressed += MainWindow_PointerPressed;
}
private void MainWindow_PointerPressed(object sender, PointerRoutedEventArgs e)
{
ReleaseCapture();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
SendMessage(hwnd, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero);
}
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool ReleaseCapture();
}
我也尝试使用pointerPressed、pointerMoved和pointerReleased手动移动窗口,但确实不够流畅。我希望鼠标和触摸都能正常工作,所以这就是为什么我不想使用光标的位置来移动窗口。
您可以尝试将
Window
内容制作为TitleBar
: