< 返回版块

Borber 发表于 2022-08-05 21:02

Tags:windows

着实是不知道该怎么解决了, 从下午弄到现在... 背景总不是透明的. 本身没学过 windows 桌面应用开发, 边搜索边凑出来的,截图 , 拜托各位大佬能帮我看看怎么解决吗. 或者有什么别的思路可以给我个方向, 我想调整字号, 字体, 和大小.

或者有什么可以悬浮显示文字的应用有现成的吗? 可以鼠标穿透的

use std::ffi::OsStr;
use std::iter::once;
use std::os::windows::prelude::OsStrExt;
use windows::Win32::Graphics::Gdi::{GetDC, SetBkMode, SetTextColor, TextOutW, TRANSPARENT};
use windows::Win32::UI::WindowsAndMessaging::{GetDesktopWindow, SetWindowLongA};


unsafe fn print_message() {
    let desk = GetDesktopWindow();
    let dc = GetDC(desk);
    SetTextColor(dc, 0x0000FF);
    SetBkMode(dc, TRANSPARENT);
    loop {
        TextOutW(dc, 1000, 100, str_to_lpcwstr("中文").as_slice());
    }

}

fn str_to_lpcwstr(str : &str) -> Vec<u16> {
    OsStr::new(str).encode_wide().chain(once(0)).collect()
}

fn main() {
    println!("Start!");
    unsafe {
        print_message()
    }
}


[dependencies.windows]
version = "0.39.0"
features = [
    "Data_Xml_Dom",
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_Threading",
    "Win32_UI_WindowsAndMessaging",
    "Win32_Graphics_Gdi"
]

评论区

写评论
metaworm 2022-08-09 15:55

之前在reddit上看过一篇类似的回答 https://www.reddit.com/r/rust/comments/wj4x8v/comment/ijflfet/?utm_source=share&utm_medium=web2x&context=3

使用winit库可以以跨平台的方式设置窗口在最上层、透明

let window = WindowBuilder::new()
    .with_decorations(false)
    .with_transparent(true)
    // only works on windows and X11
    .with_always_on_top(true)
    .build(&event_loop)
    .unwrap();

window.set_cursor_hittest(false).unwrap();
// fullscreen on current monitor
window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
BBDXF 2022-08-06 21:17

参考这个链接:https://www.cnblogs.com/findumars/p/5625176.html 核心是SetLayeredWindowAttributes 函数

1 共 3 条评论, 1 页