1
0
mirror of https://github.com/jaredtao/TaoQuick.git synced 2025-02-06 21:48:24 +08:00

150 lines
4.7 KiB
C++
Raw Normal View History

2019-12-18 13:26:44 +08:00
#include "TaoView.h"
2020-06-14 13:28:35 +08:00
2020-07-02 23:57:46 +08:00
#include <QCursor>
2020-07-02 23:13:54 +08:00
#include <QGuiApplication>
2020-06-14 00:57:36 +08:00
#include <QQmlContext>
2019-07-18 10:17:02 +08:00
#include <QQmlEngine>
#include <QQuickItem>
2020-07-05 02:12:59 +08:00
#include <QScreen>
2020-07-02 23:13:54 +08:00
#if WIN32
#include <WinUser.h>
#include <dwmapi.h>
#include <objidl.h> // Fixes error C2504: 'IUnknown' : base class undefined
#include <windows.h>
#include <windowsx.h>
#pragma comment(lib, "Dwmapi.lib") // Adds missing library, fixes error LNK2019: unresolved external symbol __imp__DwmExtendFrameIntoClientArea
#pragma comment(lib, "user32.lib")
#endif
2020-07-02 23:57:46 +08:00
const LONG border_width = 6;
2020-07-02 23:13:54 +08:00
TaoView::TaoView(QWindow* parent)
: QQuickView(parent)
2019-07-18 10:17:02 +08:00
{
2020-07-11 00:37:07 +08:00
setFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
2019-07-18 10:17:02 +08:00
setResizeMode(SizeRootObjectToView);
setColor(QColor(Qt::transparent));
2020-07-08 00:36:35 +08:00
resize(1440, 900);
2020-07-02 23:13:54 +08:00
#if WIN32
DWORD style = ::GetWindowLong(HWND(winId()), GWL_STYLE);
style |= WS_MINIMIZEBOX | WS_THICKFRAME | WS_CAPTION;
::SetWindowLong(HWND(winId()), GWL_STYLE, style);
2020-07-08 00:36:35 +08:00
// MARGINS rect{1, 1, -1, -1};
// DwmExtendFrameIntoClientArea(HWND(winId()), &rect);
2020-07-02 23:13:54 +08:00
#endif
2020-07-03 00:21:44 +08:00
setIsMax(windowState() == Qt::WindowMaximized);
connect(this, &QWindow::windowStateChanged, this, [&](Qt::WindowState state){
setIsMax(state == Qt::WindowMaximized);
});
2019-07-18 10:17:02 +08:00
}
TaoView::~TaoView()
{
}
2019-08-03 17:47:27 +08:00
void TaoView::moveToScreenCenter()
{
auto screenGeo = qApp->primaryScreen()->geometry();
auto viewGeo = geometry();
2020-07-02 23:13:54 +08:00
QPoint centerPos = { (screenGeo.width() - viewGeo.width()) / 2, (screenGeo.height() - viewGeo.height()) / 2 };
2019-08-03 17:47:27 +08:00
setPosition(centerPos);
}
2020-07-02 23:57:46 +08:00
2020-07-03 00:21:44 +08:00
void TaoView::setIsMax(bool isMax)
2020-07-02 23:57:46 +08:00
{
2020-07-03 00:21:44 +08:00
if (m_isMax == isMax)
return;
2020-07-02 23:57:46 +08:00
2020-07-03 00:21:44 +08:00
m_isMax = isMax;
emit isMaxChanged(m_isMax);
2020-07-02 23:57:46 +08:00
}
2020-07-02 23:13:54 +08:00
#if WIN32
bool TaoView::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
#if (QT_VERSION == QT_VERSION_CHECK(5, 11, 1))
MSG* msg = *reinterpret_cast<MSG**>(message);
#else
MSG* msg = reinterpret_cast<MSG*>(message);
#endif
switch (msg->message) {
case WM_NCCALCSIZE: {
NCCALCSIZE_PARAMS& params = *reinterpret_cast<NCCALCSIZE_PARAMS*>(msg->lParam);
if (!(windowStates() & Qt::WindowFullScreen) && !(windowStates() & Qt::WindowMaximized)) {
if (params.rgrc[0].top != 0)
params.rgrc[0].top -= 1;
}
//this kills the window frame and title bar we added with WS_THICKFRAME and WS_CAPTION
*result = WVR_REDRAW;
return true;
}
case WM_NCHITTEST: {
*result = 0;
RECT winrect;
GetWindowRect(HWND(winId()), &winrect);
long x = GET_X_LPARAM(msg->lParam);
long y = GET_Y_LPARAM(msg->lParam);
bool resizeWidth = minimumWidth() != maximumWidth();
bool resizeHeight = minimumHeight() != maximumHeight();
if (resizeWidth) {
//left border
if (x >= winrect.left && x < winrect.left + border_width) {
*result = HTLEFT;
}
//right border
if (x < winrect.right && x >= winrect.right - border_width) {
*result = HTRIGHT;
}
}
if (resizeHeight) {
//bottom border
if (y < winrect.bottom && y >= winrect.bottom - border_width) {
*result = HTBOTTOM;
}
//top border
if (y >= winrect.top && y < winrect.top + border_width) {
*result = HTTOP;
}
}
if (resizeWidth && resizeHeight) {
//bottom left corner
if (x >= winrect.left && x < winrect.left + border_width && y < winrect.bottom && y >= winrect.bottom - border_width) {
*result = HTBOTTOMLEFT;
}
//bottom right corner
if (x < winrect.right && x >= winrect.right - border_width && y < winrect.bottom && y >= winrect.bottom - border_width) {
*result = HTBOTTOMRIGHT;
}
//top left corner
if (x >= winrect.left && x < winrect.left + border_width && y >= winrect.top && y < winrect.top + border_width) {
*result = HTTOPLEFT;
}
//top right corner
if (x < winrect.right && x >= winrect.right - border_width && y >= winrect.top && y < winrect.top + border_width) {
*result = HTTOPRIGHT;
}
}
2020-07-03 00:21:44 +08:00
if (0 != *result) {
return true;
}
double dpr = qApp->devicePixelRatio();
QPoint pos = mapFromGlobal(QPoint(x/dpr,y/dpr));
2020-07-08 20:12:52 +08:00
QRect titleRect(border_width, border_width, width() * 0.8, 50);
2020-07-03 00:21:44 +08:00
if (titleRect.contains(pos))
{
*result = HTCAPTION;
return true;
}
return false;
2020-07-02 23:13:54 +08:00
} //end case WM_NCHITTEST
}
return QQuickView::nativeEvent(eventType, message, result);
}
#endif