⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Visual.Basic.NET实用编程百例-47.6M.zip
💻 VB
字号:
Imports VB = Microsoft.VisualBasic
Friend Class Form1
    Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
    Public Sub New()
        MyBase.New()
        '此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
    End Sub
    '窗体重写处置,以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
        If Disposing Then
            If Not components Is Nothing Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(Disposing)
    End Sub
    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer
    Public ToolTip1 As System.Windows.Forms.ToolTip
    Public WithEvents tmrWinClass As System.Windows.Forms.Timer
    Public WithEvents ListBox1 As System.Windows.Forms.ListBox
    Public WithEvents lblCount As System.Windows.Forms.Label
    Public WithEvents lblClass As System.Windows.Forms.Label
    Public WithEvents lblCurrent As System.Windows.Forms.Label
    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器来修改它。
    '不要使用代码编辑器修改它。
    Public WithEvents TextBox1 As System.Windows.Forms.TextBox
    Public WithEvents Label2 As System.Windows.Forms.Label
    Public WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.tmrWinClass = New System.Windows.Forms.Timer(Me.components)
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.lblCount = New System.Windows.Forms.Label
        Me.lblClass = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.lblCurrent = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'tmrWinClass
        '
        Me.tmrWinClass.Enabled = True
        '
        'ListBox1
        '
        Me.ListBox1.BackColor = System.Drawing.SystemColors.Window
        Me.ListBox1.Cursor = System.Windows.Forms.Cursors.Default
        Me.ListBox1.ForeColor = System.Drawing.SystemColors.WindowText
        Me.ListBox1.ItemHeight = 12
        Me.ListBox1.Location = New System.Drawing.Point(8, 47)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.ListBox1.Size = New System.Drawing.Size(341, 100)
        Me.ListBox1.Sorted = True
        Me.ListBox1.TabIndex = 2
        '
        'TextBox1
        '
        Me.TextBox1.AcceptsReturn = True
        Me.TextBox1.AutoSize = False
        Me.TextBox1.BackColor = System.Drawing.SystemColors.Window
        Me.TextBox1.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.TextBox1.ForeColor = System.Drawing.SystemColors.WindowText
        Me.TextBox1.Location = New System.Drawing.Point(8, 17)
        Me.TextBox1.MaxLength = 0
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.TextBox1.Size = New System.Drawing.Size(341, 21)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'lblCount
        '
        Me.lblCount.BackColor = System.Drawing.SystemColors.Menu
        Me.lblCount.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblCount.ForeColor = System.Drawing.Color.Black
        Me.lblCount.Location = New System.Drawing.Point(16, 152)
        Me.lblCount.Name = "lblCount"
        Me.lblCount.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblCount.Size = New System.Drawing.Size(264, 18)
        Me.lblCount.TabIndex = 9
        '
        'lblClass
        '
        Me.lblClass.BackColor = System.Drawing.SystemColors.Control
        Me.lblClass.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblClass.ForeColor = System.Drawing.Color.Black
        Me.lblClass.Location = New System.Drawing.Point(136, 200)
        Me.lblClass.Name = "lblClass"
        Me.lblClass.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblClass.Size = New System.Drawing.Size(299, 18)
        Me.lblClass.TabIndex = 7
        '
        'Label2
        '
        Me.Label2.BackColor = System.Drawing.SystemColors.Control
        Me.Label2.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label2.ForeColor = System.Drawing.Color.Black
        Me.Label2.Location = New System.Drawing.Point(10, 200)
        Me.Label2.Name = "Label2"
        Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label2.Size = New System.Drawing.Size(126, 35)
        Me.Label2.TabIndex = 6
        Me.Label2.Text = "目前活动窗口的类别:"
        '
        'lblCurrent
        '
        Me.lblCurrent.BackColor = System.Drawing.SystemColors.Control
        Me.lblCurrent.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblCurrent.ForeColor = System.Drawing.Color.Black
        Me.lblCurrent.Location = New System.Drawing.Point(136, 176)
        Me.lblCurrent.Name = "lblCurrent"
        Me.lblCurrent.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblCurrent.Size = New System.Drawing.Size(184, 26)
        Me.lblCurrent.TabIndex = 4
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.SystemColors.Control
        Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label1.ForeColor = System.Drawing.Color.Black
        Me.Label1.Location = New System.Drawing.Point(10, 176)
        Me.Label1.Name = "Label1"
        Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label1.Size = New System.Drawing.Size(126, 16)
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "目前活动窗口的标题:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(368, 229)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.lblCount)
        Me.Controls.Add(Me.lblClass)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.lblCurrent)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.Location = New System.Drawing.Point(298, 181)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "枚举系统正在运行的程序"
        Me.ResumeLayout(False)

    End Sub
#End Region

    '  获得运行的程序
    Private Sub cmdGetClass()
        Dim lngHand As Integer
        Dim strName As New VB6.FixedLengthString(255)
        Dim wndClass As wndClass
        Dim lngProcID As Integer
        Dim rctTemp As RECT

        '  定位选中的窗口并获得其句柄
        lngHand = FindWindow(vbNullString, TextBox1.Text)

        '  获得窗口的信息
        GetClassName(lngHand, strName.Value, Len(strName.Value))
        '  获得进程ID与窗口尺寸信息
        If VB.Left(strName.Value, 1) = vbNullChar Then

        Else

            GetWindowThreadProcessId(lngHand, lngProcID)
            GetWindowRect(lngHand, rctTemp)
        End If

    End Sub
    '  初始化
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        SetWindowPos(Me.Handle.ToInt32, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
        TextBox1.SelectionLength = Len(TextBox1.Text)
        lblCount.Text = "系统正在运行程序的总数是:" & GetOpenWindowNames()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.Text
        cmdGetClass()
    End Sub

    '  定时属性标签中的信息
    Private Sub tmrWinClass_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrWinClass.Tick
        Dim lngHand As Integer
        Dim strName As New VB6.FixedLengthString(255)

        lngHand = GetForegroundWindow
        GetWindowText(lngHand, strName.Value, Len(strName.Value))
        lblCurrent.Text = strName.Value
        GetClassName(lngHand, strName.Value, Len(strName.Value))
        lblClass.Text = strName.Value
    End Sub
    Public Function GetOpenWindowNames() As Integer
        Dim lngDeskTopHandle As Integer
        Dim lngHand As Integer
        Dim strName As New VB6.FixedLengthString(255)
        Dim lngWindowCount As Integer

        '  获得桌面窗口的句柄
        lngDeskTopHandle = GetDesktopWindow()

        '  获得桌面窗口的子窗口句柄
        lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
        lngWindowCount = 1

        '  循环列举
        Do While lngHand <> 0

            '  取得窗口的标题
            GetWindowText(lngHand, strName.Value, Len(strName.Value))
            lngHand = GetWindow(lngHand, GW_HWNDNEXT)
            If VB.Left(strName.Value, 1) <> vbNullChar Then
                Me.ListBox1.Items.Add(VB.Left(strName.Value, InStr(1, strName.Value, vbNullChar)))
                lngWindowCount = lngWindowCount + 1
            End If
        Loop

        GetOpenWindowNames = lngWindowCount
    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -