def nativeEventFilter(self, typ, sip_voidptr): msg = MSG.from_address(sip_voidptr.__int__()) if msg.message == win32con.WM_HOTKEY: if self.enable is True: self.hotKey.setActive(True, HIWORD(msg.lParam), LOWORD(msg.lParam)) self.count += 1 return False, 1
def nativeEvent(self, eventType, message): """ 处理windows消息 """ msg = MSG.from_address(message.__int__()) if msg.message == win32con.WM_NCHITTEST: # 解决多屏下会出现鼠标一直为拖动状态的问题 xPos = (win32api.LOWORD(msg.lParam) - self.frameGeometry().x()) % 65536 yPos = win32api.HIWORD(msg.lParam) - self.frameGeometry().y() w, h = self.width(), self.height() lx = xPos < self.BORDER_WIDTH rx = xPos + 9 > w - self.BORDER_WIDTH ty = yPos < self.BORDER_WIDTH by = yPos > h - self.BORDER_WIDTH if lx and ty: return True, win32con.HTTOPLEFT elif rx and by: return True, win32con.HTBOTTOMRIGHT elif rx and ty: return True, win32con.HTTOPRIGHT elif lx and by: return True, win32con.HTBOTTOMLEFT elif ty: return True, win32con.HTTOP elif by: return True, win32con.HTBOTTOM elif lx: return True, win32con.HTLEFT elif rx: return True, win32con.HTRIGHT elif msg.message == win32con.WM_NCCALCSIZE: if self.isWindowMaximized(msg.hWnd): self.monitorNCCALCSIZE(msg) return True, 0 elif msg.message == win32con.WM_GETMINMAXINFO: if self.isWindowMaximized(msg.hWnd): window_rect = win32gui.GetWindowRect(msg.hWnd) if not window_rect: return False, 0 # 获取显示器句柄 monitor = win32api.MonitorFromRect(window_rect) if not monitor: return False, 0 # 获取显示器信息 monitor_info = win32api.GetMonitorInfo(monitor) monitor_rect = monitor_info['Monitor'] work_area = monitor_info['Work'] # 将lParam转换为MINMAXINFO指针 info = cast(msg.lParam, POINTER(MINMAXINFO)).contents # 调整窗口大小 info.ptMaxSize.x = work_area[2] - work_area[0] info.ptMaxSize.y = work_area[3] - work_area[1] info.ptMaxTrackSize.x = info.ptMaxSize.x info.ptMaxTrackSize.y = info.ptMaxSize.y # 修改左上角坐标 info.ptMaxPosition.x = abs(window_rect[0] - monitor_rect[0]) info.ptMaxPosition.y = abs(window_rect[1] - monitor_rect[1]) return True, 1 return QWidget.nativeEvent(self, eventType, message)
def nativeEvent (self, eventType, message): if eventType == 'windows_generic_MSG': msg = MSG.from_address(message.__int__()) if msg.message==WM_LBUTTONDBLCLK: self.mouseDoubleClicked.emit(msg.lParam & 0x0000ffff, msg.lParam >> 16) elif msg.message==WM_LBUTTONDOWN: self.mousePressed.emit(msg.lParam & 0x0000ffff, msg.lParam >> 16) # Return True if the event should be filtered, i.e. stopped. # Return False to allow normal Qt processing to continue return False, 0
def nativeEvent(self, eventType, msg): # 定制nativeEvent使其处理本地(Windows)窗口消息 message = MSG.from_address(int(msg)) if message.message == WM_USER: self.displayTimer.stop() global G_CommuCounter, Time_LastCommu, CQwHandle G_CommuCounter += 1 Time_LastCommu = time.time() self.n_triggerCount.display(G_CommuCounter) if message.wParam == Breath: # 回应前端的心跳包 try: win32api.PostMessage(CQwHandle, WM_USER, Breath, 0x79) self.s_signal_out.setChecked(True) self.displayTimer.start(300) except Exception as errInfo: global Time_LastError, Status_Hint self.displayRefuresh() self.displayTimer.stop() Status_Hint = "发生异常, 等待处理..." self.o_currentStatus.setText(Status_Hint) logger1.error("回应前端心跳包出错: %s" % errInfo) Time_LastError = time.time() logger1.info("尝试更新前端窗口句柄") try: CQwHandle = win32gui.FindWindow(None, CQTitle) logger1.info("更新前端窗口句柄成功. 新句柄 → 0x%x" % CQwHandle) Status_Hint = "等待新任务中..." self.o_currentStatus.setText(Status_Hint) except Exception as errInfo: logger1.error("尝试重新查找前端句柄失败: %s" % errInfo) else: global WPA, LPA WPA, LPA = message.wParam, message.lParam if WPA == CQreturn: printc("---前端已执行命令, 返回结果: `%s`" % LPA) elif WPA == AppUpdate: proc = ThreadWithReturn(target=Update, args=()) proc.start() pass else: printc( f"---前端通信{message.message}(WM_USER): {WPA}({hex(WPA)}), {LPA}({hex(LPA)})---", style="color:#6daa6d") self.s_signal_in.setChecked(True) self.displayTimer.start(300) return False, 0
def nativeEvent(self, eventType, message): """ handle the Windows message """ msg = MSG.from_address(message.__int__()) if msg.message == win32con.WM_NCHITTEST: pos = QCursor.pos() xPos = pos.x() - self.x() yPos = pos.y() - self.y() w, h = self.width(), self.height() lx = xPos < self.BORDER_WIDTH rx = xPos > w - self.BORDER_WIDTH ty = yPos < self.BORDER_WIDTH by = yPos > h - self.BORDER_WIDTH if lx and ty: return True, win32con.HTTOPLEFT elif rx and by: return True, win32con.HTBOTTOMRIGHT elif rx and ty: return True, win32con.HTTOPRIGHT elif lx and by: return True, win32con.HTBOTTOMLEFT elif ty: return True, win32con.HTTOP elif by: return True, win32con.HTBOTTOM elif lx: return True, win32con.HTLEFT elif rx: return True, win32con.HTRIGHT elif msg.message == win32con.WM_NCCALCSIZE: if self._isWindowMaximized(msg.hWnd): self.__monitorNCCALCSIZE(msg) return True, 0 elif msg.message == win32con.WM_GETMINMAXINFO: if self._isWindowMaximized(msg.hWnd): window_rect = win32gui.GetWindowRect(msg.hWnd) if not window_rect: return False, 0 # get the monitor handle monitor = win32api.MonitorFromRect(window_rect) if not monitor: return False, 0 # get the monitor information __monitorInfo = win32api.GetMonitorInfo(monitor) monitor_rect = __monitorInfo['Monitor'] work_area = __monitorInfo['Work'] # convert lParam to MINMAXINFO pointer info = cast(msg.lParam, POINTER(MINMAXINFO)).contents # adjust the size of window info.ptMaxSize.x = work_area[2] - work_area[0] info.ptMaxSize.y = work_area[3] - work_area[1] info.ptMaxTrackSize.x = info.ptMaxSize.x info.ptMaxTrackSize.y = info.ptMaxSize.y # modify the upper left coordinate info.ptMaxPosition.x = abs(window_rect[0] - monitor_rect[0]) info.ptMaxPosition.y = abs(window_rect[1] - monitor_rect[1]) return True, 1 return QWidget.nativeEvent(self, eventType, message)
def parse_message(message): "Parse Windows message" return MSG.from_address(message.__int__())