lostfalling 2020-05-05 16:34 采纳率: 50%
浏览 532
已采纳

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条回答 默认 最新

  • 大耳朵爱被被 2020-05-05 17:53
    关注

    建议你用spy++找一下这个9的class和caption,第三个第四个参数就是这两个,另外第二个参数是子窗口句柄,而且必须是parent的直接子窗口,如果你不知道可以写NULL。
    最后可以多看看MSDN:https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexa

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建