sp1231123123 2016-03-08 03:41 采纳率: 0%
浏览 2805

C#如何获取计算器按钮1的句柄,并进行点击操作,请教一下 谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
[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);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

    [STAThread]
    static void Main(string[] args)
    {
        string path = "C:\\WINDOWS\\system32\\calc.exe";
        Process p = Process.Start(path);
        if (p == null)
            Console.WriteLine("Warning:process may already exist");


        Console.WriteLine("Finding main window handle");
        IntPtr mainWindows = FindMainWindowHandle("计算器", 100, 25);
        Console.WriteLine("Handle to main window is " + mainWindows);

        //有名字控件句柄
        Console.WriteLine("Findding handle to button1");
        IntPtr butt = FindWindowEx(mainWindows, IntPtr.Zero, null, "Backspace");//这里的1是,计算器上名字为1的按钮
        if (butt == IntPtr.Zero)

            throw new Exception("Unable to find button1");

        else
            Console.WriteLine("Handle to button1 is " + butt);
        SendMessage(mainWindows, 0X101, butt, null);


        //没有名字或者重名控件
        //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;
    }
}

}

  • 写回答

2条回答

  • threenewbee 2016-03-08 03:45
    关注

    你用spy++看下控件id,直接用getdlgitem不需要findwindow

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝