[VB.NET] Tìm handle

tuhocvba

Administrator
Thành viên BQT
Phải công nhận là vb.net được hỗ trợ mạnh.
Thậm chí không cần phải khai báo hàm API gì mà đã tìm đựoc handle chính xác. So với VB6 thì nhàn hơn rất nhiều.
Cảm ơn @NhanSu
Mã:
        Dim p As Process
        Dim hdw As Long
        Dim s As String

        For Each p In Process.GetProcessesByName("notepad")
            hdw = p.MainWindowHandle
            If hdw <> 0 Then
                s = CStr(hdw)
                MsgBox(s)
                Exit For
            End If
        Next
 

tuhocvba

Administrator
Thành viên BQT
Ở bài viết này, tôi sẽ thực hiện sendkey giống xem sao.
Mã:
Public Class Form1


    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As IntPtr


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Opacity = 1
        Me.TopMost = True
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim p As Process
        Dim hdw As Long
        Dim s As String

        For Each p In Process.GetProcessesByName("notepad")
            hdw = p.MainWindowHandle
            If hdw <> 0 Then
                SetForegroundWindow(hdw)
                SendKeys.SendWait("OK")
                Exit For
            End If
        Next
    End Sub
End Class
Nguồn tham khảo:
 
Top