這程式碼是用在要取視窗的標題文字用

深藍色字體可有可無


Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Text

Namespace VBDotNet_SendMessage_WM_GETTEXT_ObjektbeschreibungErmitteln_1

Public Class MainForm
Inherits System.Windows.Forms.Form
Private button1 As System.Windows.Forms.Button

Public Shared Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub

Public Sub New()
MyBase.New
Me.InitializeComponent
End Sub

#Region " Windows Forms Designer generated code "
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button
Me.SuspendLayout
'
'button1
'
Me.button1.Location = New System.Drawing.Point(8, 8)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(280, 40)
Me.button1.TabIndex = 0
Me.button1.Text = "&Test..."
AddHandler Me.button1.Click, AddressOf Me.Button1Click
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 53)
Me.Controls.Add(Me.button1)
Me.Name = "MainForm"
Me.Text = "Testform <SendMessage - WM_GETTEXT - Objektbeschreibung ermitteln>"
Me.ResumeLayout(false)
End Sub
#End Region

Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
Dim Result As String = GetText(button1.Handle)
If (Not Result Is String.Empty) Then
MessageBox.Show(GetText(Me.Handle), "Info")
End If
End Sub

End Class
End Namespace

Public Module Win32Api

Private Declare Function SendMessageByInt Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, _
ByVal uMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Int32

Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, _
ByVal uMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As StringBuilder) As Int32

Private Const WM_GETTEXT As Int32 = &HD
Private Const WM_GETTEXTLENGTH As Int32 = &HE

Public Function GetText(ByVal hwnd As IntPtr) As String
If (Not hwnd.Equals(IntPtr.Zero)) Then
Dim NumberOfCharacters As Int32 = SendMessageByInt(hwnd, WM_GETTEXTLENGTH, 0, 0)
If NumberOfCharacters > 0 Then
Dim ptText As New StringBuilder(NumberOfCharacters)
Dim Result As Int32 = SendMessageByString(hwnd, WM_GETTEXT, NumberOfCharacters + 1, ptText)
If Result <> 0 Then
Return ptText.ToString()
Else
Return String.Empty
End If
End If
Else
MessageBox.Show("Fehler in der Verarbeitung: IntPtr!", "Info")
End If
End Function

End Module

arrow
arrow

    dark99 發表在 痞客邦 留言(0) 人氣()