@@ -18,6 +18,15 @@ use std::sync::{Arc, Mutex};
1818use std:: thread;
1919use std:: time:: Duration ;
2020
21+ #[ cfg( target_os = "windows" ) ]
22+ use std:: ffi:: OsStr ;
23+
24+ #[ cfg( target_os = "windows" ) ]
25+ use std:: os:: windows:: ffi:: OsStrExt ;
26+
27+ #[ cfg( target_os = "windows" ) ]
28+ use winapi:: um:: wincon:: SetConsoleTitleW ;
29+
2130const DEFAULT_PORT : u16 = 13000 ;
2231const PORT_RANGE_END : u16 = 14000 ;
2332const QQ_DOWNLOAD_URL : & str = "https://dldir1v6.qq.com/qqfile/qq/QQNT/c50d6326/QQ9.9.22.40768_x64.exe" ;
@@ -324,6 +333,9 @@ fn start_login_listener(
324333 thread:: spawn ( move || {
325334 let client = PMHQClient :: new ( port) . with_timeout ( Duration :: from_secs ( 10 ) ) ;
326335
336+ #[ cfg( target_os = "windows" ) ]
337+ start_windows_selfinfo_title_thread ( client. clone ( ) ) ;
338+
327339 thread:: sleep ( Duration :: from_secs ( 3 ) ) ;
328340
329341 let logged_in_refresh = logged_in. clone ( ) ;
@@ -381,6 +393,40 @@ fn start_login_listener(
381393 } ) ;
382394}
383395
396+ #[ cfg( target_os = "windows" ) ]
397+ fn start_windows_selfinfo_title_thread ( client : PMHQClient ) {
398+ thread:: spawn ( move || {
399+ // pmhq 在登录完成后仍可能需要一点时间才能返回完整 SelfInfo
400+ loop {
401+ match client. get_self_info ( ) {
402+ Ok ( info) => {
403+ if !info. uin . is_empty ( ) && !info. nickname . is_empty ( ) {
404+ let title = format ! ( "LLBot - {}({})" , info. nickname, info. uin) ;
405+ let _ = set_windows_console_title ( & title) ;
406+ break ;
407+ }
408+ }
409+ Err ( _) => {
410+ // 忽略未就绪/临时错误,继续重试
411+ }
412+ }
413+
414+ thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
415+ }
416+ } ) ;
417+ }
418+
419+ #[ cfg( target_os = "windows" ) ]
420+ fn set_windows_console_title ( title : & str ) -> Result < ( ) , String > {
421+ let wide: Vec < u16 > = OsStr :: new ( title) . encode_wide ( ) . chain ( std:: iter:: once ( 0 ) ) . collect ( ) ;
422+ let ok = unsafe { SetConsoleTitleW ( wide. as_ptr ( ) ) } ;
423+ if ok == 0 {
424+ Err ( "SetConsoleTitleW 调用失败" . to_string ( ) )
425+ } else {
426+ Ok ( ( ) )
427+ }
428+ }
429+
384430fn migrate_old_files ( exe_dir : & Path ) {
385431 // 迁移 data 目录
386432 let data_dir = exe_dir. join ( "data" ) ;
0 commit comments