/*整个Windows编程的基础。一个句柄是指使用的一个唯一的整数值,即一个4字节(64位程序中为8字节)长的数值,来标识应用程序中的不同对象和同类中的不同的实例,诸如,一个窗口,按钮,图标,滚动条,输出设备,控件或者文件等。应用程序能够通过句柄访问相应的对象的信息,但是句柄不是指针,程序不能利用句柄来直接阅读文件中的信息。如果句柄不在I/O文件中,它是毫无用处的。 句柄是Windows用来标志应用程序中建立的或是使用的唯一整数,Windows大量使用了句柄来标识对象。*/
[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, string lpszClass, 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 = ; //增加一个返回值用来判断操作是否成功
//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();
IntPtr EdithWnd = new IntPtr(); //查到窗体,得到整个窗体
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), lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr), "");
retval++;
}
}
}

[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++;
          }
        }
    }

C#获得窗口控件句柄的更多相关文章

  1. Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)

    用我的方法来控制其他程序窗体上的窗口控件,必须先了解什么是 回调函数.我的理解是这样的: 回 调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等 ...

  2. Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄

    有了回调函数的概念及上面的例子,我们可以继续了.其实想要找到一个标题已知的窗口句柄,用一个API函数就可以了:FindWindow.其函数原形是:function FindWindow(lpClass ...

  3. windows 编程 —— 子窗口 与 子窗口控件

    目录: 子窗口与主窗口的交互 子窗口控件 按钮类别 button 滚动条类别 scrollbar 静态类别  static 编辑框类别 edit 清单方块 listbox 子窗口与主窗口的交互 创建窗 ...

  4. Win32汇编学习(9):窗口控件

    这次我们将探讨控件,这些控件是我们程序主要的输入输出设备. 理论: WINDOWS 提供了几个预定义的窗口类以方便我们的使用.大多数时间内,我们把它们用在对话框中,所以我们一般就它们叫做子窗口控件.子 ...

  5. [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  6. 使用DataGridView数据窗口控件,构建用户快速输入体验

    在"随风飘散" 博客里面,介绍了一个不错的DataGridView数据窗口控件<DataGridView数据窗口控件开发方法及其源码提供下载>,这种控件在有些场合下,还 ...

  7. WPF中窗口控件的跨线程调用

    在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...

  8. 如何让窗口控件半透明(控件在Paint自己时,首先向主窗口询问,获取主窗口上控件所在区域的背景图)

    在网上关于窗口视觉效果,有2个问题被问得最多:第一个是如何让窗口边框有阴影效果?第二个是如何让窗口控件有半透明效果? 对于第一个问题,我们的答案是用双层窗口模拟或是用Layered Window.在X ...

  9. 双缓冲绘图和窗口控件的绘制——ATL ActiveX 窗口控件生成向导绘制代码OnDraw的一个错误 .

    双缓冲绘图和窗口控件的绘制 ---ATL ActiveX 窗口控件生成向导绘制代码OnDraw的一个错误 cheungmine 我们通常使用ATL COM组件,生成一个带窗口的ActiveX控件,然后 ...

随机推荐

  1. python cookbook 笔记二

    去重和排序: #coding=utf-8 def dedupe(items): seen = set() for item in items: if item not in seen: yield i ...

  2. SpringMVC使用Redis作为缓存提供者

    (1)pom添加依赖项 <dependency> <groupId>org.springframework.data</groupId> <artifactI ...

  3. jquery筛选数组之grep、each、inArray、map的用法及遍历son对象(转)

    grep [传入的参数为返回bool类型的函数] <script type='text/javascript' src="/jquery.js"></script ...

  4. python中对列表和循环使用的小练习

    #author devilf product_list = [ (), (), (), (), () ] shop_list = [] salary = input('pls enter your s ...

  5. discuz安装:mysqli_connect()不支持advice_mysqli_connect

    原文:http://blog.csdn.net/changzhi1990/article/details/40983247 php -m 输出: PHP Warning: PHP Startup: U ...

  6. Mac 安装 JDK

    1.访问Oracle官网 http://www.oracle.com,下载 JDK 2.安装JDK 解压 1 中下载的压缩包,在Finder下载目录中双击安装. 或者命令行安装,详见:http://w ...

  7. python3中的编解码

    #一个知识点是:python3中有两种字符串数据类型:str类型和 bytes类型:sty类型存储unicode数据,bytes类型存储bytes数据 #当我们在word上编辑文件的时候,数据保存之前 ...

  8. js常用函数整理

    类型转换:parseInt\parseFloat\toString 类型判断:typeof;eg:if(typeof(var)!="undefined")\isNaN 字符处理函数 ...

  9. Ngnix日志分析

    Ngnix日志分析 cat用来读取日志内容 grep进行匹配的文本搜索 wc则进行最终的统计 grep与命令格式: grep -E “a.*b” file,ab条件同时成立 grep或命令的格式为:g ...

  10. SPLAY,LCT学习笔记(四)

    前三篇好像变成了SPLAY专题... 这一篇正式开始LCT! 其实LCT就是基于SPLAY的伸展操作维护树(森林)连通性的一个数据结构 核心操作有很多,我们以一道题为例: 例:bzoj 2049 洞穴 ...