0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows 11 のタスクバー(Taskbar)にデスクトップ(Desktop)を表示するショートカットアイコン(Shortcut Icon)を表示する

Last updated at Posted at 2025-02-27

Windows11 Show Desktop Icon.png

Windows 10 までは「%AppData%\Microsoft\Internet Explorer\Quick Launch\デスクトップの表示(Show desktop)」を追加してできていたことが、Windows 11 ではできなくなった。
代わりに、タスクバーの隅をクリックするか、Win+D ショートカットで表示することができる。

が、デスクトップを表示する操作は個人的に頻繁に行う操作であり、キーボードショートカット以外でも、画面端にマウスを移動する事無く表示できるようにしたいのが本記事の目的。

以下の手順で実現する:

  1. ウィンドウを表示するアプリを作る(既存のやつで適当に代替するのでも可能)
  2. 作ったアプリを実行し、タスクバーにピン留めする
  3. 作ったアプリを、ウィンドウを表示するアプリから、デスクトップを表示するアプリに差し替える

ウィンドウを表示するアプリを作る

ここでは Rust で作るが、C++ でも Go でも Python でもなんでもよい。

Cargo.toml
[package]
name = "toggle_desktop"
version = "0.1.0"
edition = "2024"
build = "build.rs"

[build-dependencies]
winresource = "0"

[dependencies]
anyhow = "1"
scopeguard = "1"

[target.'cfg(windows)'.dependencies]
windows = { version = "0", features = [
    "Win32_Graphics_Gdi",
    "Win32_UI_WindowsAndMessaging",
] }
main.rs
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use anyhow::{bail, Context as _, Result};

#[cfg(target_os = "windows")]
use windows::{core::w, Win32::{Foundation::{HWND, LPARAM, LRESULT, WPARAM}, Graphics::Gdi::UpdateWindow, UI::WindowsAndMessaging::{CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetMessageW, PostQuitMessage, RegisterClassW, ShowWindow, TranslateMessage, MSG, SW_SHOWNORMAL, WINDOW_EX_STYLE, WM_CLOSE, WM_CREATE, WM_DESTROY, WNDCLASSW, WS_OVERLAPPEDWINDOW}}};

pub fn create_window() -> Result<HWND> {
    let class_name = w!("toggle_desktop");
    let winc = WNDCLASSW {
        lpfnWndProc: Some(wnd_proc),
        lpszClassName: class_name,
        ..Default::default()
    };

    if unsafe { RegisterClassW(&winc) } == 0 {
        bail!("RegisterClassW failed");
    }

    unsafe {
        Ok(CreateWindowExW(WINDOW_EX_STYLE(0), class_name, w!("デスクトップの表示"), WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, None, None, None, None)?)
    }
}

unsafe extern "system" fn wnd_proc(hwnd: HWND, msg: u32, w_param: WPARAM, l_param: LPARAM) -> LRESULT {
    match msg {
        WM_CREATE => {
        },
        WM_CLOSE => {
            let _ = DestroyWindow(hwnd);
        },
        WM_DESTROY => {
            PostQuitMessage(0);
        }
        _ => {
            return DefWindowProcW(hwnd, msg, w_param, l_param);
        }
    }

    LRESULT(0)
}

fn main() -> Result<()> {
    unsafe {
        let hwnd = create_window()?;

        let _ = ShowWindow(hwnd, SW_SHOWNORMAL);
        let _ = UpdateWindow(hwnd);

        let mut msg = MSG::default();

        while GetMessageW(&mut msg, None, 0, 0).as_bool() {
            let _ = TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }

    Ok(())
}
build.rs
fn main() {
    if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
        let mut res = winresource::WindowsResource::new();
        res.set_icon("icon.ico");
        res.compile().unwrap();
    }
}

作ったアプリを実行し、タスクバーにピン留めする

ビルドした exe ファイルを実行するとタスクバーに表示されるので、右クリックしてタスクバーにピン留めする。

作ったアプリを、ウィンドウを表示するアプリから、デスクトップを表示するアプリに差し替える

IShellDispatch4#ToggleDesktop でデスクトップの表示の切り替えができる。
好きな言語で作れば OK。

Cargo.toml
[package]
name = "toggle_desktop"
version = "0.1.0"
edition = "2024"
build = "build.rs"

[build-dependencies]
winresource = "0"

[dependencies]
anyhow = "1"
scopeguard = "1"

[target.'cfg(windows)'.dependencies]
windows = { version = "0", features = [
    "Win32_System_Com",
    "Win32_UI_Shell",
] }
main.rs
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use anyhow::{Context as _, Result};

use scopeguard::defer;

#[cfg(target_os = "windows")]
use windows::{core::GUID, Win32::{System::Com::{CoCreateInstance, CoInitialize, CoUninitialize, CLSCTX_SERVER}, UI::Shell::IShellDispatch4}};

// https://learn.microsoft.com/en-us/answers/questions/1075226/how-to-use-folder-api-with-rust
const CLSID_SHELL: GUID = GUID {
    data1: 0x13709620,
    data2: 0xC279,
    data3: 0x11CE,
    data4: [0xA4, 0x9E, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00],
};

fn main() -> Result<()> {
    unsafe {
        let _ = CoInitialize(None);
        defer! { 
            CoUninitialize();
        }

        let shell: IShellDispatch4 = CoCreateInstance(&CLSID_SHELL, None, CLSCTX_SERVER)?;
        shell.ToggleDesktop()?;
    }

    Ok(())
}

ビルドして、exe ファイルを差し替える。
これで検索ウィンドウの横にピン留めしたアイコンをクリックしてデスクトップを表示できるようになる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?