- This topic has 2 replies, 3 voices, and was last updated 15 years ago by
Departure.
-
AuthorPosts
-
January 14, 2008 at 6:29 am #190357
autopilot
MemberNow that we have each part working by itself, lets make one button (add a new button to form) to complete the task from start to finish. Typically, you would create subs or functions to do the individual tasks and call them in order from the button code.
NOTE: To make this run without user input, you will have to delete the Hello.txt file after every time you click the button.
So this is my final code (although buttons 1 – 5 could be completely removed as everything is done with one click of button6):
Imports System.Text
Public Class Form1
Declare Function SendMessageAsString Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Declare Function PostMessage Lib "USER32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Auto Function FindWindow Lib "USER32" Alias "FindWindow" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Declare Function EnumChildWindows Lib "USER32" (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumChildWindowsCallback, ByVal lParam As Integer) As Integer
Delegate Function EnumChildWindowsCallback(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal cch As Integer) As Integer
Const WM_SETTEXT As Integer = &HC
Const WM_COMMAND As Integer = &H111S
Const WM_KEYDOWN As Integer = &H100
Const VK_RETURN As Integer = &HD
Dim SFClassName As String
Dim SFClassIndex As Integer
Dim SFHnd As Integer
Dim SFClassHit As Integer
Public Function GetSubFormByClassNameWithMWClassWithMWCaption(ByVal MainWindowClass As String, ByVal MainWindowCaption As String, ByVal SubFormClass As String, ByVal SubFormClassIndex As Integer) As Integer
SFHnd = 0
SFClassHit = 0
SFClassIndex = SubFormClassIndex
SFClassName = SubFormClass
EnumChildWindows(FindWindow(MainWindowClass, MainWindowCaption), AddressOf EnumChildWindowByClassName, 0)
GetSubFormByClassNameWithMWClassWithMWCaption = SFHnd
End Function
Private Function EnumChildWindowByClassName(ByVal ChildhWnd As Integer, ByVal lParam As Integer) As Integer
Dim StringBufferLength As Integer = 255
Dim wClass As New StringBuilder(StringBufferLength)
GetClassName(ChildhWnd, wClass, StringBufferLength)
If wClass.ToString.ToLower = SFClassName.ToLower Then
SFClassHit += 1
If SFClassHit = SFClassIndex Then
SFHnd = ChildhWnd
EnumChildWindowByClassName = 0
Exit Function
End If
End If
EnumChildWindowByClassName = 1 ' Continue enumeration
End Function
Private Function OpenNP() As Boolean
Dim NPhWnd As Integer = 0
Try
'Open Notepad
Process.Start("C:Windowssystem32notepad.exe")
Do While NPhWnd = 0
'wait for notepad window to open
System.Threading.Thread.Sleep(100)
NPhWnd = FindWindow("Notepad", "Untitled - Notepad")
Loop
'Allow notepad window to finish opening
System.Threading.Thread.Sleep(500)
'If successful, true
Return True
Catch
'If failed, false
Return False
End Try
End Function
Private Function WriteNP(ByVal sText As String) As Boolean
Try
'Get handle for edit control in Notepad
Dim EditHndl As Integer = GetSubFormByClassNameWithMWClassWithMWCaption("Notepad", "Untitled - Notepad", "Edit", 1)
'Send text to notepad
SendMessageAsString(EditHndl, WM_SETTEXT, 0, sText)
'If successful, true
Return True
Catch
'If failed, false
Return False
End Try
End Function
Private Function SaveNP(ByVal FileName As String) As Boolean
Try
Dim notepad As Integer, SaveBtn As Integer, EditHnd As Integer = 0
notepad = FindWindow("Notepad", "Untitled - Notepad")
PostMessage(notepad, WM_COMMAND, 4, 0)
Do While EditHnd = 0
System.Threading.Thread.Sleep(100)
EditHnd = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Edit", 1)
Loop
System.Threading.Thread.Sleep(500)
SendMessageAsString(EditHnd, WM_SETTEXT, 0, FileName)
SaveBtn = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Button", 2)
PostMessage(SaveBtn, WM_KEYDOWN, VK_RETURN, 0)
'If successful, true
Return True
Catch
'If failed, false
Return False
End Try
End Function
Private Function CloseNP(ByVal Caption As String) As Boolean
Try
Dim notepad As Integer
notepad = FindWindow("Notepad", Caption)
PostMessage(notepad, WM_COMMAND, 7, 0)
'If successful, true
Return True
Catch
'If failed, false
Return False
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("C:Windowssystem32notepad.exe")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim notepad As Integer, editx As Integer
notepad = FindWindow("notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
MsgBox(editx.ToString)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim EditHndl As Integer = GetSubFormByClassNameWithMWClassWithMWCaption("Notepad", "Untitled - Notepad", "Edit", 1)
'MsgBox(EditHndl.ToString)
SendMessageAsString(EditHndl, WM_SETTEXT, 0, "Hello World")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim notepad As Integer, SaveBtn As Integer, EditHnd As Integer = 0
notepad = FindWindow("Notepad", "Untitled - Notepad")
PostMessage(notepad, WM_COMMAND, 4, 0)
Do While EditHnd = 0
System.Threading.Thread.Sleep(100)
EditHnd = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Edit", 1)
Loop
System.Threading.Thread.Sleep(500)
SendMessageAsString(EditHnd, WM_SETTEXT, 0, "Hello.txt")
SaveBtn = GetSubFormByClassNameWithMWClassWithMWCaption("#32770", "Save As", "Button", 2)
PostMessage(SaveBtn, WM_KEYDOWN, VK_RETURN, 0)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim notepad As Integer
notepad = FindWindow("Notepad", "Hello.txt - Notepad")
PostMessage(notepad, WM_COMMAND, 7, 0)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Not OpenNP() Then
MsgBox("Failed to open Notepad")
Exit Sub
End If
If Not WriteNP("Hello World") Then
MsgBox("Failed to write to Notepad")
Exit Sub
End If
If Not SaveNP("Hello.txt") Then
MsgBox("Failed to save Notepad")
Exit Sub
End If
'Pause to let file save complete
System.Threading.Thread.Sleep(1000)
If Not CloseNP("Hello.txt - Notepad") Then
MsgBox("Failed to close Notepad")
Exit Sub
End If
End Sub
End ClassJanuary 15, 2008 at 4:40 am #190359Departure
MemberGood tutorial Autopilot, This will give people the knowlage to make basic paltalk programs with vb.net, But the basic princiable can be apply to any programming langauge.
^5 great job.
P.s this has made me think about doing a tutorial in delphi, but the problem is that im about the only who uses delphi on this forum….
January 15, 2008 at 10:34 am #190358Ponies
Member@Departure wrote:
Good tutorial Autopilot, This will give people the knowlage to make basic paltalk programs with vb.net, But the basic princiable can be apply to any programming langauge.
^5 great job.
P.s this has made me think about doing a tutorial in delphi, but the problem is that im about the only who uses delphi on this forum….
Your mother does, mother fucker.
-
AuthorPosts
Related
- You must be logged in to reply to this topic.