如何在 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/