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"
}
}