如何在窗口服务应用程序中找到ActiveWindow [英] How to find activewindow in window service application

查看:64
本文介绍了如何在窗口服务应用程序中找到ActiveWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,
我正在创建一个窗口服务应用程序,在其中我必须找出哪个是当前活动窗口(具有键盘焦点).我已经这样做了,但这总是返回空值.



I am creating a window service application in which I have to findout out which is current active window(which have keyboard focus).I have done like this but this is returning null values always.


[DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

const int nChars = 256;
            IntPtr handle = IntPtr.Zero;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();
            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                Log.WriteLine(Buff.ToString());
                return Buff.ToString();
            }
            Log.WriteLine("Returning  Null");
            return null;



但是我在控制台应用程序中尝试了Same逻辑,但是在那方面工作得很好.还有其他方法可以在窗口服务中找出活动窗口吗?
请帮帮我.

问候,
Manushi



But I have tried Same logic in console application but its working fine in that.Is there any other way to findout out active window in window service?
Kindly help me out.

Regards,
Manushi

推荐答案

从Vista开始,服务无法与UI进行交互.因此,像GetForegroundWindow 这样的调用将不起作用.这是设计使然.

解决方法是使存根客户端应用程序调用GetForegroundWindow 并与服务进行通信.对于任何UI功能,服务都会通过存根客户端进行代理.
From Vista onwards, services cannot interact with the UI. So calls like GetForegroundWindow will not work. This is by design.

The workaround is to have a stub client application that calls GetForegroundWindow and communicates with the service. For any UI function, the service proxies through the stub client.


这篇关于如何在窗口服务应用程序中找到ActiveWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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