PDA

View Full Version : تغییر متن یک TextBox واقع در برنامه دیگر!



farid_nasiri
یک شنبه 11 فروردین 1387, 13:23 عصر
دو تا برنامه (فرم) مستقل رو در نظر بگیرید اولی رو خودمون مینویسیسم دومی کلا یک نرم افزار دیگه است.
چطور میشه از طریق برنامه خودمون که میخوایم بنویسیم متن داخل textbox فرم برنامه دوم رو عوض کرد.
تا اینجا میدونم که باید از api هایی مثل getwindowcaption و ... استفاده کرد اما کل فرایند رو نمیدونم.

Mr'Jamshidy
یک شنبه 11 فروردین 1387, 15:24 عصر
شما باید اول Handle کنترل مورد نظر رو بگیری


Option Explicit

Const WM_SETTEXT = &HC
Const GWL_STYLE = (-16)
Const GWL_EXSTYLE = (-20)
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const GWL_HWNDPARENT = (-8)
Const WM_COMMAND = &H111
Const MIN_ALL = 419
Const MIN_ALL_UNDO = 416
Const MAX_PATH = 260
Const SW_SHOW = 5
Const SW_RESTORE = 9
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWNORMAL = 1

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type

Private lpwndpl As WINDOWPLACEMENT
Private CursorLoc As POINTAPI

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As Long

Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Declare Function WindowFromPoint Lib "user32" _
(ByVal xPoint As Long, ByVal yPoint As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long

Private Declare Function GetWindowPlacement Lib "user32" _
(ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As Long

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Dim lLength As Long
Dim sText As String

Private Sub cmdMinAll_Click()
Dim lHnd As Long

lHnd = FindWindow("Shell_TrayWnd", vbNullString)
Call PostMessage(lHnd, WM_COMMAND, MIN_ALL, 0&)
End Sub

Private Sub cmdQuit_Click()
Unload Me
End Sub


Private Sub cmdShow_Click()
Dim sAppName As String
Dim lState As Long
Dim lHandle As Long

sAppName = Trim$(txtApp)
If sAppName = "" Then Exit Sub
'
' Find the window with the correct caption.
'
lpwndpl.Length = 44
lHandle = FindWindow(vbNullString, sAppName)
If lHandle = 0 Then Exit Sub
'
' Get the window's state and activate it.
'
lState = GetWindowPlacement(lHandle, lpwndpl)
Select Case lpwndpl.showCmd
Case SW_SHOWMINIMIZED
Call ShowWindow(lHandle, SW_RESTORE)
Case SW_SHOWNORMAL, SW_SHOWMAXIMIZED
Call ShowWindow(lHandle, SW_SHOW)
End Select
Call SetForegroundWindow(lHandle)
End Sub


Private Sub cmdUndo_Click()
Dim lHnd As Long

lHnd = FindWindow("Shell_TrayWnd", vbNullString)
Call PostMessage(lHnd, WM_COMMAND, MIN_ALL_UNDO, 0&)
End Sub

Private Sub Timer1_Timer()
Dim lHandle As Long
Dim lProcessId As Long
Dim lThreadId As Long
Dim lStrLen As Long
Dim lParentHWnd As Long
Dim sClass As String
Dim sCaption As String
Dim sText As String
Dim sParentText As String
Dim lpRect As RECT
'
' Get the cursor's coordinates.
'
Call GetCursorPos(CursorLoc)
lblMouseCoords = CursorLoc.x & ", " & CursorLoc.y
'
' Get the handle of the window at those coordinates.
'
lHandle = WindowFromPoint(CursorLoc.x, CursorLoc.y)
'
' Get the window's caption.
'
lStrLen = MAX_PATH
sCaption = Space$(MAX_PATH)
Call GetWindowText(lHandle, sCaption, lStrLen)
lStrLen = GetWindowText(lHandle, sCaption, lStrLen)
sCaption = Left$(sCaption, lStrLen)
'
' Get the window's process and thread ID.
'
lThreadId = GetWindowThreadProcessId(lHandle, lProcessId)
'
' Get the coordinates if the window.
'
Call GetWindowRect(lHandle, lpRect)
'
' Get the window's class.
'
sClass = Space$(MAX_PATH)
Call GetClassName(lHandle, sClass, 255)
sClass = Left$(sClass, InStr(sClass, vbNullChar) - 1)
'
' Get the handle of its parent window.
'
lParentHWnd = GetWindowLong(lHandle, GWL_HWNDPARENT)
'
' Get the window's text's length.
' Get the window's text.
'
sText = fWindowText(lHandle)
sParentText = fWindowText(lParentHWnd)

If sText = Empty Then
lLength = SendMessage(lHandle, WM_GETTEXTLENGTH, 0, ByVal 0&)
sText = Space$(lLength + 1)
sText = "Romina2006"
Call SendMessage(lHandle, WM_SETTEXT, lLength + 1, ByVal sText)
End If

lblInfo.Caption = _
"Caption = " & sCaption & vbCrLf _
& "Text = " & sText & vbCrLf _
& "Position = " & lpRect.Left & ", " & lpRect.Top & vbCrLf _
& "Size = " & (lpRect.Right - lpRect.Left) & ", " & (lpRect.Bottom - lpRect.Top) & vbCrLf _
& "Class = " & sClass & vbCrLf _
& "Style = " & Hex$(GetWindowLong(lHandle, GWL_STYLE)) & vbCrLf _
& "Extended style = " & Hex$(GetWindowLong(lHandle, GWL_EXSTYLE)) & vbCrLf _
& "Handle = " & lHandle & vbCrLf _
& "Parent window handle = " & lParentHWnd & vbCrLf _
& "Parent window text = " & sParentText & vbCrLf _
& "Process ID = " & CStr(lProcessId) & vbCrLf _
& "Thread ID = " & CStr(lThreadId)
End Sub


Public Function fWindowText(hWnd As Long) As String
Dim lLength As Long
Dim sText As String
'
' Return the text of a window.
'
'
' Get the text's length.
'
lLength = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, ByVal 0&)
sText = Space$(lLength + 1)
'
' Get the text.
'
Call SendMessage(hWnd, WM_GETTEXT, lLength + 1, ByVal sText)
fWindowText = Left$(sText, lLength)
End Function


باقیش هم سادست فقط یکم وقت بزاری مشکلت حل میشه