你到这里下载一个swinapi,里面对API的介绍很全的,还有示例
http://www.csdn.net/cnshare/soft/5/5145.shtm示例是用VB写的,很容易懂

解决方案 »

  1.   

    如何通过api获得 非本程序 中具有 焦点 的 控件 的 句柄
      

  2.   

    [DllImport("User32.dll", EntryPoint="GetFocus",  SetLastError=true,ExactSpelling=true,CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Auto)]
    public static extern IntPtr GetFocus();
      

  3.   

    >>如何通过api获得 非本程序 中具有 焦点 的 控件 的 句柄
    可以吗?????应该说用什么方法都不可以.为什么呢?因为根据我了解到的是这样的,一个线程创建一个控件,就会把这个控件的句柄放到一个句柄映射表里,而其他线程要访问是不可能的,这也就是为什么如果你在一个线程类创建的父级控件,其它线程要访问那是不可能的.因为那个表是个局部表,只有创建的线程才是可见的.你说你要"非本程序"那就说要不在同`一个进程内的,可能吗?另:如果,找到方法通知一声.我想看看.谢谢.
      

  4.   

    经本人实验GetFocus只能获取本程序中具有焦点的控件的句柄(XP下,别的没试过),估计就是yarshray兄说的原因计划用WindowFromPoint,如果说“获取Cursor处的控件的句柄”的话很容易实现,现在的问题就是:如何获得非本程序中具有焦点的控件的xy暂时还没有找到解决办法
    :(
      

  5.   

    不知道楼主是为了实现什么目的?还有楼主说“我看了一些资料没搞明白”,不知道是什么资料,能不能让我也瞧一瞧至于yarshray兄说的“同一进程”的问题,建议参考一下:
    http://tongtian.net/cgi-bin/view.cgi?forum=10&topic=752
      

  6.   

    HellMaster(李晋) 获取Cursor处的控件的句柄”的话很容易实现give me some codes
      

  7.   

    我把我那个.cs的代码都给你copy过来好了using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Text;namespace CommonTest
    {
    /// <summary>
    /// Summary description for frmByteStr.
    /// </summary>
    /// 
    public class frmByteStr : System.Windows.Forms.Form
    {
    [DllImport("User32.dll")] 
    public static extern long GetCursorPos(ref PointAPI p);
    [DllImport("User32.Dll")]
    public static extern void GetWindowText(IntPtr h, StringBuilder s, int nMaxCount);
    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(PointAPI p);
    [DllImport("user32.dll")]
    public static extern IntPtr GetFocus(); private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox3;
    private System.ComponentModel.IContainer components; public frmByteStr()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.label2 = new System.Windows.Forms.Label();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.label3 = new System.Windows.Forms.Label();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(8, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(116, 17);
    this.label1.TabIndex = 1;
    this.label1.Text = "鼠标位置处的句柄:";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(8, 64);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(304, 21);
    this.textBox1.TabIndex = 2;
    this.textBox1.Text = "textBox1";
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // label2
    // 
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(8, 8);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(66, 17);
    this.label2.TabIndex = 1;
    this.label2.Text = "鼠标位置:";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(80, 8);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(216, 21);
    this.textBox2.TabIndex = 2;
    this.textBox2.Text = "textBox1";
    // 
    // label3
    // 
    this.label3.AutoSize = true;
    this.label3.Location = new System.Drawing.Point(8, 96);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(66, 17);
    this.label3.TabIndex = 1;
    this.label3.Text = "窗口文字:";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(8, 120);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(304, 21);
    this.textBox3.TabIndex = 2;
    this.textBox3.Text = "textBox1";
    // 
    // frmByteStr
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(344, 174);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.textBox3);
    this.Name = "frmByteStr";
    this.Text = "frmByteStr";
    this.Load += new System.EventHandler(this.frmByteStr_Load);
    this.ResumeLayout(false); }
    #endregion private void frmByteStr_Load(object sender, System.EventArgs e)
    {
    this.TopMost=true;
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    PointAPI pt=new PointAPI();
    IntPtr tmp;
    StringBuilder sb = new StringBuilder(1024); GetCursorPos(ref pt); this.textBox2.Text="X="+pt.X.ToString()+"\tY="+pt.Y.ToString(); tmp=WindowFromPoint(pt);
    this.textBox1.Text=tmp.ToString(); GetWindowText(tmp,sb,sb.Capacity); this.textBox3.Text=sb.ToString();
    }
    }
    }
      

  8.   

    你copy过去,直接就能用了
    运行、动一动鼠标试试!
      

  9.   

    吐血感谢HellMaster(李晋) 兄。没喷到你吧嘿嘿
      

  10.   

    我的问题难住csdn了吗?勇者上啊!!!!还给1000分
      

  11.   

    按照下面的思路走. 
    1.获得当前活动窗口的handle,我用简单的GetForegroundWindow,你可以可以用其他的,我以前给你贴过一个叫弹出窗口的程序中有,自己愿意看就去看看.2.获得活动窗口的线程号,获得与自己程序的窗口相关的线程号
    3.将两个线程的输入联系起来
    4.调用getfocus
    5.线程分离.我做了个代码如下,自己参考参考.还有,作为版主,说句话,开贴给分,小心被删.把别人回答对的帖子结了就足够了.注意结贴率.using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Text;namespace activeWindow
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    [DllImport("user32.dll")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size); [ DllImport("user32.dll") ]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")] 
    private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("user32.dll")] 
    private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach); [DllImport("user32.dll")] 
    private static extern IntPtr GetFocus(); private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label captionWindowLabel;
    private System.Windows.Forms.Label IDWindowLabel;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components; //
    // GetForegroundWindow API 
    //
    private void GetActiveWindow()
    {

    const int nChars = 256;

    StringBuilder Buff = new StringBuilder(nChars);
       
    //获得当前活动窗口
    IntPtr ActiveWindowHandle  = GetForegroundWindow();

    //获得活动窗口的线程号
    IntPtr handle1 = GetWindowThreadProcessId(this.Handle,IntPtr.Zero);
    IntPtr handle2 = GetWindowThreadProcessId(ActiveWindowHandle,IntPtr.Zero);
    if (ActiveWindowHandle != this.Handle)
    {
    if ( GetWindowText(handle1, Buff, nChars) > 0 )
    {
    this.captionWindowLabel.Text = Buff.ToString();
    this.IDWindowLabel.Text = handle1.ToString();
    }
    //将两个线程的输入联系起来
    AttachThreadInput(handle1,handle2,1); //得到当前的具有输入焦点的子窗口 IntPtr handleControl = GetFocus(); this.label3.Text = handleControl.ToString(); //将联到一起的两个线程的输入分离
    AttachThreadInput(handle1,handle2,0);
    } }
    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.captionWindowLabel = new System.Windows.Forms.Label();
    this.IDWindowLabel = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(174, 9);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(143, 19);
    this.label1.TabIndex = 0;
    this.label1.Text = "Active Window Detail";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(20, 46);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(123, 19);
    this.label2.TabIndex = 1;
    this.label2.Text = "Window Caption : ";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(20, 102);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(123, 18);
    this.label3.TabIndex = 2;
    this.label3.Text = "Window Handle :";
    // 
    // captionWindowLabel
    // 
    this.captionWindowLabel.Location = new System.Drawing.Point(143, 46);
    this.captionWindowLabel.Name = "captionWindowLabel";
    this.captionWindowLabel.Size = new System.Drawing.Size(287, 47);
    this.captionWindowLabel.TabIndex = 3;
    // 
    // IDWindowLabel
    // 
    this.IDWindowLabel.Location = new System.Drawing.Point(143, 102);
    this.IDWindowLabel.Name = "IDWindowLabel";
    this.IDWindowLabel.Size = new System.Drawing.Size(128, 18);
    this.IDWindowLabel.TabIndex = 4;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(225, 148);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(62, 28);
    this.button1.TabIndex = 5;
    this.button1.Text = "EXIT";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(512, 200);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.IDWindowLabel,
      this.captionWindowLabel,
      this.label3,
      this.label2,
      this.label1});
    this.Name = "Form1";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Window Information";
    this.TopMost = true;
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    GetActiveWindow();
    } private void button1_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }
    }
    }