c# - Win 7 中的 "pin to desktop",兼容 XP

标签 c# windows-7 desktop

如何在 Win 7 中使用 FindWindow + SetParent 方法或任何其他在 XP 中也适用的方法实现“固定到桌面”效果(即不受“显示桌面”命令的影响)? 我知道我可以创建一个小工具,但我希望向后兼容 XP,我有这段代码可以很好地完成它:

IntPtr hWnd = FindWindow(null, "Untitled - Notepad");
IntPtr hDesktop = FindWindow("ProgMan", "Program Manager");
SetParent(hWnd, hDesktop);

最佳答案

在我的 WPF 应用程序中,我能够使用计时器解决它,它在 XP 和 Win 7 中都有效。

public MainWindow()
{
    InitializeComponent();

    // have some timer to fire in every 1 second
    DispatcherTimer detectShowDesktopTimer = new DispatcherTimer();
    detectShowDesktopTimer.Tick += new EventHandler(detectShowDesktopTimer_Tick);
    detectShowDesktopTimer.Interval = new TimeSpan(0, 0, 1);
    detectShowDesktopTimer.Start();
}

#region support immunizing against "Show Desktop"
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetWindowText(IntPtr handle)
{
    int chars = 256;
    StringBuilder buff = new StringBuilder(chars);
    if (GetWindowText(handle, buff, chars) > 0)
        return buff.ToString();
    else
        return string.Empty;
}
#endregion

private void detectShowDesktopTimer_Tick(object sender, EventArgs e)
{
    IntPtr fore = GetForegroundWindow();
    if (string.IsNullOrWhiteSpace(GetWindowText(fore)))
        ShowDesktopDetected();
}

private void ShowDesktopDetected()
{
    WindowInteropHelper wndHelper = new WindowInteropHelper(this);
    SetForegroundWindow(wndHelper.Handle);
}

关于c# - Win 7 中的 "pin to desktop",兼容 XP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350934/

相关文章:

ruby - 以编程方式在 Ruby 中获取桌面的屏幕截图?

linux - 如何在桌面上创建一个以其他用户身份自动执行的快捷方式?

c# - 从 SHA1 迁移到 SHA2 ASP.net 4.5,C#

c# - 作为可重用 XAML 片段的矢量图像

c# - 此版本的 sql server 不允许用户实例登录标志。 SQL Server 2012 企业版

mysql - django、phpmyadmin 和 mysql?

c# - Nullable<int> 到 Int 错误

python - 如何在 WIndows 7 上将 SQLITE 与 DJANGO 一起使用

windows-7 - win7 boost::asio::windows::stream_handle 构造函数抛出错误

java - 保护应用程序不被从一台 PC 复制到另一台 PC