منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : تعديل كود
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
اولآ اريد تعديل هذه الكلاس لجعل كود الفروم يعمل بالاسم وليس بى PID
كود :
Imports System.Text
Imports System.Collections
Imports System.Runtime.InteropServices

''' <summary>
''' Object used to control a Windows Form.
''' </summary>
Public Class Wind
    ''' <summary>
    ''' Win32 API Imports
    ''' </summary>
    <DllImport("user32.dll")> _
    Private Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function IsIconic(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function IsZoomed(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByVal ProcessId As IntPtr) As IntPtr
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function AttachThreadInput(ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, ByVal fAttach As Integer) As IntPtr
    End Function

    ''' <summary>
    ''' Win32 API Constants for ShowWindowAsync()
    ''' </summary>
    Private Const SW_HIDE As Integer = 0
    Private Const SW_SHOWNORMAL As Integer = 1
    Private Const SW_SHOWMINIMIZED As Integer = 2
    Private Const SW_SHOWMAXIMIZED As Integer = 3
    Private Const SW_SHOWNOACTIVATE As Integer = 4
    Private Const SW_RESTORE As Integer = 9
    Private Const SW_SHOWDEFAULT As Integer = 10

    ''' <summary>
    ''' Private Fields
    ''' </summary>
    Private m_hWnd As IntPtr
    Private m_Title As String
    Private m_Visible As Boolean = True
    Private m_Process As String
    Private m_WasMax As Boolean = False

    ''' <summary>
    ''' Window Object's Public Properties
    ''' </summary>
    Public ReadOnly Property hWnd() As IntPtr
        Get
            Return m_hWnd
        End Get
    End Property
    Public ReadOnly Property Title() As String
        Get
            Return m_Title
        End Get
    End Property
    Public ReadOnly Property Process() As String
        Get
            Return m_Process
        End Get
    End Property

    ''' <summary>
    ''' Sets this Window Object's visibility
    ''' </summary>
    Public Property Visible() As Boolean
        Get
            Return m_Visible
        End Get
        Set(ByVal value As Boolean)
            'show the window
            If value = True Then
                If m_WasMax Then
                    If ShowWindowAsync(m_hWnd, SW_SHOWMAXIMIZED) Then
                        m_Visible = True
                    End If
                Else
                    If ShowWindowAsync(m_hWnd, SW_SHOWNORMAL) Then
                        m_Visible = True
                    End If
                End If
            End If
            'hide the window
            If value = False Then
                m_WasMax = IsZoomed(m_hWnd)
                If ShowWindowAsync(m_hWnd, SW_HIDE) Then
                    m_Visible = False
                End If
            End If
        End Set
    End Property

    ''' <summary>
    ''' Constructs a Window Object
    ''' </summary>
    ''' <param name="Title">Title Caption</param>
    ''' <param name="hWnd">Handle</param>
    ''' <param name="Process">Owning Process</param>
    Public Sub New(ByVal Title As String, ByVal hWnd As IntPtr, ByVal Process As String)
        m_Title = Title
        m_hWnd = hWnd
        m_Process = Process
    End Sub

    'Override ToString()
    Public Overrides Function ToString() As String
        'return the title if it has one, if not return the process name
        If m_Title.Length > 0 Then
            Return m_Title
        Else
            Return m_Process
        End If
    End Function

    ''' <summary>
    ''' Sets focus to this Window Object
    ''' </summary>
    Public Sub Activate()
        If m_hWnd = GetForegroundWindow() Then
            Return
        End If

        Dim ThreadID1 As IntPtr = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)
        Dim ThreadID2 As IntPtr = GetWindowThreadProcessId(m_hWnd, IntPtr.Zero)

        If ThreadID1 <> ThreadID2 Then
            AttachThreadInput(ThreadID1, ThreadID2, 1)
            SetForegroundWindow(m_hWnd)
            AttachThreadInput(ThreadID1, ThreadID2, 0)
        Else
            SetForegroundWindow(m_hWnd)
        End If

        If IsIconic(m_hWnd) Then
            ShowWindowAsync(m_hWnd, SW_RESTORE)
        Else
            ShowWindowAsync(m_hWnd, SW_SHOWNORMAL)
        End If
    End Sub
End Class

''' <summary>
''' Collection used to enumerate Window Objects
''' </summary>
Public Class Windo
    Implements IEnumerable
    Implements IEnumerator
    ''' <summary>
    ''' Win32 API Imports
    ''' </summary>
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowText(ByVal hWnd As Integer, ByVal title As StringBuilder, ByVal size As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowModuleFileName(ByVal hWnd As Integer, ByVal title As StringBuilder, ByVal size As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function EnumWindows(ByVal ewp As EnumWindowsProc, ByVal lParam As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function IsWindowVisible(ByVal hWnd As Integer) As Boolean
    End Function

    'delegate used for EnumWindows() callback function
    Public Delegate Function EnumWindowsProc(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean

    Private m_Position As Integer = -1
    'holds current index of wndArray, necessary for IEnumerable
    Private wndArray As New ArrayList()
    'array of windows
    'Object's private fields
    Private m_invisible As Boolean = False
    Private m_notitle As Boolean = False

    ''' <summary>
    ''' Collection Constructor with additional options
    ''' </summary>
    ''' <param name="Invisible">Include invisible Windows</param>
    ''' <param name="Untitled">Include untitled Windows</param>
    Public Sub New(ByVal Invisible As Boolean, ByVal Untitled As Boolean)
        m_invisible = Invisible
        m_notitle = Untitled

        'Declare a callback delegate for EnumWindows() API call
        Dim ewp As New EnumWindowsProc(AddressOf EvalWindow)
        'Enumerate all Windows
        EnumWindows(ewp, 0)
    End Sub
    ''' <summary>
    ''' Collection Constructor
    ''' </summary>
    Public Sub New()
        'Declare a callback delegate for EnumWindows() API call
        Dim ewp As New EnumWindowsProc(AddressOf EvalWindow)
        'Enumerate all Windows
        EnumWindows(ewp, 0)
    End Sub
    'EnumWindows CALLBACK function
    Private Function EvalWindow(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean
        If m_invisible = False AndAlso Not IsWindowVisible(hWnd) Then
            Return (True)
        End If

        Dim title As New StringBuilder(256)
        Dim [module] As New StringBuilder(256)

        GetWindowModuleFileName(hWnd, [module], 256)
        GetWindowText(hWnd, title, 256)

        If m_notitle = False AndAlso title.Length = 0 Then
            Return (True)
        End If

        wndArray.Add(New Wind(title.ToString(), CType(hWnd, IntPtr), [module].ToString()))

        Return (True)
    End Function

    'implement IEnumerable
    Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return DirectCast(Me, IEnumerator)
    End Function
    'implement IEnumerator
    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
        m_Position += 1
        If m_Position < wndArray.Count Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub Reset() Implements IEnumerator.Reset
        m_Position = -1
    End Sub
    Public ReadOnly Property Current() As Object Implements IEnumerator.Current
        Get
            Return wndArray(m_Position)
        End Get
    End Property
End Class
الفورم
كود :
Private myself As Wind
    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myself = New Wind("Window Hider", Me.Handle, "")
        ListBox1.Items.Clear()
        ListBox1.BeginUpdate()
        Dim wndList As New Windo()
        For Each wnd As Wind In wndList
            ListBox1.Items.Add(wnd)
        Next
        ListBox1.EndUpdate()
    End Sub

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
        VisListBox.Items.Add(Me.ListBox1.SelectedItem)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each wnd As Wind In VisListBox.Items
            wnd.Visible = False
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        UnhideAll()
    End Sub
    Private Sub UnhideAll()
        For Each wnd As Wind In VisListBox.Items
            wnd.Visible = True
        Next
    End Sub