簡體   English   中英

如何在 Lua 中發送 Windows 通知?

[英]How can I send a Windows Notification in Lua?

我正在嘗試通過 Lua 發送 Windows 通知,我找到了幾種方法,但是在 Python 中,我找不到 Lua 的任何信息。有什么方法可以做到這一點嗎? 如果是,有人可以告訴我怎么做嗎? 謝謝!

我嘗試尋找答案,但找不到任何相關信息。

Lua 沒有這樣的標准 function,你需要使用一些外部庫。
例如,您可以使用 FFI 庫直接調用 Win32API 函數。
以下腳本使用 LuaJIT FFI:

local ffi = require'ffi'
local shell32 = ffi.load'shell32'

ffi.cdef[[
typedef int BOOL;
typedef unsigned int DWORD;
typedef unsigned int UINT;
typedef intptr_t HANDLE;
typedef intptr_t HWND;
typedef intptr_t HICON;
typedef intptr_t HINSTANCE;
typedef struct {int Data[4];} GUID;

typedef struct {
  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;
  };
  char  szInfoTitle[64];
  DWORD dwInfoFlags;
  GUID  guidItem;
  HICON hBalloonIcon;
} NOTIFYICONDATAA;

BOOL Shell_NotifyIconA(
  int dwMessage,
  NOTIFYICONDATAA * lpData
);
HICON LoadIconA(
  HINSTANCE hInstance,
  intptr_t IconCode
);
BOOL DestroyIcon(
  HICON hIcon
);
HWND GetConsoleWindow();
]]



-- icon codes:
--    32512   Your application's icon
--    32513   Error       ("X" inside red circle)
--    32514   Question    ("?" inside blue circle)
--    32515   Warning     ("!" inside yellow triangle)
--    32516   Information ("i" inside blue circle)
--    32518   Security Shield

local tray_icon_code    = 32516   -- Information
local balloon_icon_code = 32518   -- Shield

-- create tray icon
local tray_icon_handle = ffi.C.LoadIconA(0, tray_icon_code)
local balloon_icon_handle = ffi.C.LoadIconA(0, balloon_icon_code)
local notify_icon_data = ffi.new"NOTIFYICONDATAA"
notify_icon_data.cbSize = ffi.sizeof(notify_icon_data)
notify_icon_data.hWnd = ffi.C.GetConsoleWindow()  -- HWND of your application window
notify_icon_data.uFlags = 1 + 2  -- NIF_MESSAGE | NIF_ICON
notify_icon_data.hIcon = tray_icon_handle
notify_icon_data.uVersion = 4
notify_icon_data.hBalloonIcon = balloon_icon_handle
shell32.Shell_NotifyIconA(0, notify_icon_data)  -- NIM_ADD
shell32.Shell_NotifyIconA(4, notify_icon_data)  -- NIM_SETVERSION
print("Tray icon added.  Press Enter to continue..."); io.read()

-- show notifications
local function copy_string(dest_array_ptr, str)
    ffi.copy(dest_array_ptr, (str or ""):sub(1, ffi.sizeof(dest_array_ptr) - 1))
end

local function show_notification(text, title)
    notify_icon_data.uFlags = 1 + 2 + 16   -- NIF_MESSAGE | NIF_ICON | NIF_INFO
    notify_icon_data.dwInfoFlags = 4 + 32  -- NIIF_USER | NIIF_LARGE_ICON
    copy_string(notify_icon_data.szInfoTitle, title)
    copy_string(notify_icon_data.szInfo, text)
    shell32.Shell_NotifyIconA(1, notify_icon_data) -- NIM_MODIFY
end

show_notification("some text", "some title")
print("Notification displayed.  Press Enter to continue..."); io.read()

show_notification("first line\nsecond line", "another title")
print("Another notification displayed.  Press Enter to continue..."); io.read()

-- notification requests generated by show_notification() are queued,
-- each notification is kept on the screen for several seconds before next one is displayed
-- to immediately remove the notification - remove the tray icon

-- remove tray icon
shell32.Shell_NotifyIconA(2, notify_icon_data)  -- NIM_DELETE
ffi.C.DestroyIcon(balloon_icon_handle)
ffi.C.DestroyIcon(tray_icon_handle)
print("Tray icon removed.  Press Enter to exit..."); io.read()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM