
希望下面这个对您有所帮助:
利用vb *** 作一个外部程序的窗体上的按钮可以用到控制鼠标的API函数。
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
'而仅仅有这个函数是不够的,还需要定义一个type格式的自定义变量。定义为:
Private Type POINTAPI
X As Long
Y As Long
End Type
'它用于存放鼠标的位置(屏幕上的位置)。
'获得鼠标在屏幕上的位置要用到另一个函数:GetCursorPos,它的功能是获得屏幕上鼠标的坐标。
'它的声明如下:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
举个例子,放置1个Command控件,并且复制它,粘贴,点确定生成一控件数组。
Private Sub Command1_Click(Index As Integer)
Dim msX, msY As Long
Dim Pos As POINTAPI
a = GetCursorPos(Pos)
Select Case Index
Case 0 'up
msY = PosY - 15
msX = PosX
Case 1 'left
msX = PosX - 15
msY = PosY
Case 2 'down
msY = PosY + 15
msX = PosX
Case 3 'right
msX = PosX + 15
msY = PosY
Case Else
Exit Sub
End Select
a = SetCursorPos(msX, msY) '这个是利用API移动鼠标
End Sub
mouse_event
声明
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
说明
模拟一次鼠标事件
参数表
参数 类型及说明
dwFlags Long,下述标志的一个组合
MOUSEEVENTF_ABSOLUTE dx和dy指定鼠标坐标系统中的一个绝对位置。在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元
MOUSEEVENTF_MOVE 移动鼠标
MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下
MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起
MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下
MOUSEEVENTF_RIGHTUP 模拟鼠标右键按下
MOUSEEVENTF_MIDDLEDOWN 模拟鼠标中键按下
MOUSEEVENTF_MIDDLEUP 模拟鼠标中键按下
dx Long,根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定水平方向的绝对位置或相对运动
dy Long,根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定垂直方向的绝对位置或相对运动
cButtons Long,未使用
dwExtraInfo Long,通常未用的一个值。用GetMessageExtraInfo函数可取得这个值。可用的值取决于特定的驱动程序
注解
进行相对运动的时候,由SystemParametersInfo函数规定的系统鼠标轨迹速度会应用于鼠标运行的速度
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32dll", EntryPoint = "FindWindowEx", CharSet = CharSetAuto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "\\\\\\AUT3\\bin\\Debug\\AUT3exe";
Process p = ProcessStart(path);
if (p==null)
ConsoleWriteLine("Warning:process may already exist");
ConsoleWriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
ConsoleWriteLine("Handle to main window is " + mwh);
//有名字控件句柄
ConsoleWriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtrZero, null, "<enter color>");
if (tb == IntPtrZero)
throw new Exception("Unable to find textbox1");
else
ConsoleWriteLine("Handle to textbox1 is " + tb);
ConsoleWriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtrZero, null, "button1");
if (butt == IntPtrZero)
throw new Exception("Unable to find button1");
else
ConsoleWriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
ConsoleWriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtrZero)
throw new Exception("Unable to find listbox1");
else
ConsoleWriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtrZero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtrZero)
{
++ct;
}
} while (ct<index&&result!=IntPtrZero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtrZero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtrZero)
{
ConsoleWriteLine("Form not yet found");
ThreadSleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
ConsoleWriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtrZero)
throw new Exception("Could not find main window");
else
return mwh;
FindWindow
FindWindowEx
一般是用钩子程序的。
比较复杂,只能说个大体思路,要花最少30分钟。分太少了。
API函数的声明就不说了。
先用findwindow找到窗体,用GetWindow和GetClassName获得其窗体内各对象的句柄,从而可以获得文本框的句柄。
最后用SendMessage函数想文本框发送文本。
tWnd = FindWindow(vbNullString, "窗口名")可以获得外部程序窗口的句柄twnd
bwnd = GetWindow(tWnd, GW_CHILD)可以获得该窗口子对象的集合句柄bwnd
先把控件引用到你的程序用在去
调用控件的实体查看所提供的接口方法。
--------------------------------------------------
你可以去找找看该控件的说明文档,
一般第三方控件都会提供说明文档,去找找看他提供的接口
先用spy工具找到
任务管理器
的窗口类名
标题名,
然后根据类名使用win
api
的
findwindow
函数找到程序的窗口wnd,
再在窗口中找到Edit控件,获取内容
都是用winapi的东西,有些忘记了,网上可以找到的
’‘新建Command1,Text1,List1
''窗体代码
Private Sub Command1_Click()
Dim FormName As String
FormName = Text1
Dim t As Long
Dim Hwd As Long
Dim FormHwd As Long
List1Clear
FormHwd = F_FindForm(FormName)
If FormHwd = 0 Then Exit Sub
S_GetAllCon (FormHwd)
For t = 1 To CollConCount
Hwd = CollConItem(t)
'''列出所有控件的句柄,类型,及内容
List1AddItem t & "" & Hwd & "" & F_GetClass(Hwd) & "" & F_GetText(Hwd)
Next
end sub
'''模块代码Fbas
Option Explicit
Public Enum E_ActionType
E_ActionType_None
E_ActionType_填入文本框
E_ActionType_点击按钮
E_ActionType_获取焦点
E_ActionType_填入组合框
E_ActionType_查找窗体
E_ActionType_手动暂停
E_ActionType_获取文本框
End Enum
Public Enum E_CID
E_CID_名称 = 1
E_CID_父窗体名称 = 2
E_CID_步骤类型 = 3
E_CID_控件ID = 4
E_CID_参数 = 5
End Enum
Public NowWindow As String
Public NowAction As Long
Public Pause As Boolean
Public TempStr As String
Public Arr()
'步骤名称
'步骤父窗体名称
'步骤动作 1 填入 2点击 3焦点
'控件ID
'步骤参数
Sub AddAction(Name As String, _
Optional Father As String, _
Optional Action As E_ActionType, _
Optional ID As Long, _
Optional Para As String)
ReDim Preserve Arr(0 To 10, 0 To UBound(Arr, 2) + 1)
Arr(1, UBound(Arr, 2)) = Name
Arr(2, UBound(Arr, 2)) = Father
Arr(3, UBound(Arr, 2)) = Action
Arr(4, UBound(Arr, 2)) = ID
Arr(5, UBound(Arr, 2)) = Para
End Sub
Function ExecuteAction() As Long
Dim Action As E_ActionType
Action = Arr(3, NowAction)
Dim FormName As String
Dim FormHwd As Long
Dim Hwd As Long
Dim ID As Long
Dim Para As String
If Action = E_ActionType_查找窗体 Then
FormName = Arr(5, NowAction)
If FF_FindForm(FormName) = 0 Then
MsgBox "未找到窗体:" & Arr(5, NowAction)
ExecuteAction = 0
Exit Function
End If
ExecuteAction = 1
End If
If Action = E_ActionType_填入文本框 Then
FormName = Arr(2, NowAction)
FormHwd = FF_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If
ID = Arr(4, NowAction)
Hwd = FF_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If
Para = Arr(5, NowAction)
Call SendMessage(Hwd, WM_SETTEXT, 0, ByVal Para)
' If FF_GetText(Hwd) <> Para Then
' MsgBox "验证失败"
' ExecuteAction = 0
' Exit Function
' End If
' End If
ExecuteAction = 1
End If
If Action = E_ActionType_获取文本框 Then
FormName = Arr(2, NowAction)
FormHwd = FF_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If
ID = Arr(4, NowAction)
Hwd = FF_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If
TempStr = FF_GetText(Hwd)
ExecuteAction = 1
End If
If Action = E_ActionType_点击按钮 Then
FormName = Arr(2, NowAction)
FormHwd = FF_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If
ID = Arr(4, NowAction)
Hwd = FF_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If
'FF_Click (Hwd)
Call SendMessage(Hwd, BM_CLICK, 0, 0)
ExecuteAction = 1
End If
If Action = E_ActionType_手动暂停 Then
ExecuteAction = 2
End If
If Action = E_ActionType_获取焦点 Then
ExecuteAction = 2
End If
End Function
Function F_FindForm(Name As String, Optional Class As String = vbNullString, Optional Father As Long = 0, Optional Start As Long = 0) As Long
F_FindForm = FindWindowEx(Father, Start, Class, Name)
End Function
Function F_FindByID(Optional Father As Long = 0, Optional ID As Long = 1, Optional Class As String = vbNullString) As Long
Dim t As Long, p As Long
Dim Class1 As String 255
Dim Class2 As String
F_FindByID = 0
For t = CollConCount To 1 Step -1
CollConRemove t
Next
EnumChildWindows Father, AddressOf EnumChildWindowsProc, ByVal 0&
If Class = vbNullString Then '任意控件
If ID > CollConCount Then
F_FindByID = 0
Exit Function
End If
F_FindByID = CollConItem(ID)
Else '制定控件
p = 0
For t = 1 To CollConCount
Call GetClassNameA(CollConItem(t), Class1, 255)
Class2 = Replace(Class1, Chr(0), "")
If Class = Class2 Then p = p + 1
If p = ID Then
F_FindByID = CollConItem(t)
Exit Function
End If
Next
End If
End Function
Sub S_GetAllCon(Optional Father As Long = 0)
Dim t As Long, p As Long
For t = CollConCount To 1 Step -1
CollConRemove t
Next
EnumChildWindows Father, AddressOf EnumChildWindowsProc, ByVal 0&
End Sub
Function F_GetClass(Hwd As Long) As String
Dim Class1 As String 255
Call GetClassNameA(Hwd, Class1, 255)
F_GetClass = Replace(Class1, Chr(0), "")
End Function
Function F_GetText(Hwd As Long) As String
'Dim Text1 As String 2550
'Call GetWindowText(Hwd, Text1, 2550)
'F_GetText = Replace(Text1, Chr(0), "")
Dim Text1 As String 2550
Call SendMessage(Hwd, WM_GETTEXT, 2550, ByVal Text1)
F_GetText = Replace(Trim(Text1), Chr(0), "")
End Function
Function F_SetText(Hwd As Long, Str As String) As Long
'F_SetText = SetWindowText(Hwd, Str)
F_SetText = SendMessage(Hwd, WM_SETTEXT, 0, ByVal Str)
End Function
Function F_Click(Hwd As Long) As Long
'F_Click = SendMessage(Hwd, WM_LBUTTONDOWN, 0, 0)
'F_Click = SendMessage(Hwd, WM_LBUTTONUP, 0, 0)
'F_Click = SendMessage(Hwd, BM_CLICK, 0, 0)
'F_Click = SendMessage(Hwd, BM_CLICK, 0, 0)
'Call SendMessage(Hwd, BM_CLICK, 0, 0)
End Function
以上就是关于vb获得外部程序的combobox内容全部的内容,包括:vb获得外部程序的combobox内容、如何用C#获取外部程序(VC写的)窗口中某个按钮的句柄并执行点击 *** 作、如何获得其他程序有焦点的控件的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)