|
I am coding in VB.NET to control an external application.
I used the following code to launch a Form2 from a theoretical Form1 using the shortcut ALT-F1 on Form 1.
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Get handle by window title. You can specify the class name too. Dim hwnd As Int32 = apiFindWindowEx(HWND_DESKTOP, 0, Nothing, "Form1") SetForegroundWindow(hwnd) SendKeys.SendWait("%{F1}")
End Sub
Now, there is a TextBox on Form2. How do I find and place the cursor on that TextBox so I can send some text to that textbox.
I know the classname of the TextBox. How can I accomplish this?
- Edited bytrekkie25 Sunday, June 22, 2008 12:09 AMClarification
-
| | trekkie25 Sunday, June 22, 2008 12:08 AM | Hi Trekkie25, First use FindWindow to find the windows' handle. Then use FindWindowEx to find its child control's handle. | ImportsSystem.Runtime.InteropServices | | ImportsMicrosoft.Win32 | | PublicClassForm1 | | PrivateConstWM_LBUTTONDOWN=&H201 | | PrivateConstWM_LBUTTONDBLCLK=&H203 | | PrivateConstWM_LBUTTONUP=&H202 | | <DllImport("user32.dll",CharSet:=CharSet.Auto,SetLastError:=False)>_ | | PrivateSharedFunctionSendMessage(ByValhWndAsIntPtr,ByValMsgAsUInteger,ByValwParamAsIntPtr,_ | | ByVallParamAsIntPtr)AsIntPtr | | EndFunction | | <System.Runtime.InteropServices.DllImport("USER32.DLL",CharSet:=System.Runtime.InteropServices.CharSet.Auto)>_ | | PrivateSharedFunctionFindWindow(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsIntPtr | | EndFunction | | <DllImport("user32.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_ | | PrivateSharedFunctionFindWindowEx(ByValparentHandleAsIntPtr,ByValchildAfterAsIntPtr,_ | | ByVallclassNameAsString,ByValwindowTitleAsString)AsIntPtr | | EndFunction | | PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click | | DimFHandleAsIntPtr | | FHandle=FindWindow(Nothing,"Form1") | | DimchildhandleAsIntPtr=FindWindowEx(FHandle,IntPtr.Zero,Nothing,"Button1") | | IfFHandle=IntPtr.ZeroThen | | MessageBox.Show("notepadisnotrunning.") | | Else | | MessageBox.Show(childhandle.ToString) | | SendMessage(childhandle,WM_LBUTTONDBLCLK,IntPtr.Zero,IntPtr.Zero) | | SendMessage(childhandle,WM_LBUTTONUP,IntPtr.Zero,IntPtr.Zero) | | EndIf | | EndSub | | PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load | | Me.Text="FormX" | | EndSub | | EndClass | |
Best regards, Riquel
Please remember to mark the replies as answers if they help and unmark them if they provide no help. - Marked As Answer byRiquel_DongModeratorThursday, June 26, 2008 5:23 AM
-
| | Riquel_Dong Wednesday, June 25, 2008 2:08 PM | Hi Trekkie25, First use FindWindow to find the windows' handle. Then use FindWindowEx to find its child control's handle. | ImportsSystem.Runtime.InteropServices | | ImportsMicrosoft.Win32 | | PublicClassForm1 | | PrivateConstWM_LBUTTONDOWN=&H201 | | PrivateConstWM_LBUTTONDBLCLK=&H203 | | PrivateConstWM_LBUTTONUP=&H202 | | <DllImport("user32.dll",CharSet:=CharSet.Auto,SetLastError:=False)>_ | | PrivateSharedFunctionSendMessage(ByValhWndAsIntPtr,ByValMsgAsUInteger,ByValwParamAsIntPtr,_ | | ByVallParamAsIntPtr)AsIntPtr | | EndFunction | | <System.Runtime.InteropServices.DllImport("USER32.DLL",CharSet:=System.Runtime.InteropServices.CharSet.Auto)>_ | | PrivateSharedFunctionFindWindow(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsIntPtr | | EndFunction | | <DllImport("user32.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_ | | PrivateSharedFunctionFindWindowEx(ByValparentHandleAsIntPtr,ByValchildAfterAsIntPtr,_ | | ByVallclassNameAsString,ByValwindowTitleAsString)AsIntPtr | | EndFunction | | PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click | | DimFHandleAsIntPtr | | FHandle=FindWindow(Nothing,"Form1") | | DimchildhandleAsIntPtr=FindWindowEx(FHandle,IntPtr.Zero,Nothing,"Button1") | | IfFHandle=IntPtr.ZeroThen | | MessageBox.Show("notepadisnotrunning.") | | Else | | MessageBox.Show(childhandle.ToString) | | SendMessage(childhandle,WM_LBUTTONDBLCLK,IntPtr.Zero,IntPtr.Zero) | | SendMessage(childhandle,WM_LBUTTONUP,IntPtr.Zero,IntPtr.Zero) | | EndIf | | EndSub | | PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load | | Me.Text="FormX" | | EndSub | | EndClass | |
Best regards, Riquel
Please remember to mark the replies as answers if they help and unmark them if they provide no help. - Marked As Answer byRiquel_DongModeratorThursday, June 26, 2008 5:23 AM
-
| | Riquel_Dong Wednesday, June 25, 2008 2:08 PM |
|