lostfalling
2020-05-05 16:34
采纳率: 75%
浏览 378

c# FindWindowEx 这个API函数怎么用?还有第二个参数该怎么写,试了下获取电脑自带计算器的按钮句柄失败?求大神指点一下。

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace findwindow

{

public partial class Form1 : Form
{
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    public Form1()
    {
        InitializeComponent();
        this.ShowInTaskbar = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //如找到目标窗口显示对应句柄;找不到显示0
        IntPtr hwnd = FindWindow("CalcFrame", "计算器"); //
        if (hwnd != IntPtr.Zero)
        {
            txt.AppendText("发现目示窗口,其句柄是:" + hwnd + "\n");
        }
        else
        {
            txt.AppendText("没有找到目标窗口" + "\n");
        }

        IntPtr sonhwnd = FindWindowEx(hwnd,IntPtr.Zero,"Button", "9");//获取名为“9”的按钮句柄

        if (sonhwnd != IntPtr.Zero)
        {
            txt.AppendText("找到按钮");
        }
        else
        {
            txt.AppendText("句柄号为:" + sonhwnd.ToString() + " 没有找到目标句柄" + "\n");
        }
    }
}

}

图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题