dpi适配实现

qt6
zcy 2020-12-07 00:00:45 +08:00
parent eb0505738b
commit 0b958aec4e
2 changed files with 116 additions and 2 deletions

26
Qss.cpp
View File

@ -14,6 +14,8 @@
#include <QFile> #include <QFile>
#include <QSizePolicy> #include <QSizePolicy>
#include <QDesktopWidget> #include <QDesktopWidget>
#include "windows.h"
#include "winuser.h"
#define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding #define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding
#define QSSDIALOG_BODER_WIDTH 0 #define QSSDIALOG_BODER_WIDTH 0
@ -233,7 +235,7 @@ void QssMainWindow::OnMaxOrRestore(bool max)
this->setFixedHeight(rc.height()); this->setFixedHeight(rc.height());
} }
} }
DEFINE_RESIZE(MainWindow)
QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = 0*/,float scale) QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = 0*/,float scale)
: QMainWindow(parent, flags), : QMainWindow(parent, flags),
ICallDPIChanged(), ICallDPIChanged(),
@ -597,6 +599,28 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev )
return QMainWindow::eventFilter(obj, ev); return QMainWindow::eventFilter(obj, ev);
} }
bool QssMainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* pMsg = reinterpret_cast<MSG*>(message);
switch (pMsg->message)
{
case WM_DPICHANGED:
{
qDebug()<<"DPI CHANGED";
/*DWORD dpi = LOWORD(pMsg->wParam);
//WId id = WINDOW_WINID;
//if (DPIHelper()->DPIChanged(dpi, id))
{
ScaleChanged(DPIHelper()->GetDPIScale(id));
RefrushSheet(this, id);
}*/
}
}
return QMainWindow::nativeEvent(eventType, message, result);
}
void QssMainWindow::ScaleChanged(float scale) void QssMainWindow::ScaleChanged(float scale)
{ {

92
Qss.h
View File

@ -9,17 +9,80 @@
#include <QFrame> #include <QFrame>
#include <QDockWidget> #include <QDockWidget>
#include <QPushButton> #include <QPushButton>
#include <QAbstractNativeEventFilter>
#include <QDebug>
#include "windows.h"
#include "winuser.h"
class QPushButton; class QPushButton;
class QLabel; class QLabel;
class QMouseEvent; class QMouseEvent;
#define STANDARD_DPI 96.0 #define STANDARD_DPI 96.0
#define WM_DPICHANGED 0x02e0
typedef enum{
RA_Resize = 1,
RA_FixedWidth = 2,
RA_FixedHeight = 4,
RA_FixedSize = 8,
RA_MinimumSize = 16,
RA_MinimumHeight = 32,
RA_MinimumWidth = 64,
RA_MaximumSize = 128,
RA_MaximumHeight = 256,
RA_MaximumWidth = 512
}ReiszeActions ;
#define DECLARE_RESIZE() \
void resize(int w, int h);void resize(const QSize &); void setFixedHeight(int w); \
void setFixedWidth(int w);void setFixedSize(int w, int h);void setFixedSize(const QSize &s); \
void setMinimumSize(const QSize &);void setMinimumSize(int minw, int minh); \
void setMinimumHeight(int minh);void setMinimumWidth(int minw); \
void setMaximumSize(const QSize &);void setMaximumSize(int maxw, int maxh); \
void setMaximumHeight(int minh);void setMaximumWidth(int minw);
#define DEFINE_RESIZE(name)\
void Qss##name::resize(int w, int h){ m_sizeActions |= RA_Resize; float scale = cale; m_size = QSize(w, h);Q##name::resize(m_size.width() * scale, m_size.height() * scale);}\
void Qss##name::resize(const QSize & size){ m_sizeActions |= RA_Resize; float scale = cale;m_size = size;Q##name::resize(m_size * scale);}\
void Qss##name::setFixedHeight(int h){m_sizeActions |= RA_FixedHeight;float scale = cale;m_size.setHeight(h);Q##name::setFixedHeight(m_size.height() * scale);}\
void Qss##name::setFixedWidth(int w){m_sizeActions |= RA_FixedWidth;float scale = cale;m_size.setWidth(w);Q##name::setFixedWidth(m_size.width() * scale);}\
void Qss##name::setFixedSize(int w, int h){m_sizeActions |= RA_FixedSize;float scale = cale; m_size = QSize(w, h); Q##name::setFixedSize(m_size.width() * scale, m_size.height() * scale);}\
void Qss##name::setFixedSize(const QSize & size){m_sizeActions |= RA_FixedSize;float scale = cale; m_size = size; Q##name::setFixedSize(m_size * scale);}\
void Qss##name::setMinimumSize(const QSize & size){m_sizeActions |= RA_MinimumSize;float scale = cale;m_minimumSize = size; Q##name::setMinimumSize(m_minimumSize * scale);}\
void Qss##name::setMinimumSize(int w, int h){m_sizeActions |= RA_MinimumSize;float scale = cale; m_minimumSize = QSize(w, h); Q##name::setMinimumSize(m_minimumSize.width() * scale, m_minimumSize.height() * scale);}\
void Qss##name::setMinimumHeight(int h){m_sizeActions |= RA_MinimumHeight;float scale = cale;m_minimumSize.setHeight(h); Q##name::setMinimumHeight(m_minimumSize.height() * scale);}\
void Qss##name::setMinimumWidth(int w){m_sizeActions |= RA_MinimumWidth;float scale = cale; m_minimumSize.setWidth(w); Q##name::setMinimumWidth(m_minimumSize.width() * scale);}\
void Qss##name::setMaximumSize(const QSize & size){m_sizeActions |= RA_MaximumSize;float scale = cale; m_maximumSize = size; Q##name::setMaximumSize(m_maximumSize * scale);}\
void Qss##name::setMaximumSize(int w, int h){m_sizeActions |= RA_MaximumSize;float scale = cale; m_maximumSize = QSize(w, h); Q##name::setMaximumSize(m_maximumSize.width() * scale, m_maximumSize.height() * scale);}\
void Qss##name::setMaximumHeight(int h){m_sizeActions |= RA_MaximumHeight;float scale = cale; m_maximumSize.setHeight(h); Q##name::setMaximumHeight(m_maximumSize.height() * scale);}\
void Qss##name::setMaximumWidth(int w){m_sizeActions |= RA_MaximumWidth;float scale = cale; m_maximumSize.setWidth(w); Q##name::setMaximumWidth(m_maximumSize.width() * scale);}
#define DEFINTE_SCALE_RESIZE(name)\
if (m_sizeActions.testFlag(RA_FixedWidth)){Qss##name::setFixedWidth(m_size.width() * scale);}\
if (m_sizeActions.testFlag(RA_FixedHeight)){Qss##name::setFixedHeight(m_size.height() * scale);}\
if (m_sizeActions.testFlag(RA_FixedSize)){Qss##name::setFixedSize(m_size * scale);}\
if (m_sizeActions.testFlag(RA_Resize)){ QSize newSize = m_size * scale;if(minimumSize().width() > newSize.width()){name::setMinimumSize(newSize);}name::resize(newSize);}\
if (m_sizeActions.testFlag(RA_MinimumSize)){Qss##name::setMinimumSize(m_minimumSize * scale);}\
if (m_sizeActions.testFlag(RA_MinimumHeight)){Qss##name::setMinimumHeight(m_minimumSize.height() * scale);}\
if (m_sizeActions.testFlag(RA_MinimumWidth)){Qss##name::setMinimumWidth(m_minimumSize.width() * scale);}\
if (m_sizeActions.testFlag(RA_MaximumSize)){Qss##name::setMaximumSize(m_maximumSize * scale);}\
if (m_sizeActions.testFlag(RA_MaximumHeight)){Qss##name::setMaximumHeight(m_maximumSize.height() * scale);}\
if (m_sizeActions.testFlag(RA_MaximumWidth)){ Qss##name::setMaximumWidth(m_maximumSize.width() * scale); }\
cale = scale;
typedef class ICallDPIChanged typedef class ICallDPIChanged
{ {
virtual void ScaleChanged(float scale) = 0; virtual void ScaleChanged(float scale) = 0;
virtual WId GetWID() const = 0; virtual WId GetWID() const = 0;
virtual void SetScale(float scale) = 0; virtual void SetScale(float scale) = 0;
protected:
uint32_t m_sizeActions;
float cale = 1;
QSize m_size;
QSize m_minimumSize;
QSize m_maximumSize;
}QssCallDpiChanged; }QssCallDpiChanged;
typedef class IDPIHelper typedef class IDPIHelper
@ -111,6 +174,32 @@ private:
QTitleBar_Type m_type; QTitleBar_Type m_type;
}; };
class QssEventFilter
: public QAbstractNativeEventFilter
{
public:
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE
{
MSG* pMsg = reinterpret_cast<MSG*>(message);
switch (pMsg->message)
{
case WM_DPICHANGED:
{
qDebug()<<"DPI CHANGED";
/*DWORD dpi = LOWORD(pMsg->wParam);
//WId id = WINDOW_WINID;
//if (DPIHelper()->DPIChanged(dpi, id))
{
ScaleChanged(DPIHelper()->GetDPIScale(id));
RefrushSheet(this, id);
}*/
}
}
// TODO: filter out or modify msg struct here
// ...
return false;
}
};
class QssMainWindow : public QMainWindow,ICallDPIChanged class QssMainWindow : public QMainWindow,ICallDPIChanged
{ {
@ -135,8 +224,9 @@ public:
inline QssTtitleBar* titleBar(){return m_titleBar;} inline QssTtitleBar* titleBar(){return m_titleBar;}
inline QFrame* frame(){return m_frame;} inline QFrame* frame(){return m_frame;}
int CalCursorCol(QPoint pt); //计算鼠标X的位置 int CalCursorCol(QPoint pt); //计算鼠标X的位置
DECLARE_RESIZE();
virtual bool eventFilter(QObject * obj, QEvent * ev); virtual bool eventFilter(QObject * obj, QEvent * ev);
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
private: private:
void onMouseMoveEvent(QMouseEvent * ev); void onMouseMoveEvent(QMouseEvent * ev);