日期:2014-05-18  浏览次数:21003 次

[求助]获取指定点窗口的PID
我的方法大概是点击按钮后,挂个全局钩子,当鼠标左键点击时,获取点击点所在窗口的句柄,然后由句柄得到窗口的PID.
但是总是不能成功得到pid ,下面是代码!
请大家帮忙!

C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;

namespace getPid
{
    public partial class Form1 : Form
    {
        //鼠标 左右键
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        //定义钩子句柄
        static int hHook = 0;
        //定义钩子类型
        public const int WH_MOUSE = 14;
        //定义钩子处理函数

        HookProc MouseHookProcedure;
        [StructLayout(LayoutKind.Sequential)]

        public class POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        //导入windows Api函数声明,这里要用到的函数有
        [DllImport("user32.dll", CharSet = CharSet.Auto,
        CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
        CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
        CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
        IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
        public static extern int WindowFromPoint(
            int xPoint,
            int yPoint
        );

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern int GetWindowThreadProcessId(
            int hwnd,
            ref int lpdwProcessId
        );

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process currentProcess = Process.GetCurrentProcess();
            if (hHook == 0)
            {
                MouseHookProcedure = new HookProc(this.MouseHookProc);
                //这里挂节全局钩子
                hHook = SetWindowsHookEx(WH_MOUSE,
                MouseHookProcedure,
                currentProcess.MainModule.BaseAddress,
                0);

                if (hHook == 0)
                {
                    MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }
            }
        }

        public int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
            int w=0;
            if (nCode < 0)
            {
                return CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                //鼠标左键按下
                if (wParam == (System.IntPtr)WM_LBUTTONDOWN)
                {
                    //释放鼠标钩子
                    bool ret = UnhookWindowsHookEx(hHook);
                    if (ret == false)
                    {
                        MessageBox.Show("UnhookWindowsHookEx Failed");
                        return 0;
                    }
                    hHook = 0;
                    //获得鼠标单击窗体的pid