This commit is contained in:
朱子楚\zhuzi 2023-12-19 20:02:15 +08:00
parent f4112ee5dc
commit 61659b5e31
5 changed files with 44 additions and 23 deletions

View File

@ -1,10 +1,12 @@
#include "FluFrameless.h"
#include "FluFramelessHelper.h"
#include <QGuiApplication>
#include <QOperatingSystemVersion>
#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#pragma comment (lib,"user32.lib")
#pragma comment (lib,"dwmapi.lib")
#include <windows.h>
#include <dwmapi.h>
static inline QByteArray qtNativeEventType()
{
@ -28,6 +30,26 @@ static inline bool isCompositionEnabled(){
return false;
}
static inline void showShadow(HWND hwnd){
if(isCompositionEnabled()){
const MARGINS shadow = { 1, 1, 1, 1 };
typedef HRESULT (WINAPI* DwmExtendFrameIntoClientAreaPtr)(HWND hWnd, const MARGINS *pMarInset);
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module)
{
DwmExtendFrameIntoClientAreaPtr dwm_extendframe_into_client_area_;
dwm_extendframe_into_client_area_= reinterpret_cast<DwmExtendFrameIntoClientAreaPtr>(GetProcAddress(module, "DwmExtendFrameIntoClientArea"));
if (dwm_extendframe_into_client_area_)
{
dwm_extendframe_into_client_area_(hwnd, &shadow);
}
}
}else{
ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW;
SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle);
}
}
#endif
FramelessEventFilter::FramelessEventFilter(QQuickWindow* window){
@ -86,15 +108,15 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
return false;
}
FluFrameless::FluFrameless(QObject *parent)
FluFramelessHelper::FluFramelessHelper(QObject *parent)
: QObject{parent}
{
}
void FluFrameless::classBegin(){
void FluFramelessHelper::classBegin(){
}
void FluFrameless::updateCursor(int edges){
void FluFramelessHelper::updateCursor(int edges){
switch (edges) {
case 0:
_window->setCursor(Qt::ArrowCursor);
@ -118,7 +140,7 @@ void FluFrameless::updateCursor(int edges){
}
}
bool FluFrameless::eventFilter(QObject *obj, QEvent *ev){
bool FluFramelessHelper::eventFilter(QObject *obj, QEvent *ev){
if (!_window.isNull() && _window->flags() & Qt::FramelessWindowHint) {
static int edges = 0;
@ -178,7 +200,7 @@ bool FluFrameless::eventFilter(QObject *obj, QEvent *ev){
return QObject::eventFilter(obj, ev);
}
void FluFrameless::componentComplete(){
void FluFramelessHelper::componentComplete(){
auto o = parent();
while (nullptr != o) {
_window = (QQuickWindow*)o;
@ -190,11 +212,10 @@ void FluFrameless::componentComplete(){
_nativeEvent =new FramelessEventFilter(_window);
qApp->installNativeEventFilter(_nativeEvent);
HWND hwnd = reinterpret_cast<HWND>(_window->winId());
ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW;
SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle);
DWORD style = GetWindowLongPtr(hwnd,GWL_STYLE);
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION &~ WS_SYSMENU);
SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE |SWP_FRAMECHANGED);
showShadow(hwnd);
#endif
_stayTop = QQmlProperty(_window,"stayTop");
_stayTop.connectNotifySignal(this,SLOT(_onStayTopChange()));
@ -204,12 +225,12 @@ void FluFrameless::componentComplete(){
}
}
void FluFrameless::_onScreenChanged(){
void FluFramelessHelper::_onScreenChanged(){
_window->update();
QGuiApplication::processEvents();
}
void FluFrameless::_onStayTopChange(){
void FluFramelessHelper::_onStayTopChange(){
bool isStayTop = _stayTop.read().toBool();
#ifdef Q_OS_WIN
HWND hwnd = reinterpret_cast<HWND>(_window->winId());
@ -225,7 +246,7 @@ void FluFrameless::_onStayTopChange(){
#endif
}
FluFrameless::~FluFrameless(){
FluFramelessHelper::~FluFramelessHelper(){
if (!_window.isNull()) {
_window->setFlags(Qt::Window);
#ifdef Q_OS_WIN

View File

@ -1,5 +1,5 @@
#ifndef FLUFRAMELESS_H
#define FLUFRAMELESS_H
#ifndef FLUFRAMELESSHELPER_H
#define FLUFRAMELESSHELPER_H
#include <QObject>
#include <QQuickWindow>
@ -25,13 +25,13 @@ public:
qint64 _current = 0;
};
class FluFrameless : public QObject, public QQmlParserStatus
class FluFramelessHelper : public QObject, public QQmlParserStatus
{
Q_OBJECT
QML_NAMED_ELEMENT(FluFrameless)
QML_NAMED_ELEMENT(FluFramelessHelper)
public:
explicit FluFrameless(QObject *parent = nullptr);
~FluFrameless();
explicit FluFramelessHelper(QObject *parent = nullptr);
~FluFramelessHelper();
void classBegin() override;
void componentComplete() override;
protected:
@ -47,4 +47,4 @@ private:
QQmlProperty _screen;
};
#endif // FLUFRAMELESS_H
#endif // FLUFRAMELESSHELPER_H

View File

@ -16,7 +16,7 @@
#include "Screenshot.h"
#include "FluRectangle.h"
#include "FluNetwork.h"
#include "FluFrameless.h"
#include "FluFramelessHelper.h"
#include "QRCode.h"
void FluentUI::registerTypes(QQmlEngine *engine){
@ -41,7 +41,7 @@ void FluentUI::registerTypes(const char *uri){
qmlRegisterType<FluRectangle>(uri,major,minor,"FluRectangle");
qmlRegisterType<NetworkCallable>(uri,major,minor,"FluNetworkCallable");
qmlRegisterType<NetworkParams>(uri,major,minor,"FluNetworkParams");
qmlRegisterType<FluFrameless>(uri,major,minor,"FluFrameless");
qmlRegisterType<FluFramelessHelper>(uri,major,minor,"FluFramelessHelper");
qmlRegisterType(QUrl("qrc:/qt/qml/FluentUI/Controls/ColorPicker/ColorPicker.qml"),uri,major,minor,"ColorPicker");
qmlRegisterType(QUrl("qrc:/qt/qml/FluentUI/Controls/ColorPicker/Content/Checkerboard.qml"),uri,major,minor,"Checkerboard");

View File

@ -92,7 +92,7 @@ Window {
}
Component{
id:com_frameless
FluFrameless{}
FluFramelessHelper{}
}
Component{
id:com_background

View File

@ -91,7 +91,7 @@ Window {
}
Component{
id:com_frameless
FluFrameless{}
FluFramelessHelper{}
}
Component{
id:com_background