按标题VB.Net关闭窗口 [英] VB.Net Close window by title

查看:272
本文介绍了按标题VB.Net关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个方法,通过标题来关闭特定的窗口。

I'm searching a method to close a specific window by the title.

我试着用 Process.GetProcessesByName ;但不被这种情况下,格外的工作。

I tried with Process.GetProcessesByName; but not is working by this case particulary.

我在寻找与API或类似的方法(而不是在C#中,我看到几个code,但在vb.net中不能正常工作)

I'm searching a method with APIs or similar (Not in C#, I see several code but not work fine in vb.net)

谢谢!

更新

感谢您的答复。但我仍然有你形容我下面的解决方案的一个问题。
我有一个控制两个窗口只有一个进程。然后,如果我关闭(或杀死)窗口#2,立即关闭第一个(见图片)。

Thanks for the reply. But I'm still have a problem with the solution that you describe me below. I'm have an only process that's control two windows. Then, if I close (or kill) the Window #2, instantly close the first one (See the image).

通过这个原因,我觉得在使用API​​方法从乞讨。

By this reason I think in using an API method from the begging.

我只希望关闭第二个窗口。

I'm only want close the second window.

推荐答案

尝试使用这样的事情。使用 Process.MainWindowTitle 获得标题文本和 Process.CloseMainWindow 关闭该UI,它多了几分婉约不是杀死进程。

Try using something like this. using Process.MainWindowTitle to get the Title Text and Process.CloseMainWindow to close down the UI, its a little more graceful than killing the Process.

请注意:包含做了区分大小写的搜索

Note: Contains does a case-sensitive search

Imports System.Diagnostics
Module Module1

    Sub Main()
        Dim myProcesses() As Process = Process.GetProcesses

        For Each p As Process In myProcesses
            If p.MainWindowTitle.Contains("Notepad") Then
                p.CloseMainWindow()
            End If
        Next

    End Sub

End Module


至于赢的API函数尝试这样的事情。请注意,如果您关闭的父窗口关闭孩子也。

Module Module1
    Private Declare Auto Function FindWindowEx Lib "user32" (ByVal parentHandle As Integer, _
                                               ByVal childAfter As Integer, _
                                               ByVal lclassName As String, _
                                               ByVal windowTitle As String) As Integer

    Private Declare Auto Function PostMessage Lib "user32" (ByVal hwnd As Integer, _
                                                            ByVal message As UInteger, _
                                                            ByVal wParam As Integer, _
                                                            ByVal lParam As Integer) As Boolean

    Dim WM_QUIT As UInteger = &H12
    Dim WM_CLOSE As UInteger = &H10


    Sub Main()
        Dim handle As Integer = FindWindowEx(0, 0, Nothing, "YourFormsTitle")
        PostMessage(handle, WM_CLOSE, 0, 0)
    End Sub

End Module

这篇关于按标题VB.Net关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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