代码如下:
import tkinter as tk
import win32api
import win32con
import pywintypes
import os
import threading
import time
from screeninfo import get_monitors
from PIL import Image, ImageDraw, ImageFont,ImageTk
def get_total_screen_size():
monitors = get_monitors()
width = sum(monitor.width for monitor in monitors)
height = max(monitor.height for monitor in monitors)
return width, height
font_family = "msyh.ttf" # Replace with your actual font filename
font_size = 36
def generate_watermark(text, width, height,color='#d5d5d5', opacity=0, lines=10, angle=0):
get_total_screen_size()
spacing = 100
# 创建一个 Image 对象,用于绘制水印
img = Image.new('RGBA', (width, height), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(font_family, font_size)
# 将文本拆分成多行
text_lines = text.split('\n') if text else lines
# 计算每个水印的宽度和高度
bbox = draw.textbbox((0, 0), text_lines[0], font=font)
text_width, text_height = bbox[2] - bbox[0] + spacing, bbox[3] - bbox[1] + spacing
# 计算每行可以容纳的水印数量
watermarks_per_row = width // (text_width + spacing)
total_rows = (height - 50) // (text_height + spacing)
# 设置初始位置
x = 50
y = 30
for row in range(0,total_rows):
x = 50 # 每行重新从左侧开始
y = 50 + row * (text_height + spacing)
for col in range(0,watermarks_per_row):
rotated_text = Image.new('RGBA', (text_width, text_height), (255, 255, 255, 0))
rotated_draw = ImageDraw.Draw(rotated_text)
rotated_draw.text((0, 0), text_lines[row % len(text_lines)], font=font, fill=color)
rotated_text = rotated_text.rotate(angle, expand=True)
# 将旋转后的文本粘贴到主图像上
img.paste(rotated_text, (x, y), rotated_text)
x += text_width + spacing
tk_img = ImageTk.PhotoImage(img)
# 创建一个 Label 并显示图片
label = tk.Label(image=tk_img,bg="white")
label.image = tk_img # 保持引用,防止垃圾回收
label.pack()
def create_watermark_window(text, **kwargs):
root = tk.Tk()
def generate_watermark_with_size():
screen_width, screen_height = get_total_screen_size()
generate_watermark(text, screen_width, screen_height, **kwargs)
def monitor_resolution_changes():
nonlocal generate_watermark_with_size
prev_monitor_count = len(get_monitors())
while True:
current_monitor_count = len(get_monitors())
if current_monitor_count != prev_monitor_count:
generate_watermark_with_size()
prev_monitor_count = current_monitor_count
time.sleep(3)
monitor_thread = threading.Thread(target=monitor_resolution_changes)
monitor_thread.daemon = True
monitor_thread.start()
# 使用 after 方法延迟执行生成水印函数
root.after(100, generate_watermark_with_size)
root.overrideredirect(True)
root.lift()
root.attributes('-alpha', 0.3) # 设置透明度
root.wm_attributes('-topmost', True)
root.wm_attributes('-disabled', True)
root.wm_attributes('-transparentcolor',"white")
hWindow = pywintypes.HANDLE(int(root.frame(), 16))
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)
root.mainloop()
# 使用示例
if __name__ == '__main__':
# 获取当前用户名
username = os.getlogin()
# 组合文本
mytext = f"Allianz\n{username}" # 使用 f-string 格式化字符串
angle = 45 # 旋转角度
create_watermark_window(mytext, angle=angle, lines=10)
这个代码在切换一次外接屏幕后,只绘制了主屏幕,副屏不能绘制。Windows 11 系统 恳请各位赐教一下
1
life90 OP 或者说有什么办法,让他检测到处于扩展屏状态,重新启动一次程序。
|
2
paopjian 19 天前
我看不懂 python 代码,但是看你好像是起了个多线程持续监控屏幕数量?current_monitor_count 和 prev_monitor_count 不匹配就换,这我理解是屏幕数改变就触发重绘水印,可是切换屏幕意思就是屏幕还是 1 个吧,那不会触发重绘.
|