目录
这种效果可以做背景,这样的话,开发应用程序的背景就不用愁了,目前展示的效果是win11。win10的效果不一样,win10没这么糊。
1.先看效果
2.实现代码
需要添加 winextras 模块,我用的是 vs+QT5
我继承了QWidget也就写好一个类叫CglasseFfect。 写了四个方法 修改背景。
void setGlassBackgroundR(int R); //红色通道 取值0~255
void setGlassBackgroundG(int G);//绿色通道 取值0~255
void setGlassBackgroundB(int B);//蓝色通道 取值0~255
void setGlassBackgroundA(int A);//透明通道 取值1~100 注意 至少是1,不然的话 窗口看得到摸不着
CglasseFfect .h文件
#pragma once
#include <QWidget>
#include <QPaintEvent>
#include <QtWin>
#include <QPainter>
class CglasseFfect : public QWidget
{
Q_OBJECT
public:
CglasseFfect(QWidget*parent);
~CglasseFfect();
void setGlassBackgroundR(int R);
void setGlassBackgroundG(int G);
void setGlassBackgroundB(int B);
void setGlassBackgroundA(int A);
void paintEvent(QPaintEvent* ev);
private:
QColor*bgColor = nullptr;
};
typedef enum _WINDOWCOMPOSITIONATTRIB
{
CA_UNDEFINED = 0,
WCA_NCRENDERING_ENABLED = 1,
WCA_NCRENDERING_POLICY = 2,
WCA_TRANSITIONS_FORCEDISABLED = 3,
WCA_ALLOW_NCPAINT = 4,
WCA_CAPTION_BUTTON_BOUNDS = 5,
WCA_NONCLIENT_RTL_LAYOUT = 6,
WCA_FORCE_ICONIC_REPRESENTATION = 7,
WCA_EXTENDED_FRAME_BOUNDS = 8,
WCA_HAS_ICONIC_BITMAP = 9,
WCA_THEME_ATTRIBUTES = 10,
WCA_NCRENDERING_EXILED = 11,
WCA_NCADORNMENTINFO = 12,
WCA_EXCLUDED_FROM_LIVEPREVIEW = 13,
WCA_VIDEO_OVERLAY_ACTIVE = 14,
WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15,
WCA_DISALLOW_PEEK = 16,
WCA_CLOAK = 17,
WCA_CLOAKED = 18,
WCA_ACCENT_POLICY = 19,//
WCA_FREEZE_REPRESENTATION = 20,
WCA_EVER_UNCLOAKED = 21,
WCA_VISUAL_OWNER = 22,
WCA_LAST = 23
} WINDOWCOMPOSITIONATTRIB;
typedef struct _WINDOWCOMPOSITIONATTRIBDATA
{
WINDOWCOMPOSITIONATTRIB Attrib;
PVOID pvData;
SIZE_T cbData;
} WINDOWCOMPOSITIONATTRIBDATA;
typedef enum _ACCENT_STATE
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
} ACCENT_STATE;
typedef struct _ACCENT_POLICY
{
ACCENT_STATE AccentState;
DWORD AccentFlags;
DWORD GradientColor;
DWORD AnimationId;
} ACCENT_POLICY;
WINUSERAPI
BOOL
WINAPI
GetWindowCompositionAttribute(
_In_ HWND hWnd,
_Inout_ WINDOWCOMPOSITIONATTRIBDATA* pAttrData);
typedef BOOL(WINAPI* pfnGetWindowCompositionAttribute)(HWND, WINDOWCOMPOSITIONATTRIBDATA*);
WINUSERAPI
BOOL
WINAPI
SetWindowCompositionAttribute(
_In_ HWND hWnd,
_Inout_ WINDOWCOMPOSITIONATTRIBDATA* pAttrData);
typedef BOOL(WINAPI* pfnSetWindowCompositionAttribute)(HWND, WINDOWCOMPOSITIONATTRIBDATA*);
CglasseFfect.cpp文件
#include "CglasseFfect.h"
#include <QDebug>
CglasseFfect::CglasseFfect(QWidget* parent)
: QWidget(parent)
{
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
//setWindowFlags( Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);//背景半透明属性设置 //窗口透明
HWND hWnd = HWND(this->winId());
HMODULE hUser = GetModuleHandle(L"user32.dll");
if (hUser)
{
pfnSetWindowCompositionAttribute setWindowCompositionAttribute = (pfnSetWindowCompositionAttribute)GetProcAddress(hUser, "SetWindowCompositionAttribute");
if (setWindowCompositionAttribute)
{
ACCENT_POLICY accent = { ACCENT_ENABLE_BLURBEHIND, 0, 0, 0 };
WINDOWCOMPOSITIONATTRIBDATA data;
data.Attrib = WCA_ACCENT_POLICY;
data.pvData = &accent;
data.cbData = sizeof(accent);
setWindowCompositionAttribute(hWnd, &data);
}
}
bgColor = new QColor(0, 0, 0,1);
}
CglasseFfect::~CglasseFfect()
{}
void CglasseFfect::setGlassBackgroundR(int R)
{
bgColor->setRed(R);
}
void CglasseFfect::setGlassBackgroundG(int G)
{
bgColor->setGreen(G);
}
void CglasseFfect::setGlassBackgroundB(int B)
{
bgColor->setBlue(B);
}
void CglasseFfect::setGlassBackgroundA(int A)
{
bgColor->setAlpha(A);
}
void CglasseFfect::paintEvent(QPaintEvent * ev)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(*bgColor);
//painter.drawRoundedRect(rect(), 20, 20);
painter.drawRect(0, 0, width(), height());
}
程序看起来又臭又长,不用管, 直接 ctrl+c。
![](https://i-blog.csdnimg.cn/blog_migrate/ce07edfd44b9e4a3e4efe88f40315d88.png)
3.使用方法
你可以自己把我的CglasseFfect类 复制去使用,创建QWidget去继承CglasseFfect。这样你的QWidget 就没有这么多复杂的代码,直接调用四个方法 修改自己想要的背景即可。
以下是我的实现方式
项目结构
使用类 .h文件
#pragma once
#include <QtWidgets/QWidget>
#include "ui_glassui.h"
#include "CglasseFfect.h"
class glassui : public CglasseFfect
{
Q_OBJECT
public:
glassui(QWidget *parent = nullptr);
~glassui();
void setValueR(int R);
void setValueG(int G);
void setValueB(int B);
void setValueA(int A);
public slots:
private:
Ui::glassuiClass ui;
int cruuR = 0;
int G = 0;
int B = 0;
int A = 0;
};
使用类 .cpp文件
#include "glassui.h"
#include <QDebug>
glassui::glassui(QWidget *parent)
: CglasseFfect(parent)
{
ui.setupUi(this);
QObject::connect(ui.horizontalSliderB, &QSlider::valueChanged, this, &glassui::setValueB);
QObject::connect(ui.horizontalSliderR, &QSlider::valueChanged, this, &glassui::setValueR);
QObject::connect(ui.horizontalSliderG, &QSlider::valueChanged, this, &glassui::setValueG);
QObject::connect(ui.horizontalSliderA, &QSlider::valueChanged, this, &glassui::setValueA);
}
glassui::~glassui()
{}
void glassui::setValueG(int G)
{
setGlassBackgroundR(G);
ui.spinBoxG->setValue(G);
qDebug() << "G" << G;
update();
}
void glassui::setValueB(int B)
{
setGlassBackgroundB(B);
ui.spinBoxB->setValue(B);
qDebug() << "B" << B;
update();
}
void glassui::setValueA(int A)
{
setGlassBackgroundA(A);
qDebug() << "A" << A;
ui.spinBoxA->setValue(A);
update();
}
void glassui::setValueR(int R)
{
setGlassBackgroundR(R);
qDebug() << "G" << G;
ui.spinBoxR->setValue(R);
update();
}
使用类.ui
4.窗口没有窗口表头
表头在 setWindowFlags 设置去掉了。需要做自定义窗口表头的,这样会更加好看。标题功能的基本功能 有最小化 、最大化、关闭窗口、窗口移动。 下次有得闲了再给大家制作窗口头表,欢迎卷王们来瞅瞅。