从setwindowhook获取findow标题

时间:2018-09-25 11:17:33

标签: c# setwindowshookex

首先我不确定我是否正确执行了此操作(setSetWindowsHookEx)。 我想从一个进程(第三方应用程序)创建的所有窗口中得到通知,并获得标题以查看它是否是我要使用的窗口,如果可以的话可以得到他的位置并在其上放置一些表单,以及SetWinEventHook,因此如果此窗口移动,表单将在窗口上保持蜂鸣状态。 如何从新窗口获取标题?

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public enum HookType : int
    {
        WH_JOURNALRECORD = 0,
        WH_JOURNALPLAYBACK = 1,
        WH_KEYBOARD = 2,
        WH_GETMESSAGE = 3,
        WH_CALLWNDPROC = 4,
        WH_CBT = 5,
        WH_SYSMSGFILTER = 6,
        WH_MOUSE = 7,
        WH_HARDWARE = 8,
        WH_DEBUG = 9,
        WH_SHELL = 10,
        WH_FOREGROUNDIDLE = 11,
        WH_CALLWNDPROCRET = 12,
        WH_KEYBOARD_LL = 13,
        WH_MOUSE_LL = 14
    }
    public enum ShellEvents : int {
        HSHELL_WINDOWCREATED = 1,
        HSHELL_WINDOWDESTROYED = 2,
        HSHELL_ACTIVATESHELLWINDOW = 3,
        HSHELL_WINDOWACTIVATED = 4,
        HSHELL_GETMINRECT = 5,
        HSHELL_REDRAW = 6,
        HSHELL_TASKMAN = 7,
        HSHELL_LANGUAGE = 8,
        HSHELL_ACCESSIBILITYSTATE = 11
    }


    private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
    static IntPtr hHook;


    HookProc myCallbackDelegate;



    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);


    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern IntPtr GetModuleHandle(string lpModuleName);
   //const int HSHELL_WINDOWCREATED = 1;

    public int MyCallbackFunction(int nCode, IntPtr wParam, IntPtr lParam)
    {

        if (Convert.ToInt32 (nCode) >0 )//HSHELL_WINDOWCREATED)
        {

            //MessageBox.Show("hola");
        }

       //Is this correct or should be on a else?

        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }


    [DllImport("kernel32.dll")]
    static extern uint GetCurrentThreadId();
    private void Form1_Load(object sender, EventArgs e)
    {
        myCallbackDelegate = new HookProc(MyCallbackFunction);
       processHandle);



        hHook = SetWindowsHookEx(HookType.WH_SHELL, myCallbackDelegate, (IntPtr)0,GetCurrentThreadId());

    }
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool UnhookWindowsHookEx(IntPtr hhk);
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        UnhookWindowsHookEx(hHook);
    }
}

1 个答案:

答案 0 :(得分:0)

SetWindowsHookEx是赋予安装钩子权限的功能(除了鼠标和键盘外,本地钩子不是全局的)

要获得Windows的标题,可以使用 该链接List Processes