cluic / wxauto

Windows版本微信客户端(非网页版)自动化,可实现简单的发送、接收微信消息,简单微信机器人
MIT License
2.88k stars 465 forks source link

表情包代码,自取 #262

Open zz-big opened 2 months ago

zz-big commented 2 months ago

增加个表情包代码,截图实现,不需要窗口置顶。 动图也是截个图 图片也可以用这个方法实现,但是有的图片不下载 直接截图不清晰

安装 PyQt5

elements.py文件新增方法

from PyQt5.QtWidgets import QApplication

def _download_emo(self, msgitem, id=None):
        self._show()
        emocontrol = msgitem.ButtonControl(Name='')
        if not emocontrol.Exists(0.5):
            return None
        RollIntoView(self.C_MsgList, emocontrol)

        rect_hwnd = win32gui.GetWindowRect(self.HWND)
        x_rect_hwnd = rect_hwnd[0]
        y_rect_hwnd = rect_hwnd[1]

        rect = emocontrol.BoundingRectangle
        x_rect = rect.left
        y_rect = rect.top
        width_rect = rect.width()
        height_rect = rect.height()

        x_grab = x_rect - x_rect_hwnd
        y_grab = y_rect - y_rect_hwnd
        width_grab = width_rect
        height_grab = height_rect

        app = QApplication(sys.argv)
        screen = QApplication.primaryScreen()
        img = screen.grabWindow(self.HWND, x=x_grab, y=y_grab, width=width_grab, height=height_grab).toImage()
        if id is None:
            file_name = f"动画表情_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}.jpg"
        else:
            file_name = f"动画表情_{id}.jpg"
        savepath = os.path.join(WxParam.DEFALUT_SAVEPATH, file_name)
        if not os.path.exists(os.path.split(savepath)[0]):
            os.makedirs(os.path.split(savepath)[0])
        img.save(savepath)
        return savepath

_getmsgs 方法中 添加:

  elif msg.content.startswith(f"[{self._lang('动画表情')}]") and savepic:
      filepath = self._download_emo(msg.control, msg.id)
      msg.content = filepath if filepath else msg.content
        msgtypes = [
            f"[{self._lang('图片')}]",
            f"[{self._lang('文件')}]",
            f"[{self._lang('语音')}]",
            f"[{self._lang('动画表情')}]"
        ]

languages.py 中 添加 动画表情

# 消息类型
'链接': {'cn': '链接', 'cn_t': '鏈接', 'en': 'Link'}, 
'视频': {'cn': '视频', 'cn_t': '視頻', 'en': 'Video'}, 
'动画表情': {'cn': '动画表情', 'cn_t': '动画表情', 'en': '动画表情'},
'图片': {'cn': '图片', 'cn_t': '圖片', 'en': 'Photo'},
'文件': {'cn': '文件', 'cn_t': '文件', 'en': 'File'},
'语音': {'cn': '语音', 'cn_t': '語音', 'en': 'Voice'},
'查看更多消息': {'cn': '查看更多消息', 'cn_t': '', 'en': ''},