无法通过窗口服务最大化窗口 [英] Cannot Maximize window from a window service

查看:93
本文介绍了无法通过窗口服务最大化窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口服务,在启动时,我希望通过特定的应用程序来最大化屏幕.

所以在我的窗口服务中,我有以下内容

 私有声明函数ShowWindow Lib"user32" (ByVal句柄为IntPtr,ByVal nCmdShow为Integer)为Integer
   私有常量SW_SHOWMAXIMIZED为整数= 3
   私人共享的aTimer作为System.Timers.Timer

 受保护的重写Sub OnStart(ByVal args()作为字符串)
        '在此处添加代码以启动您的服务.这个方法应该设置东西
        ",以便您的服务可以正常工作.
       试试
            EventLog.WriteEntry("In OnStart")

            DoTheJob()
       异常捕获

       结束尝试


   结束

 私人子DoTheJob()
       试试


          
           昏暗的hwnd作为String = findPartialTitle("MyApplication")

            ShowWindow(hwnd,SW_SHOWMAXIMIZED)



            'EventLog.WriteEntry(hwnd)

       异常捕获
            EventLog.WriteEntry(例如消息)
       结束尝试



   结束子

    '功能:按部分窗口标题查找句柄
   私有函数findPartialTitle(ByVal部分标题作为字符串)作为IntPtr
       对于Process中的每个p作为Process.GetProcesses()
           如果p.MainWindowTitle.IndexOf(partialTitle,0,StringComparison.CurrentCultureIgnoreCase)> -1然后
               返回p.MainWindowHandle
           如果结束
       下一个
       返回IntPtr.Zero
   结束功能

如果我从Windows窗体运行,上面的代码将正确运行.

但是它不是从窗口服务内部运行的.处理程序为0.

我有一些疑问

是否可以从窗口服务内部最大化屏幕?

如果不可能这样做,我该如何创建一些东西在后台运行并侦听"?  如果我的应用正在运行,那么我可以将其最大化吗?

有人可以看到我的代码有问题吗? (该代码在Windows窗体应用中运行正常).

感谢您能给我的任何帮助.

谢谢

解决方案

开发Windows服务应用程序

以下链接中的注释可能与您尝试执行的操作有关.

Windows服务应用程序简介

..NET Framework支持的Windows服务类不支持与交互式工作站(即登录的用户)进行交互. .NET Framework还不包括代表工作站和桌面的类.如果您的Windows服务 必须与其他工作站进行交互,您将需要访问非托管Windows API.有关更多信息,请参阅Windows SDK文档.


Hi ,

I have a window service where on start I want from a specific app to maximize the screen.

So inside my window service I have the below

  Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Const SW_SHOWMAXIMIZED As Integer = 3
    Private Shared aTimer As System.Timers.Timer

  Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Try
            EventLog.WriteEntry("In OnStart")

            DoTheJob()
        Catch ex As Exception

        End Try


    End Sub

  Private Sub DoTheJob()
        Try


          
            Dim hwnd As String = findPartialTitle("MyApplication")

            ShowWindow(hwnd, SW_SHOWMAXIMIZED)



            'EventLog.WriteEntry(hwnd)

        Catch ex As Exception
            EventLog.WriteEntry(ex.Message)
        End Try



    End Sub

    'FUNCTION: Find handle by partial window title
    Private Function findPartialTitle(ByVal partialTitle As String) As IntPtr
        For Each p As Process In Process.GetProcesses()
            If p.MainWindowTitle.IndexOf(partialTitle, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                Return p.MainWindowHandle
            End If
        Next
        Return IntPtr.Zero
    End Function

The above code is running correctly if I run from a windows form.

But it doesn't run from inside a window service. The handler is 0.

I have some questions 

Is it possible to maximize a screen from inside a window service ?

If it is not possible to do that how can I create something to run in the background and "listens"  if my app is running so I can maximize it ?

Can anyone see something wrong in my code ? (The code is running OK from a windows form app).

Appreciate any help you could give me.

thank you

解决方案

Walkthrough: Creating a Windows Service Application in the Component Designer

Developing Windows Service Applications

Note in the following link may be an issue with what you are attempting to do.

Introduction to Windows Service Applications

"The Windows service classes supported by the .NET Framework do not support interaction with interactive stations, that is, the logged-on user. The .NET Framework also does not include classes that represent stations and desktops. If your Windows service must interact with other stations, you will need to access the unmanaged Windows API. For more information, see the Windows SDK documentation."


这篇关于无法通过窗口服务最大化窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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