You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Lua语言中发送Windows系统通知?

在Lua中发送Windows系统通知的可行方法

方法1:调用PowerShell的BurntToast模块(推荐,样式美观)

这是实现现代Toast通知的简便方式,需先以管理员权限在PowerShell中安装BurntToast模块:

Install-Module -Name BurntToast

Lua代码实现:

local function send_windows_notification(title, message)
    -- 转义双引号避免PowerShell解析错误
    local safe_title = title:gsub('"', '\\"')
    local safe_msg = message:gsub('"', '\\"')
    local cmd = string.format([[powershell -Command "New-BurntToastNotification -Text '%s', '%s'"]], safe_title, safe_msg)
    os.execute(cmd)
end

-- 调用示例
send_windows_notification("Lua通知", "这是来自Lua的Windows系统通知!")

方法2:PowerShell原生Toast通知(无需额外模块)

若不想安装第三方模块,可直接调用Windows Runtime API生成Toast通知:

local function send_native_toast(title, message)
    -- 构造Toast通知的XML模板
    local toast_xml = string.format([[
    <toast>
        <visual>
            <binding template="ToastGeneric">
                <text>%s</text>
                <text>%s</text>
            </binding>
        </visual>
    </toast>
    ]], title, message)
    -- 转义XML特殊字符
    toast_xml = toast_xml:gsub('&', '&amp;'):gsub('<', '&lt;'):gsub('>', '&gt;'):gsub('"', '&quot;'):gsub("'", '&apos;')
    
    local cmd = string.format([[powershell -Command "[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::CreateToastNotifier('LuaNotification').Show([Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]::new([Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]::new().LoadXml('%s')))"]], toast_xml)
    os.execute(cmd)
end

-- 调用示例
send_native_toast("原生Lua通知", "无需额外模块的Windows Toast通知")

方法3:LuaJIT FFI调用Windows API(传统气泡通知)

如果使用LuaJIT,可直接通过FFI调用Shell_NotifyIcon函数发送传统右下角气泡通知,无需依赖外部工具:

local ffi = require("ffi")

-- 声明Windows API结构和函数
ffi.cdef[[
typedef struct _NOTIFYICONDATA {
    DWORD cbSize;
    HWND hWnd;
    UINT uID;
    UINT uFlags;
    UINT uCallbackMessage;
    HICON hIcon;
    char szTip[128];
    DWORD dwState;
    DWORD dwStateMask;
    char szInfo[256];
    union {
        UINT uTimeout;
        UINT uVersion;
    } DUMMYUNIONNAME;
    char szInfoTitle[64];
    DWORD dwInfoFlags;
    GUID guidItem;
    HICON hBalloonIcon;
} NOTIFYICONDATAW, *PNOTIFYICONDATAW;

BOOL Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW pnid);
HWND GetConsoleWindow(void);
void Sleep(DWORD dwMilliseconds);
]]

local shell32 = ffi.load("shell32")
local kernel32 = ffi.load("kernel32")

local function send_balloon_notification(title, message)
    local nid = ffi.new("NOTIFYICONDATAW")
    nid.cbSize = ffi.sizeof("NOTIFYICONDATAW")
    nid.hWnd = kernel32.GetConsoleWindow()
    nid.uID = 1
    nid.uFlags = 0x00000010 -- NIF_INFO,指定显示气泡内容
    ffi.copy(nid.szInfo, message)
    ffi.copy(nid.szInfoTitle, title)
    nid.dwInfoFlags = 0x00000000 -- NIIF_NONE,不显示图标

    -- 添加通知图标并显示气泡
    shell32.Shell_NotifyIconW(0x00000000, nid) -- NIM_ADD
    -- 保持通知显示5秒
    kernel32.Sleep(5000)
    -- 删除通知图标
    shell32.Shell_NotifyIconW(0x00000002, nid) -- NIM_DELETE
end

-- 调用示例
send_balloon_notification("LuaJIT气泡通知", "通过FFI调用Windows API实现的传统通知")

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:05:16