C# 如何设置“关注点”;无标题-记事本“;使用windows服务
我想把焦点放在记事本(无标题的记事本)上,并在上面写一些文字。我必须为此创建一个Windows服务 我可以创建windows服务,但不知道如何在记事本上设置焦点 请提供Windows服务中的代码示例 我尝试了以下代码。但是运气不好C# 如何设置“关注点”;无标题-记事本“;使用windows服务,c#,windows,service,C#,Windows,Service,我想把焦点放在记事本(无标题的记事本)上,并在上面写一些文字。我必须为此创建一个Windows服务 我可以创建windows服务,但不知道如何在记事本上设置焦点 请提供Windows服务中的代码示例 我尝试了以下代码。但是运气不好 namespace SampleService { public partial class Service1 : ServiceBase { string application = string.Empty; pub
namespace SampleService
{
public partial class Service1 : ServiceBase
{
string application = string.Empty;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
GetTaskWindows();
int iHandle = NativeWin32.FindWindow(null, application);
NativeWin32.SetForegroundWindow(iHandle);
}
protected override void OnStop()
{
GetTaskWindows();
int iHandle = NativeWin32.FindWindow(null, application);
NativeWin32.SetForegroundWindow(iHandle);
}
private void GetTaskWindows()
{
// Get the desktopwindow handle
int nDeshWndHandle = NativeWin32.GetDesktopWindow();
// Get the first child window
int nChildHandle = NativeWin32.GetWindow(nDeshWndHandle, NativeWin32.GW_CHILD);
while (nChildHandle != 0)
{
// Get only visible windows
if (NativeWin32.IsWindowVisible(nChildHandle) != 0)
{
StringBuilder sbTitle = new StringBuilder(1024);
// Read the Title bar text on the windows to put in combobox
NativeWin32.GetWindowText(nChildHandle, sbTitle, sbTitle.Capacity);
String sWinTitle = sbTitle.ToString();
{
if (sWinTitle.Length > 0)
{
if (sWinTitle.Contains("Notepad"))
{
application = sWinTitle;
}
}
}
}
// Look for the next child.
nChildHandle = NativeWin32.GetWindow(nChildHandle, NativeWin32.GW_HWNDNEXT);
}
}
}
}
我自己修好了。创建了一个小型应用程序,该应用程序将重点放在记事本上,并使用sendKeys功能将文本写入记事本。显示一些代码您尝试了哪些功能您尝试过用谷歌搜索吗?最新版本的windows不支持通过windows服务与桌面交互。你为什么要这样做,你想实现什么?在我的问题中添加了我正在使用的代码。