想实现用win32 API在tkinter窗口上画图,那么应该先获得tkinter窗口的句柄hwnd,然后再获得tkinter的设备hdc。尝试了FindWindow(),GetActiveWindow(),GetForegroundWindow()这几个函数貌似都不能正确获得tkinter的句柄
后来在stackoverflow上看到一个列子,使用pywintypes.HANDLE(int(root.frame(), 16))才成功。
原例:https://stackoverflow.com/questions/25602986/using-python-tkinter-how-to-smoothly-move-and-resize-the-non-focusable-window
这里我把原例中创建一个non focusable window的tkinter窗口,修改为把窗口设置样式设置为WS_POPUP
importtkinter as tkimportwin32apiimportwin32conimportpywintypes
root=tk.Tk()
root.geometry("+0+0")
root.lift()
hWindow= pywintypes.HANDLE(int(root.frame(), 16))
win32api.SetWindowLong(hWindow,win32con.GWL_STYLE,win32con.WS_POPUP)
root.mainloop()