Как сделать иконку в трее для Windows с помощью ящика winapi? - PullRequest
0 голосов
/ 05 января 2019

Я пытаюсь использовать ящик для винтажа Rust, чтобы сделать простой значок в трее. Мне раньше это удавалось в Си, но я не могу осчастливить Руста. Позже я добавлю код C, чтобы показать, какие биты части NOTIFYICONDATA я хочу использовать.

Супер базовые цели:

  • Заставь сказать слова

  • Сделать такой значок по умолчанию, как этот

    default icon

    Это самое простое; Другие встроенные значки я могу выяснить позже.

  • Обновить слова

  • Удалите его после завершения программы

Ссылка на библиотеку winapi в Rust (с функцией поиска!)

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

  • Windows 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();

}

Cargo.toml нуждается в этом:

[target.'cfg(windows)'.dependencies]
winapi = { version = "*", features = ["wincon","shellapi","ntdef"] }

А вот функциональность кода C, которую я пытаюсь имитировать (не уверен, какие библиотеки нужны, поэтому я бросил большинство из них):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define _WIN32_WINNT 0x0500 //must be before windows.h for mystical reasons such as widnows.h overwrites it with not right thing
#include <windows.h>
#include <shellapi.h> // make some system tray stuff go on
#define WM_MYMESSAGE (WM_USER + 1) //for that tray icon

int main()
{
    HWND hWnd = GetConsoleWindow(); // from https://stackoverflow.com/questions/11812095/hide-the-console-window-of-a-c-program via Anthropos

    NOTIFYICONDATA nid; //thing that has info on window and system tray stuff in it
        nid.cbSize = sizeof(NOTIFYICONDATA); //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 = LoadIcon(NULL, IDI_APPLICATION); //icon idk
        strcpy(nid.szTip, "Tool tip words here"); //tooltip for the icon
        nid.szTip[19] = '\0'; //null at the end of it
        nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; //who knows
        size_t nidszTipLength = sizeof(nid.szTip) / sizeof(nid.szTip[0]); //gets the size of nid.szTip (tooltip length)

    Shell_NotifyIcon(NIM_ADD, &nid); //shows the icon

    system("pause");

    strcpy(nid.szTip, "An updated tooltip is now here!"); //tooltip for the icon
    Shell_NotifyIcon(NIM_MODIFY, &nid); //updates system tray icon
    nid.szTip[31] = '\0'; //null at the end of it

    system("pause");

    Shell_NotifyIcon(NIM_DELETE, &nid); //deletes system tray icon when done

    system("pause");

    return 0;
}

1 Ответ

0 голосов
/ 09 января 2019

Я вычеркнул сам и направился к источнику винапи в Русте https://github.com/retep998/winapi-rs/issues/725 и получил достаточно помощи, чтобы успешно решить эту проблему. Код теперь синтаксически действителен как чудесный бонус!

Требовалась пара улучшений, в основном:

  • Обработка строки в UTF-16 формате для ОС для чтения

  • Запишите, что UTF-16 в 128-длинный uint16 векторный массив

  • Создать nid вне unsafe{ }, чтобы его можно было использовать в другом месте

  • Переключитесь на W-серию вызовов Winapi вместо A-серии (не уверен в отличии, кроме разыскиваемых странных A-серий, таких как int8 вместо uint16 в 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();

}

И не забудьте включить следующее в Cargo.toml!

[target.'cfg(windows)'.dependencies]
winapi = { version = "*", features = ["winuser","wincon","shellapi"] }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...