Windows App SDK 1.4更改标题栏可拖拽区域

这天,我刚刚更新到Windows App SDK 1.4,你猜怎么着,SetDragRectangles方法不能用了!

这我哪能忍!我直接上去给微软了一个Q&A:

How to fix AppWindow.TitleBar.SetDragRectangles not working in Windows App SDK 1.4.230822000? - Microsoft Q&A

最终得到了解决方法:

IntPtr hWndMain = WinRT.Interop.WindowNative.GetWindowHandle(this);
Microsoft.UI.WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWndMain);
var incps = InputNonClientPointerSource.GetForWindowId(myWndId);
incps.SetRegionRects(NonClientRegionKind.Caption, new RectInt32[] { new RectInt32(X, Y, Width, Height) });

其中,X,Y表示坐标,Width表示宽,Height表示高,直接修改就好。

至于DPI,貌似还是会受到的:(

这还不得点个关注嘛awa

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Qt 的 QWidget 类来实现自定义标题栏,并且让鼠标可以拖动窗口。 以下是一个简单的示例代码: ```cpp #include <QtWidgets> class CustomTitleBar : public QWidget { public: CustomTitleBar(QWidget *parent = nullptr) : QWidget(parent) { // 设置标题栏大小和背景色 setFixedHeight(30); setStyleSheet("background-color: gray;"); // 添加标题栏上的标题 titleLabel = new QLabel("Custom Title Bar", this); titleLabel->setAlignment(Qt::AlignCenter); titleLabel->setStyleSheet("color: white;"); // 添加关闭按钮 closeButton = new QPushButton("X", this); closeButton->setFixedSize(20, 20); closeButton->setStyleSheet("background-color: red; color: white;"); // 将标题和关闭按钮布局到标题栏 QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(titleLabel); layout->addStretch(); layout->addWidget(closeButton); layout->setContentsMargins(5, 0, 5, 0); } // 重写鼠标按下事件,实现窗口的拖动 void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = true; lastMousePos = event->globalPos(); event->accept(); } } // 重写鼠标移动事件,实现窗口的拖动 void mouseMoveEvent(QMouseEvent *event) override { if (isDragging) { QPoint delta = event->globalPos() - lastMousePos; lastMousePos = event->globalPos(); parentWidget()->move(parentWidget()->pos() + delta); event->accept(); } } // 重写鼠标释放事件,实现窗口的拖动 void mouseReleaseEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = false; event->accept(); } } private: QLabel *titleLabel; QPushButton *closeButton; bool isDragging = false; QPoint lastMousePos; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建窗口并设置标题栏为自定义标题栏 QWidget window; window.setWindowFlags(Qt::FramelessWindowHint); CustomTitleBar *titleBar = new CustomTitleBar(&window); QVBoxLayout *layout = new QVBoxLayout(&window); layout->addWidget(titleBar); layout->addWidget(new QLabel("Hello World!", &window)); window.setLayout(layout); window.resize(300, 200); window.show(); return app.exec(); } ``` 在上述代码中,首先创建了一个名为 `CustomTitleBar` 的自定义窗口部件,它包括一个标题和一个关闭按钮。然后,在 `CustomTitleBar` 类中重写了鼠标按下、移动和释放事件,实现了窗口的拖动。 在 `main` 函数中,创建了一个窗口并设置标题栏为自定义标题栏,然后将自定义标题栏和一个标签添加到窗口中。最后,设置窗口大小并显示窗口。 需要注意的是,为了实现窗口的拖动,需要将窗口的边框隐藏,可以使用 `setWindowFlags(Qt::FramelessWindowHint)` 来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值