QT实现毛玻璃窗口

最近想做一个毛玻璃的窗口效果,经过一番折腾终于搞出了

先上效果

QT实现毛玻璃窗口_第1张图片

程序代码:

#ifndef SYSDIALOG_H
#define SYSDIALOG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include //水平
#include //垂直
#include 
class SysDialog : public QDialog
{
    Q_OBJECT

public:
    explicit SysDialog(QWidget *parent = nullptr);
    ~SysDialog();
    void paintEvent(QPaintEvent *event);
private:
    QVBoxLayout *Main_VBoxLayout;
    QHBoxLayout *Main_HBoxLayout;
    QSpinBox    *Value_SpinBox;
    QColor bgColor;
private slots:
    void Value_SpinBox_valueChanged_Slot(int v);
};

#endif // SYSDIALOG_H
#include "SysDialog.h"
#include 
#include 
//
enum AccentState
{
        ACCENT_DISABLED = 0,
        ACCENT_ENABLE_GRADIENT = 1,
        ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
        ACCENT_ENABLE_BLURBEHIND = 3,
        ACCENT_INVALID_STATE = 4
};
struct AccentPolicy
{
        AccentState AccentState;
        int AccentFlags;
        int GradientColor;
        int AnimationId;
};
enum WindowCompositionAttribute
{
        WCA_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
};
struct WindowCompositionAttributeData
{
        WindowCompositionAttribute Attribute;
        int * Data;
        int SizeOfData;
};

typedef int* (*pfun)(HWND hwnd, WindowCompositionAttributeData *data);
SysDialog::SysDialog(QWidget *parent)
    : QDialog(parent)
{

//    setWindowFlags(Qt::FramelessWindowHint);
    HWND hWnd = HWND(winId());
    HMODULE hUser = GetModuleHandle(L"user32.dll");

    if (hUser) {
        pfun setWindowCompositionAttribute = (pfun)GetProcAddress(hUser, "SetWindowCompositionAttribute");
        if (setWindowCompositionAttribute) {
//            AccentPolicy accent = { ACCENT_ENABLE_BLURBEHIND, 0x20 | 0x40 | 0x80 | 0x100, 0, 0 };
            AccentPolicy accent = { ACCENT_ENABLE_BLURBEHIND,0, 0, 0 };
            WindowCompositionAttributeData data;
            data.Attribute = WCA_ACCENT_POLICY;
            data.Data = reinterpret_cast(&accent) ;
            data.SizeOfData = sizeof(accent);
            setWindowCompositionAttribute(hWnd, &data);
        }
    }
    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
//    setWindowOpacity(1);
    setWindowTitle(QString(""));

    Main_VBoxLayout = new QVBoxLayout(this);
    Main_HBoxLayout = new QHBoxLayout;
    Value_SpinBox   = new QSpinBox;
    Value_SpinBox->setMaximum(255);
    Value_SpinBox->setValue(50);
    connect(Value_SpinBox, SIGNAL(valueChanged(int)), this, SLOT(Value_SpinBox_valueChanged_Slot(int)));



    Main_HBoxLayout->addWidget(Value_SpinBox,0);
    Main_HBoxLayout->addStretch();
    Main_HBoxLayout->setContentsMargins(0,0,0,0);
    Main_HBoxLayout->setSpacing(0);


    Main_VBoxLayout->addLayout(Main_HBoxLayout,0);
    Main_VBoxLayout->addStretch();
    Main_VBoxLayout->setContentsMargins(0,0,0,0);
    Main_VBoxLayout->setSpacing(0);
    resize(900,600);

    //
    bgColor = QColor(255, 255, 255, 100);
}

SysDialog::~SysDialog()
{
    delete Main_VBoxLayout;
}

void SysDialog::Value_SpinBox_valueChanged_Slot(int v)
{
    qDebug()<rect(), bgColor);
}

 

不过最后还是抛弃了这种方法,因为本身这种就是属于违规操作,而且还不能跨平台,所以只能不使用毛玻璃效果。

 

 

作者:大道至简

简介:一枚机器视觉工程师

公众号:QtHalcon

QT实现毛玻璃窗口_第2张图片

 

 

 

你可能感兴趣的:(QT学习,QT,毛玻璃)