C
CMathewson
Guest
As the title says, when I click an icon on my desktop, I want a MsgBox to appear and say the name of it. For instance, If I click "Ccleaner", the msgbox should say "Ccleaner". Well, I already have the code for the mousebuttonclick, but I dont have the code for the icon title. Something like this:
ntcoder/bab/2008/08/15/capturing-text-from-under-your-mouse-cursor/ (add .com)
Is exactly what I want to do.
Imports System.Runtime.InteropServices
Public Class Form1
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
<DllImport("user32.dll")> _
Private Shared Function WindowFromPoint(ByVal xPoint As Integer, ByVal yPoint As Integer) As IntPtr
End Function
<DllImport("user32.dll", ExactSpelling:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
Private Function FindRoot(ByVal hWnd As IntPtr) As IntPtr
Do
Dim parent_hwnd As IntPtr = GetParent(hWnd)
If parent_hwnd.ToInt32 = 0 Then Return hWnd
hWnd = parent_hwnd
Loop
End Function
Private Function WindowText(ByVal hWnd As IntPtr) As String
If hWnd.ToInt32 = 0 Then Return ""
Dim text_len As Integer = GetWindowTextLength(hWnd)
If text_len = 0 Then Return ""
Dim sb As New System.Text.StringBuilder(text_len + 1)
Dim ret = GetWindowText(hWnd, sb, sb.Capacity)
If ret = 0 Then Return ""
Return sb.ToString
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Path As String
Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim txt As String = ""
Dim txt1 As String = ""
If MouseButtons.ToString = "Left" Then
Dim window_handle As IntPtr = _
CType(CInt(WindowFromPoint(MousePosition.X, MousePosition.Y).ToString()), IntPtr)
txt1 &= "Window handle: " & window_handle.ToString & vbCrLf
Dim root_handle As IntPtr = FindRoot(window_handle)
txt1 &= "Root handle: " & root_handle.ToString & vbCrLf
txt &= "Caption: " & vbCrLf & WindowText(root_handle) _
& vbCrLf
Label1.Text = txt
End If
End Sub
End Class
Ive posted this on another forum, but this is really urgent lol. Summer school projects are pain.
Continue reading...
ntcoder/bab/2008/08/15/capturing-text-from-under-your-mouse-cursor/ (add .com)
Is exactly what I want to do.
Imports System.Runtime.InteropServices
Public Class Form1
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
<DllImport("user32.dll")> _
Private Shared Function WindowFromPoint(ByVal xPoint As Integer, ByVal yPoint As Integer) As IntPtr
End Function
<DllImport("user32.dll", ExactSpelling:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
Private Function FindRoot(ByVal hWnd As IntPtr) As IntPtr
Do
Dim parent_hwnd As IntPtr = GetParent(hWnd)
If parent_hwnd.ToInt32 = 0 Then Return hWnd
hWnd = parent_hwnd
Loop
End Function
Private Function WindowText(ByVal hWnd As IntPtr) As String
If hWnd.ToInt32 = 0 Then Return ""
Dim text_len As Integer = GetWindowTextLength(hWnd)
If text_len = 0 Then Return ""
Dim sb As New System.Text.StringBuilder(text_len + 1)
Dim ret = GetWindowText(hWnd, sb, sb.Capacity)
If ret = 0 Then Return ""
Return sb.ToString
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Path As String
Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim txt As String = ""
Dim txt1 As String = ""
If MouseButtons.ToString = "Left" Then
Dim window_handle As IntPtr = _
CType(CInt(WindowFromPoint(MousePosition.X, MousePosition.Y).ToString()), IntPtr)
txt1 &= "Window handle: " & window_handle.ToString & vbCrLf
Dim root_handle As IntPtr = FindRoot(window_handle)
txt1 &= "Root handle: " & root_handle.ToString & vbCrLf
txt &= "Caption: " & vbCrLf & WindowText(root_handle) _
& vbCrLf
Label1.Text = txt
End If
End Sub
End Class
Ive posted this on another forum, but this is really urgent lol. Summer school projects are pain.
Continue reading...