注 : 由python实现
消息提示参考 : python windows右下角消息提示 - abcyrf - 博客园
1.前言
1.1.效果图
1.2.发布说明
发布时间 : 2025-07-02
无需python环境直接运行的程序包:
- windows : 下载
一句话功能说明 : 复制文本或者文件内容到粘贴板
详细功能说明 :
- 创建快捷方式设置要复制的文本或者文件内容
2.安装依赖
pip3 install pyinstaller pywin32 -i https://mirrors.aliyun.com/pypi/simple/ requests
3.代码(copy_ztb.py)
# -*- coding: utf-8 -*-
"""
# python版本
3.4+
# 安装依赖
pip3 install pyinstaller pywin32 -i https://mirrors.aliyun.com/pypi/simple/ requests
# 打包
pyinstaller -F -w -i F:\my\imgs\图标\21.png --clean --noconfirm copy_ztb.py
"""
import argparse
import ctypes
import sys
from pathlib import Path
import win32clipboard
import win32gui
import win32con
import time
class TestTaskbarIcon:
def __init__(self):
# 注册一个窗口类
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32gui.GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbarDemo"
wc.lpfnWndProc = {win32con.WM_DESTROY: self.OnDestroy, }
classAtom = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow(classAtom, "Taskbar Demo", style,
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, hinst, None)
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
nid = (self.hwnd, 0, win32gui.NIF_ICON, win32con.WM_USER + 20, hicon, "Demo")
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
def showMsg(self, title, msg):
# 原作者使用Shell_NotifyIconA方法代替包装后的Shell_NotifyIcon方法
# 据称是不能win32gui structure, 我稀里糊涂搞出来了.
# 具体对比原代码.
nid = (self.hwnd, # 句柄
0, # 托盘图标ID
win32gui.NIF_INFO, # 标识
0, # 回调消息ID
0, # 托盘图标句柄
"TestMessage", # 图标字符串
msg, # 气球提示字符串
0, # 提示的显示时间
title, # 提示标题
win32gui.NIIF_INFO # 提示用到的图标
)
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # Terminate the app.
def copy_text(text):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
win32clipboard.CloseClipboard()
t = TestTaskbarIcon()
t.showMsg("提示", "复制成功")
time.sleep(1)
win32gui.DestroyWindow(t.hwnd)
def read_file(filepath):
try:
file_path = Path(filepath)
content = file_path.read_text(encoding='utf-8')
except Exception:
ctypes.windll.user32.MessageBoxW(0, f"文件{filepath}读取内容失败, 不支持的文件类型", "警告", 0x10)
sys.exit(1)
return content
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='将指定文本或文件内容复制到剪贴板。'
'当 type 为 text 时,target 表示要复制的文本;'
'当 type 为 file 时,target 应为文件路径,程序会读取文件内容并复制。'
)
parser.add_argument('--type', type=str, choices=['text', 'file'], default='text',
help='复制类型:text 表示直接复制文本;file 表示复制指定文件的内容(默认为 text)')
parser.add_argument('--target', type=str,
default='这是默认复制到剪贴板的文本,请设置 target 参数内容',
help='当 type=text 时,表示要复制的文本内容;'
'当 type=file 时,表示要读取并复制的文件路径')
args, _ = parser.parse_known_args()
type = args.type
target = args.target
if type == 'file':
copy_text(read_file(target))
else:
copy_text(target)
4.打包
pyinstaller -F -w -i F:\my\imgs\图标\21.png copy_ztb.py --clean --noconfirm
5.使用方法
5.1.直接运行(无意义)
复制到粘贴板的为默认信息, 如下:
这是默认复制到剪贴板的文本,请设置 target 参数内容
5.2.创建快捷方式-复制文本
目标 :
F:\test\copy_ztb.exe --target=你好...
复制到粘贴板内容如下:
你好...
5.3.创建快捷方式-复制文件内容
目标 :
F:\test\copy_ztb.exe --type=file --target=1.txt
复制到粘贴板内容如下:
哈喽....