import sys

'''
from PyQt5.QtWidgets import QMenuBar, QHBoxLayout, QPushButton, QApplication, QWidget, QMainWindow
from PyQt5.QtCore import Qt, pyqtSlot as Slot
from PyQt5.QtGui import QCursor
'''

from PySide2.QtWidgets import QMenuBar, QHBoxLayout, QPushButton, QApplication, QWidget, QMainWindow
from PySide2.QtCore import Qt, Slot
from PySide2.QtGui import QCursor

import ctypes as ct
from ctypes import windll
from ctypes.wintypes import MSG, RECT


HTCAPTION = 2

WM_NCCALCSIZE = 131
WM_NCHITTEST = 132

HTLEFT = 10
HTRIGHT = 11
HTTOP = 12
HTTOPLEFT = 13
HTTOPRIGHT = 14
HTBOTTOM = 15
HTBOTTOMLEFT = 16
HTBOTTOMRIGHT = 17


class MARGINS(ct.Structure):
    _fields_ = [('cxLeftWidth', ct.c_int),
                ('cxRightWidth', ct.c_int),
                ('cyTopHeight', ct.c_int),
                ('cyBottomHeight', ct.c_int)]

    
class Example(QWidget):
    
    def __init__(self):
        super().__init__()

        self._bwidth = 8
        self._theight = 20
        
        self.hbox = QHBoxLayout(self)
        self.hbox.setContentsMargins(0, 0, 0, 0)
        self.hbox.setSpacing(0)
        self.hbox.setAlignment(Qt.AlignTop)

        self.menuBar = QMenuBar()
        self.menuBar.addMenu('Hello!')
        self.hbox.addWidget(self.menuBar)

        self.btnClose = QPushButton('X')
        self.btnClose.clicked.connect(self.on_btnClose_clicked)
        self.hbox.addWidget(self.btnClose)
        
        self.setGeometry(300, 300, 250, 150)

        self.hWnd = ct.c_int(self.winId())
        
        margins = MARGINS(1, 1, 1, 1)
        windll.dwmapi.DwmExtendFrameIntoClientArea(self.hWnd, ct.byref(margins))

    @Slot()
    def on_btnClose_clicked(self):
        self.close()
        
    def nativeEvent(self, eventType, message):
        try:
            # FIX: QTBUG-69074
            msg = ct.POINTER(MSG).from_address(int(message))[0]
            #msg = MSG.from_address(int(message))

            if msg.message == WM_NCCALCSIZE:
                return True, 0
            elif msg.message == WM_NCHITTEST:
                # make the borders and title bar transparents so that they are
                # handled by the native parent window
                
                wr = RECT()
                windll.user32.GetWindowRect(msg.hWnd, ct.byref(wr))
                
                x = (msg.lParam & 0xFFFF)
                y = (msg.lParam >> 16)

                borderWidth = 8

                # check if we are on a side or corner (for resizing)
                if (wr.left <= x < wr.left + borderWidth and
                    wr.bottom > y >= wr.bottom - borderWidth):
                    return True, HTBOTTOMLEFT
                elif (wr.right > x >= wr.right - borderWidth and
                      wr.bottom > y >= wr.bottom - borderWidth):
                    return True, HTBOTTOMRIGHT
                elif (wr.left <= x < wr.left + borderWidth and
                    wr.top <= y < wr.top + borderWidth):
                    return True, HTTOPLEFT
                elif (wr.right > x >= wr.right - borderWidth and
                      wr.top <= y < wr.top + borderWidth):
                    return True, HTTOPRIGHT
                elif wr.left <= x < wr.left + borderWidth:
                    return True, HTLEFT
                elif wr.right > x >= wr.right - borderWidth:
                    return True, HTRIGHT
                elif wr.bottom > y >= wr.bottom - borderWidth:
                    return True, HTBOTTOM
                elif wr.top <= y < wr.top + borderWidth:
                    return True, HTTOP
                elif QApplication.instance().widgetAt(QCursor.pos()) == self.menuBar:
                    return True, HTCAPTION
        except Exception as e:
            print(str(e))

        return False, 0

app = QApplication(sys.argv)

ex = Example()
ex.show()

sys.exit(app.exec_())
