C# 获取其他窗体句柄
我再form1(父窗体)里想获得form2(子窗体)的句柄(form2已经打开),要怎么获取啊,IntPtrmaindHwnd=FindWindow(null,"Form...
我再form1(父窗体)里想获得form2(子窗体)的句柄(form2已经打开),要怎么获取啊,
IntPtr maindHwnd = FindWindow(null, "Form2");
IntPtr maindHwnd = My_lParam.FindWindowEx(this.Handle, (IntPtr)0, null, "Form2");
上面两种方法输出都是0,求帮助! 谢谢 展开
IntPtr maindHwnd = FindWindow(null, "Form2");
IntPtr maindHwnd = My_lParam.FindWindowEx(this.Handle, (IntPtr)0, null, "Form2");
上面两种方法输出都是0,求帮助! 谢谢 展开
展开全部
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, stringlpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//string lpszParentClass = "#32770"; //整个窗口的类名
string lpszParentWindow = "Form1"; //窗口标题
string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框
//string lpszClass = "Edit";
string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名
//string lpszClass_Submit = "Button";
string lpszName_Submit = "确定"; //需要查找的Button的标题
string text = "";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(null, lpszParentWindow);
//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到Form1这个子窗体的文本框,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]这里获取到的EdithWnd始终为0;[/color]
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "test1";
//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, text);
retval++;
}
//得到Button这个子窗体,并触发它的Click事件
EdithWnd = FindWindowEx(ParenthWnd,
(IntPtr)0, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}
}
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, stringlpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//string lpszParentClass = "#32770"; //整个窗口的类名
string lpszParentWindow = "Form1"; //窗口标题
string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框
//string lpszClass = "Edit";
string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名
//string lpszClass_Submit = "Button";
string lpszName_Submit = "确定"; //需要查找的Button的标题
string text = "";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(null, lpszParentWindow);
//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到Form1这个子窗体的文本框,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]这里获取到的EdithWnd始终为0;[/color]
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "test1";
//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, text);
retval++;
}
//得到Button这个子窗体,并触发它的Click事件
EdithWnd = FindWindowEx(ParenthWnd,
(IntPtr)0, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}
}
展开全部
[DllImport("user32.dll")]public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); StringBuilder s = new StringBuilder(512); int i = GetWindowText(this.Handle, s, s.Capacity); //把this.handle换成你需要的句柄 MessageBox.Show(s.ToString());
满意请采纳。
满意请采纳。
追问
你这是知道句柄获取标题吧 我要的是知道标题获得句柄。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
[DllImport("user32.dll")]
public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
string lpszClass, string lpszWindow);
请参考:http://www.cnblogs.com/alex_itxz/archive/2010/09/15/1827156.html
public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
string lpszClass, string lpszWindow);
请参考:http://www.cnblogs.com/alex_itxz/archive/2010/09/15/1827156.html
追问
我上面写的 IntPtr maindHwnd = My_lParam.FindWindowEx(this.Handle, (IntPtr)0, null, "Form2"); 为什么mainHwnd是0
追答
你的参数有问题吧
hwndParent:父窗体的句柄
hwndChildAfter:填0即可(IntPtr.Zero)
lpszClass:窗口类名
lpszWindow:窗口标题
还有:FindWindowEx 最好是放到当前类中 使用时不要带有类名My_lParam
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询