qt 窗体嵌入到任务栏

qwidget嵌入到任务栏

#include <QApplication>
#include <QWidget>
#include <QDebug>
#include <windows.h>
#include <QVBoxLayout> // 包含 QVBoxLayout 头文件
#include <QLabel>
#include <QLineEdit>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    // 获取任务栏的高度
    HWND taskbar = FindWindow(L"Shell_traywnd", NULL);
    RECT taskbarRect;
    GetWindowRect(taskbar, &taskbarRect);
    int taskbarHeight = taskbarRect.bottom - taskbarRect.top;

    // 创建一个简单的 QWidget 对象
    QWidget widget;
    widget.setWindowTitle("Embedded Widget");
    int widgetHeight = taskbarHeight;
    int widgetWidth = taskbarHeight * 8;
    widget.resize(widgetWidth, widgetHeight);

    // 创建一个垂直布局管理器
    QVBoxLayout *layout = new QVBoxLayout(&widget);
    layout->setContentsMargins(0, 0, 0, 0); // 设置布局边距为0

    // 创建一个 QLabel 控件用于显示红色文字
    QLabel *label = new QLabel;
    label->setText("<font color='red'>Red Text</font>");
    label->setAlignment(Qt::AlignCenter);

    // 将 QLabel 添加到垂直布局中
    layout->addWidget(label);

    // 设置 QWidget 的布局
    widget.setLayout(layout);

    // 隐藏标题栏并设置为不可移动
    widget.setWindowFlag(Qt::FramelessWindowHint);
    widget.setWindowFlag(Qt::WindowStaysOnTopHint);

    widget.show();


    // 寻找任务栏的句柄
    HWND hShell = FindWindowEx(NULL, NULL, L"Shell_TrayWnd", NULL);
    if (hShell == NULL) {
        qDebug() << "Failed to find taskbar.";
        return 1;
    }

    // 寻找ReBarWindow32的句柄
    HWND hReBar = FindWindowEx(hShell, NULL, L"ReBarWindow32", NULL);
    if (hReBar == NULL) {
        qDebug() << "Failed to find ReBarWindow32.";
        return 1;
    }

    // 将窗口嵌入到ReBarWindow32容器中
    SetParent((HWND)widget.winId(), hReBar);

    // 将窗口移动到指定位置
    RECT rcReBar;
    GetWindowRect(hReBar, &rcReBar);
    int windowX = rcReBar.right - widgetWidth; // 将窗口放置在ReBarWindow32的最右侧
    int windowY = (rcReBar.bottom - rcReBar.top - widgetHeight) / 2; // 竖直居中
    if (!MoveWindow((HWND)widget.winId(), windowX, windowY, widgetWidth, widgetHeight, TRUE)) {
        qDebug() << "Failed to move embedded widget.";
        return 1;
    }

    return a.exec();
}

qml嵌入到任务栏,原理是qml文件嵌入到QQuickWidget 中,然后用它实现上一步的qwidget嵌入,注意qml的内容,不能是window等,只能是item,Rectangle等。

QQuickWidget *qmlWidget = new QQuickWidget(QUrl("qrc:/main.qml"));
    qmlWidget->setWindowFlags(Qt::FramelessWindowHint);
    qmlWidget->show();



    // 获取屏幕的尺寸
    QScreen *screen = QApplication::primaryScreen();
    QRect screenGeometry = screen->geometry();

    // 获取任务栏的高度
    HWND taskbar = FindWindow(L"Shell_traywnd", NULL);
    RECT taskbarRect;
    GetWindowRect(taskbar, &taskbarRect);
    int taskbarHeight = taskbarRect.bottom - taskbarRect.top;

    int widgetHeight = taskbarHeight;
    int widgetWidth = taskbarHeight * 2;

    // 寻找任务栏的句柄
    HWND hShell = FindWindowEx(NULL, NULL, L"Shell_TrayWnd", NULL);
    if (hShell == NULL) {
        qDebug() << "Failed to find taskbar.";
        return 1;
    }
    // 寻找ReBarWindow32的句柄
    HWND hReBar = FindWindowEx(hShell, NULL, L"ReBarWindow32", NULL);
    if (hReBar == NULL) {
        qDebug() << "Failed to find ReBarWindow32.";
        return 1;
    }
    // 将窗口嵌入到ReBarWindow32容器中
    SetParent((HWND)qmlWidget->winId(), hReBar);
    // 将窗口移动到指定位置
    RECT rcReBar;
    GetWindowRect(hReBar, &rcReBar);
    int windowX = rcReBar.right - widgetWidth * 2; // 将窗口放置在ReBarWindow32的最右侧
    int windowY = (rcReBar.bottom - rcReBar.top - widgetHeight) / 2; // 竖直居中
    if (!MoveWindow((HWND)qmlWidget->winId(), windowX, windowY, widgetWidth, widgetHeight, TRUE)) {
        qDebug() << "Failed to move embedded widget.";
        return 1;
    }

qml文件

import QtQuick 2.0

Rectangle {
    id: root
    width: 400
    height: 400
    color: "lightblue"

    MouseArea {
        id: dragArea
        anchors.fill: parent
        cursorShape: Qt.OpenHandCursor

        property var dragStartPosition: Qt.point()

        onPressed: {
            dragStartPosition = Qt.point(mouse.x, mouse.y)
        }

        onPositionChanged: {
            if (dragArea.drag.active) {
                qmlWidget.x += mouse.x - dragStartPosition.x
                qmlWidget.y += mouse.y - dragStartPosition.y
                dragStartPosition = Qt.point(mouse.x, mouse.y)
            }
        }
    }

    Text {
        text: "Hello, World!"
        anchors.centerIn: parent
        font.pointSize: 24
        color: "black"
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt窗体自定义标题栏基类是一种用于创建自定义窗体标题栏的基类。在Qt中,默认情况下,窗体的标题栏是由操作系统提供的,并且通常具有标准的布局和样式。但是,有时我们可能希望根据自己的需求,自定义窗体标题栏的外观和行为。 Qt窗体自定义标题栏基类可以通过重写一些方法和信号槽来实现自定义标题栏。使用这种基类,我们可以实现以下功能: 1. 自定义标题栏的颜色和样式:我们可以通过重写paintEvent方法来绘制自定义的标题栏,包括设置背景颜色、绘制按钮、标题等。 2. 实现窗口移动:通常情况下,窗口可以通过鼠标左键点击标题栏并拖动来移动。我们可以使用mousePressEvent、mouseMoveEvent和mouseReleaseEvent重写这些事件来实现窗口移动的功能。 3. 添加自定义按钮:我们可以在自定义标题栏中添加自定义的按钮,如最小化、最大化和关闭按钮。这些按钮可以连接到相应的槽函数来实现相应的窗口操作。 4. 响应标题栏双击事件:通常情况下,双击标题栏会触发窗口的最大化/还原操作。我们可以通过重写mouseDoubleClickEvent方法来实现此功能。 使用Qt窗体自定义标题栏基类,我们可以根据自己的需求轻松创建具有自定义外观和行为的窗体标题栏。这为我们提供了更大的自由度来设计窗口界面,并为用户提供更好的使用体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗蛋儿l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值