Python win32 API to mimic AutoHotKey Tooltip UI

553 views Asked by At

I would like to mimic the tooltip functionality found in AutoHotKey.

AutoHotKey code:

infoTooltip() {
    MouseGetPos, xPos, yPos, winId
    PixelGetColor, color, %xPos%, %yPos%, RGB
    WinGetTitle, winTitle, ahk_id %winId%
    ToolTip "%winTitle%"`n%xPos% %yPos% %color%
}

Looks like this when called inside a loop and will follow mouse around: Example of what I want

References found from googling:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx

How to use Windows ToolTip Control without bounding to a tool

My Python code attempt so far:

import win32con, win32gui
from win32api import GetModuleHandle
from commctrl import (TOOLTIPS_CLASS, TTS_ALWAYSTIP, TTS_NOPREFIX, TTM_ADDTOOL, TTM_SETMAXTIPWIDTH)
class TooltipWindow:
    def __init__(self):
        win32gui.InitCommonControls() # Loads COMCTL32.DLL (Shell Common Controls Library)
        self.hwnd = win32gui.CreateWindowEx(
            win32con.WS_EX_CLIENTEDGE | win32con.WS_EX_TOPMOST,
            TOOLTIPS_CLASS,
            "MyTooltipWindow",
            win32con.WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | win32con.WS_BORDER,
            win32con.CW_USEDEFAULT,
            win32con.CW_USEDEFAULT,
            win32con.CW_USEDEFAULT,
            win32con.CW_USEDEFAULT,
            None,
            None,
            GetModuleHandle(None),
            None
        )
        win32gui.SendMessage(self.hwnd, TTM_ADDTOOL, None, ???)

tooltip = TooltipWindow()

I have no idea how to translate the TOOLINFO struct to the lparam argument. Also, going in blind trying to get most of this code working as this is my first real win32gui project.

Any help is appreciated, thank you!

2

There are 2 answers

2
nelsontruran On

Have you considered instead doing a Dll import? It's definitely worth lugging around the .dll it if you are planning on using more authotkey functionality.

import ctypes
dll = ctypes.cdll.LoadLibrary("AutoHotkey.dll")
dll.ahktextdll(u"")
dll.ahkExec('MouseGetPos, xPos, yPos, winId' \
'\n    PixelGetColor, color, %xPos%, %yPos%, RGB' \
'\n    WinGetTitle, winTitle, ahk_id %winId%' \
'\n    ToolTip "%winTitle%"`n%xPos% %yPos% %color%')
0
Vaeryn On

You may also use python wrapper for autohotkey called ahk (https://pypi.org/project/ahk/). Once you have autohotkey installed already (it needs to be installed 1st, wrapper import is a separate thing), below code should work (executable path is path to your AutoHotkey executable, if you have it on your windows path you may use ahk = AHK() or if you don't you may add it to your path)

from ahk import AHK
import time

ahk = AHK(executable_path=r'D:\Programy\Autohotkey\AutoHotkey.exe')

ahk.show_tooltip("hello4", x=10, y=10)
time.sleep(3)
ahk.hide_tooltip() # hide the tooltip
ahk.show_info_traytip("Info", "It's also info", silent=False, blocking=True)  # Default info traytip
ahk.show_warning_traytip("Warning", "It's a warning")                           # Warning traytip
ahk.show_error_traytip("Error", "It's an error")