Google
 
Web itpro-blogger.blogspot.com
木曜日, 3月 10, 2011

[VB.NET]APIのFindWindowをフレームワーク(.NET Framework)に変更するには?

▼質問
VB.NETでWin32APIのFindWindowを次の様にコーディングしているんだが・・・。



Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

Public Function GetWindowName(ByVal WindowName As String) As String

Try

If (FindWindow(vbNullString, WindowName) <> 0) Then
Return WindowName
End If

Catch ex As Exception

End Try

Return Nothing
End Function



これを.NET Frameworkに変更するには、どうしたらよいか教えてくれ!

▼回答
.NET FrameworkにあるProcessクラスを使えば実現できるよ!

参考にそのコードを次に示しておくよ!!


Public Function GetWindowName(ByVal WindowName As String, ByVal ProcessesName As String) As String

Try

Dim localByName As Process() = Process.GetProcessesByName(ProcessesName)

For i As Integer = 0 To localByName.Length - 1
If (localByName(i).MainWindowTitle.ToUpper = WindowName.ToUpper) Then
Return WindowName
End If
Next i

Catch ex As Exception

End Try

Return Nothing
End Function



This page is powered by Blogger. Isn't yours?