use*_*531 5 winapi trayicon rust
我正在尝试使用 Rust 的 winapi crate 来制作一个简单的托盘图标。我之前在 C 中设法做到了,但我不能让 Rust 高兴。稍后我将包含 C 代码以显示NOTIFYICONDATA
我想要使用的部件的哪些部分。
超基本目标:
让它说的话
使它成为这样的默认图标
这是最简单的;稍后我可以找出其他内置图标。
更新单词
程序完成后删除它
链接到 Rust 的 winapi 库(带有搜索功能!)
https://docs.rs/winapi/*/x86_64-pc-windows-msvc/winapi/um/wincon/fn.GetConsoleWindow.html
我真的根本不知道 Windows API,所以对我来说都是希腊语,我只是匹配我在其他示例中找到的语法等。所以请不要跳过任何内容,因为我可能不知道隐含的是什么那里(例如使用 std:: 或其他东西)!
Rust 版本 1.3.1
winapi 板条箱版本 0.3.6
视窗 10
这是我到目前为止管理的 Rust 代码(但不起作用!):
//-----Import Libraries (called crates)-----
extern crate winapi;
//-----Import Built-in Libraries (not called crates)-----
use std::process::Command; //use cmd.exe
use std::mem::size_of; //get size of stuff
fn main()
{
// to navigate calling with the winapi "crate" use the search function at link
// https://docs.rs/winapi/*/x86_64-pc-windows-msvc/winapi/um/wincon/fn.GetConsoleWindow.html
let hWnd = unsafe { winapi::um::wincon::GetConsoleWindow }; //gets the current console window handle
//System Tray Icon support - here it is
let WM_MYMESSAGE = winapi::um::winuser::WM_APP + 100; //prep WM_MYMESSAGE
let mut trayToolTip = "Tool tip words here"; //record tooltip words for the icon
let nid = winapi::um::shellapi::NOTIFYICONDATAA //thing that has info on window and system tray stuff in it
{
cbSize: size_of::<winapi::um::shellapi::NOTIFYICONDATAA>() as u32, //prep
hWnd: hWnd(), //links the console window
uID: 1001, //it's a number
uCallbackMessage: WM_MYMESSAGE, //whoknows should be related to click capture but doesn't so
//Couldn't find anything for WM_MYMESSAGE at all
hIcon: winapi::um::winuser::LoadIconA(winapi::shared::ntdef::NULL, winapi::um::winuser::IDI_APPLICATION), //icon idk
szTip: trayToolTip, //tooltip for the icon
uFlags: winapi::um::shellapi::NIF_MESSAGE | winapi::um::shellapi::NIF_ICON | winapi::um::shellapi::NIF_TIP, //who knows
};
let nidszTipLength: u64 = szTip.chars().count(); //gets the size of nid.szTip (tooltip length)
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_ADD, &nid); //shows the icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
nid.szTip: "An updated tooltip is now here!"; //tooltip for the icon
//abs total guess hoping some Python . stuff that I see sometimes in Rust works here and maybe it gets a : instead of a = too
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_MODIFY, &nid); //updates system tray icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_DELETE, &nid); //deletes system tray icon when done
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml 需要这个:
[target.'cfg(windows)'.dependencies]
winapi = { version = "*", features = ["wincon","shellapi","ntdef"] }
Run Code Online (Sandbox Code Playgroud)
这是我试图模仿的 C 代码功能(不确定需要哪些库,所以我把大部分都扔了):
//-----Import Libraries (called crates)-----
extern crate winapi;
//-----Import Built-in Libraries (not called crates)-----
use std::process::Command; //use cmd.exe
use std::mem::size_of; //get size of stuff
fn main()
{
// to navigate calling with the winapi "crate" use the search function at link
// https://docs.rs/winapi/*/x86_64-pc-windows-msvc/winapi/um/wincon/fn.GetConsoleWindow.html
let hWnd = unsafe { winapi::um::wincon::GetConsoleWindow }; //gets the current console window handle
//System Tray Icon support - here it is
let WM_MYMESSAGE = winapi::um::winuser::WM_APP + 100; //prep WM_MYMESSAGE
let mut trayToolTip = "Tool tip words here"; //record tooltip words for the icon
let nid = winapi::um::shellapi::NOTIFYICONDATAA //thing that has info on window and system tray stuff in it
{
cbSize: size_of::<winapi::um::shellapi::NOTIFYICONDATAA>() as u32, //prep
hWnd: hWnd(), //links the console window
uID: 1001, //it's a number
uCallbackMessage: WM_MYMESSAGE, //whoknows should be related to click capture but doesn't so
//Couldn't find anything for WM_MYMESSAGE at all
hIcon: winapi::um::winuser::LoadIconA(winapi::shared::ntdef::NULL, winapi::um::winuser::IDI_APPLICATION), //icon idk
szTip: trayToolTip, //tooltip for the icon
uFlags: winapi::um::shellapi::NIF_MESSAGE | winapi::um::shellapi::NIF_ICON | winapi::um::shellapi::NIF_TIP, //who knows
};
let nidszTipLength: u64 = szTip.chars().count(); //gets the size of nid.szTip (tooltip length)
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_ADD, &nid); //shows the icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
nid.szTip: "An updated tooltip is now here!"; //tooltip for the icon
//abs total guess hoping some Python . stuff that I see sometimes in Rust works here and maybe it gets a : instead of a = too
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_MODIFY, &nid); //updates system tray icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
winapi::um::shellapi::Shell_NotifyIconA(winapi::um::shellapi::NIM_DELETE, &nid); //deletes system tray icon when done
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
}
Run Code Online (Sandbox Code Playgroud)
我自己动手,前往 Rust 中的 winapi 源代码https://github.com/retep998/winapi-rs/issues/725并获得了足够的帮助来成功解决这个问题。该代码现在在语法上是有效的,这是一个奇妙的好处!
需要进行一些升级,主要是:
将字符串转换UTF-16
为操作系统可以读取的格式
将其UTF-16
写入 128 长的uint16
向量数组
nid
在外部创建unsafe{ }
,以便可以在其他地方使用
切换到 winapi 调用的 W 系列而不是 A 系列(除了 A 系列想要奇怪的东西之外,不确定有什么区别,比如而int8
不是uint16
in LoadIcon[letter]
)
工作代码如下:
//-----Import Libraries (called crates)-----
extern crate winapi;
//-----Import Built-in Libraries (not called crates)-----
use std::process::Command; //use cmd.exe
use std::mem::{size_of, zeroed}; //get size of stuff and init with zeros
use std::ptr::null_mut; //use a null pointer (I think)
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
fn main()
{
// to navigate calling with the winapi "crate" use the search function at link
// https://docs.rs/winapi/*/x86_64-pc-windows-msvc/winapi/um/wincon/fn.GetConsoleWindow.html
let hWnd = unsafe { winapi::um::wincon::GetConsoleWindow }; //gets the current console window handle
//System Tray Icon support - here it is
let WM_MYMESSAGE = winapi::um::winuser::WM_APP + 100; //prep WM_MYMESSAGE
let mut trayToolTip = "Tool tip words here".to_string(); //record tooltip words for the icon
let mut trayToolTipInt: [u16; 128] = [0; 128]; //fill with 0's
let trayToolTipStrStep: &str = &*trayToolTip; //these two types of strings
let mut trayToolTipStepOS = OsStr::new(trayToolTipStrStep); //convert to OS string format or something
let mut trayToolTipStepUTF16 = trayToolTipStepOS.encode_wide().collect::<Vec<u16>>(); //now actually convert to UTF16 format for the OS
trayToolTipInt[..trayToolTipStepUTF16.len()].copy_from_slice(&trayToolTipStepUTF16); //record it in that nice integer holder
let mut nid: winapi::um::shellapi::NOTIFYICONDATAW = unsafe{ zeroed() }; //thing that has info on window and system tray stuff in it
unsafe
{
nid.cbSize = size_of::<winapi::um::shellapi::NOTIFYICONDATAW>() as u32; //prep
nid.hWnd = hWnd(); //links the console window
nid.uID = 1001; //it's a number
nid.uCallbackMessage = WM_MYMESSAGE; //whoknows should be related to click capture but doesn't so
nid.hIcon = winapi::um::winuser::LoadIconW(null_mut(), winapi::um::winuser::IDI_APPLICATION); //icon idk
nid.szTip = trayToolTipInt; //tooltip for the icon
nid.uFlags = winapi::um::shellapi::NIF_MESSAGE | winapi::um::shellapi::NIF_ICON | winapi::um::shellapi::NIF_TIP; //who knows
};
//let mut nidszTipLength = trayToolTip.chars().count() as u64; //gets the size of nid.szTip (tooltip length) indirectly (not the right size!)
let mut nidszTipLength = trayToolTipStepUTF16.len() as u64; //gets the size of nid.szTip (tooltip length) for the UTF-16 format, which is what Windows cares about
unsafe{ winapi::um::shellapi::Shell_NotifyIconW(winapi::um::shellapi::NIM_ADD, &mut nid) }; //shows the icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
trayToolTip = "An updated tooltip is now here!".to_string(); //update the tooltip string
trayToolTipInt = [0; 128]; //fill with 0's (clear it out I hope)
let trayToolTipStrStep: &str = &*trayToolTip; //these two types of strings are hella annoying
trayToolTipStepOS = OsStr::new(trayToolTipStrStep); //convert to OS string format or something
trayToolTipStepUTF16 = trayToolTipStepOS.encode_wide().collect::<Vec<u16>>(); //now actually convert to UTF16 format for the OS
trayToolTipInt[..trayToolTipStepUTF16.len()].copy_from_slice(&trayToolTipStepUTF16); //record it in that nice integer holder
nid.szTip = trayToolTipInt; //tooltip for the icon
//nidszTipLength = trayToolTip.chars().count() as u64; //gets the size of nid.szTip (tooltip length) indirectly (not the right size!)
nidszTipLength = trayToolTipStepUTF16.len() as u64; //gets the size of nid.szTip (tooltip length) for the UTF-16 format, which is what Windows cares about
unsafe{ winapi::um::shellapi::Shell_NotifyIconW(winapi::um::shellapi::NIM_MODIFY, &mut nid) }; //updates system tray icon
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
unsafe{ winapi::um::shellapi::Shell_NotifyIconW(winapi::um::shellapi::NIM_DELETE, &mut nid) }; //deletes system tray icon when done
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记将以下内容包含在您的Cargo.toml
!
[target.'cfg(windows)'.dependencies]
winapi = { version = "*", features = ["winuser","wincon","shellapi"] }
Run Code Online (Sandbox Code Playgroud)