如何用C#获取外部程序(VC写的)窗口中某个按钮的句柄并执行点击操作?
请赐教,谢谢。>>大胃美女,我使用了FindWindow函数,但不知道怎样调用外部程序的某个控件,我现在实现的程度只是调用自己窗口中的某个按钮。代码如下:IntPtrhw...
请赐教,谢谢。
>>大胃美女,我使用了FindWindow函数,但不知道怎样调用外部程序的某个控件,我现在实现的程度只是调用自己窗口中的某个按钮。代码如下:
IntPtr hwnd_win;
IntPtr hwnd_button;
hwnd_win = FindWindow(null, "Call Extern App");
hwnd_button = FindWindowEx(hwnd_win, new IntPtr(0), null, "Test");
请问,如何使用FindWindow调用外部程序的窗口及其控件?谢谢。 展开
>>大胃美女,我使用了FindWindow函数,但不知道怎样调用外部程序的某个控件,我现在实现的程度只是调用自己窗口中的某个按钮。代码如下:
IntPtr hwnd_win;
IntPtr hwnd_button;
hwnd_win = FindWindow(null, "Call Extern App");
hwnd_button = FindWindowEx(hwnd_win, new IntPtr(0), null, "Test");
请问,如何使用FindWindow调用外部程序的窗口及其控件?谢谢。 展开
展开全部
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "..\\..\\..\\AUT3\\bin\\Debug\\AUT3.exe";
Process p = Process.Start(path);
if (p==null)
Console.WriteLine("Warning:process may already exist");
Console.WriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
Console.WriteLine("Handle to main window is " + mwh);
//有名字控件句柄
Console.WriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtr.Zero, null, "<enter color>");
if (tb == IntPtr.Zero)
throw new Exception("Unable to find textbox1");
else
Console.WriteLine("Handle to textbox1 is " + tb);
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "button1");
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
Console.WriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtr.Zero)
throw new Exception("Unable to find listbox1");
else
Console.WriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct<index&&result!=IntPtr.Zero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
FindWindow
FindWindowEx
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "..\\..\\..\\AUT3\\bin\\Debug\\AUT3.exe";
Process p = Process.Start(path);
if (p==null)
Console.WriteLine("Warning:process may already exist");
Console.WriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
Console.WriteLine("Handle to main window is " + mwh);
//有名字控件句柄
Console.WriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtr.Zero, null, "<enter color>");
if (tb == IntPtr.Zero)
throw new Exception("Unable to find textbox1");
else
Console.WriteLine("Handle to textbox1 is " + tb);
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "button1");
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
Console.WriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtr.Zero)
throw new Exception("Unable to find listbox1");
else
Console.WriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct<index&&result!=IntPtr.Zero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
FindWindow
FindWindowEx
展开全部
请用WINAPI函数吧!
FindWindow
FindWindowEx
FindWindow
FindWindowEx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "..\\..\\..\\AUT3\\bin\\Debug\\AUT3.exe";
Process p = Process.Start(path);
if (p==null)
Console.WriteLine("Warning:process may already exist");
Console.WriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
Console.WriteLine("Handle to main window is " + mwh);
//有名字控件句柄
Console.WriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtr.Zero, null, "<enter color>");
if (tb == IntPtr.Zero)
throw new Exception("Unable to find textbox1");
else
Console.WriteLine("Handle to textbox1 is " + tb);
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "button1");
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
Console.WriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtr.Zero)
throw new Exception("Unable to find listbox1");
else
Console.WriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct<index&&result!=IntPtr.Zero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
}
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "..\\..\\..\\AUT3\\bin\\Debug\\AUT3.exe";
Process p = Process.Start(path);
if (p==null)
Console.WriteLine("Warning:process may already exist");
Console.WriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
Console.WriteLine("Handle to main window is " + mwh);
//有名字控件句柄
Console.WriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtr.Zero, null, "<enter color>");
if (tb == IntPtr.Zero)
throw new Exception("Unable to find textbox1");
else
Console.WriteLine("Handle to textbox1 is " + tb);
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "button1");
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
Console.WriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtr.Zero)
throw new Exception("Unable to find listbox1");
else
Console.WriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct<index&&result!=IntPtr.Zero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
}
参考资料: 《.NET测试自动化之道》
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询