Welcome, guest | Sign In | My Account | Store | Cart

Create a window which sticks to screen edges using PyQt4 under Windows.

Python, 36 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
import sys, ctypes
class WINDOWPOS(ctypes.Structure):
    _fields_ = [
        ('hwnd', ctypes.c_ulong),
        ('hwndInsertAfter', ctypes.c_ulong),
        ('x', ctypes.c_int),
        ('y', ctypes.c_int),
        ('cx', ctypes.c_int),
        ('cy', ctypes.c_int),
        ('flags', ctypes.c_ulong)
    ]
    
WM_WINDOWPOSCHANGING = 0x46 #Sent to a window whose size, position, or place in the Z order is about to change

class AuMainWindow(QtGui.QMainWindow):
    def winEvent(self, message):
        if message.message == WM_WINDOWPOSCHANGING:
            stickAt = 10 #px near screen edge
            pos = WINDOWPOS.from_address(message.lParam)
            mon = QApplication.desktop().availableGeometry(self)
            if abs(pos.x - mon.left()) <= stickAt:
                pos.x = mon.left()
            elif abs(pos.x + pos.cx - mon.right()) <= stickAt:
                pos.x = mon.right() - pos.cx
            if abs(pos.y - mon.top()) <= stickAt:
                pos.y = mon.top()
            elif abs(pos.y + pos.cy - mon.bottom()) <= stickAt:
                pos.y = mon.bottom() - pos.cy
        return False, 0 
                
app = QtGui.QApplication(sys.argv)
mainwnd = AuMainWindow()
mainwnd.show()
sys.exit(app.exec_())