"针到桌面"在Win 7,XP兼容 [英] "pin to desktop" in Win 7, XP compatible

查看:164
本文介绍了"针到桌面"在Win 7,XP兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在Win 7实现销到桌面的效果(对显示桌面命令即免疫),使用FindWindow函数+的setparent的方式,或任何其他的会在XP中工作,以及方法?
我知道我可以创造一个小工具,但我想有XP,在那里我有这样的代码做精细的向后兼容性:

 的IntPtr的hWnd = FindWindow函数(NULL,无标题 - 记事本); 
IntPtr的hDesktop = FindWindow函数(PROGMAN,项目经理);
的setparent(HWND,hDesktop);


解决方案
在我的WPF应用程序

,我能解决这个问题使用定时器,它的工作无论是在XP和Win 7。

 公共主窗口()
{
的InitializeComponent();

//有一些计时器火在每1秒
DispatcherTimer detectShowDesktopTimer =新DispatcherTimer();
detectShowDesktopTimer.Tick + =新的EventHandler(detectShowDesktopTimer_Tick);
detectShowDesktopTimer.Interval =新时间跨度(0,0,1);
detectShowDesktopTimer.Start();
}

#地区的支持,对免疫显示桌面
函数[DllImport(user32.dll中)]
私人静态外部的IntPtr GetForegroundWindow();
函数[DllImport(user32.dll中)]
公共静态的extern BOOL SetForegroundWindow(IntPtr的的hWnd);
函数[DllImport(user32.dll中)]
私人静态外部INT GetWindowText函数(IntPtr的的HWND,StringBuilder的文字,诠释计数);

私人字符串GetWindowText函数(IntPtr的手柄)
{
INT字符= 256;
StringBuilder的BUFF =新的StringBuilder(字符);
如果(GetWindowText函数(句柄,浅黄色,字符)0)
返回buff.ToString();
,否则
返回的String.Empty;
}
#endregion

私人无效detectShowDesktopTimer_Tick(对象发件人,EventArgs五)
{
IntPtr的脱颖而出= GetForegroundWindow();
如果(string.IsNullOrWhiteSpace(GetWindowText函数(前)))
ShowDesktopDetected();
}

私人无效ShowDesktopDetected()
{
WindowInteropHelper wndHelper =新WindowInteropHelper(本);
SetForegroundWindow(wndHelper.Handle);
}


How could I achieve "pin to desktop" effect (i.e immune against "Show Desktop" command) in Win 7, using FindWindow + SetParent approach, or any other approach that would work in XP as well ? I know I could create a gadget, but I'd like to have backwards compatibility with XP, where I have this code doing it fine:

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

解决方案

in my WPF app, I was able to solve it using a timer, it's working both in XP and 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);
}

这篇关于"针到桌面"在Win 7,XP兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆