Example #1
0
        public override void HookProcedure(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            var eventName = EventMapConverter.CodeToName((int)eventType);

            if ((uint)idObject != OBJID_WINDOW)
            {
                return;
            }
            if (eventName == EventName.UNKNOWN)
            {
                return;
            }
            var windowInfo         = new WindowInfoWithHandle(hwnd);
            var windowLong         = GetWindowLong(hwnd, GWL_STYLE);
            var windowTitle        = windowInfo.WindowTitle;
            var isVisible          = (windowLong & WS_VISIBLE) == WS_VISIBLE;
            var isOverlappedwindow = (windowLong & WS_OVERLAPPEDWINDOW) == WS_OVERLAPPEDWINDOW;
            var isUwp = (windowLong & WS_UWP) == WS_UWP;

            if (windowLong == 0)
            {
                return;
            }
            if ((windowLong & WS_CHILD) == WS_CHILD)
            {
                return;
            }


            if (!WindowHandles.Contains(windowInfo))
            {
                if (isVisible && isOverlappedwindow && !isUwp &&
                    (eventName == EventName.EVENT_OBJECT_SHOW ||
                     eventName == EventName.EVENT_OBJECT_NAMECHANGE))
                {
                    //Debug.WriteLine("Window created: " + windowTitle + " : " + windowLong.ToString("x8"));
                    WindowHandles.Add(windowInfo);
                    OnAddEvent(windowInfo);
                    OnShowEvent(windowInfo);

                    /*
                     * Debug.WriteLine("---");
                     * Debug.WriteLine(windowTitle + ":" + is_visible + ":" + eventName + ":" + windowLong);
                     * Debug.WriteLine("WS_VISIBLE = " + ((windowLong & WS_VISIBLE) == WS_VISIBLE));
                     * Debug.WriteLine("WS_OVERLAPPEDWINDOW = " + ((windowLong & WS_OVERLAPPEDWINDOW) == WS_OVERLAPPEDWINDOW));
                     */
                }
            }
            else
            {
                //Debug.WriteLine( " - " + windowTitle + " : " + eventName + ": " + windowLong.ToString("x8"));

                if (eventName == EventName.EVENT_OBJECT_DESTROY)
                {
                    // Window がなくなった
                    //Debug.WriteLine("Window destroyed: " + windowTitle + " : " + windowLong.ToString("x8"));
                    WindowHandles.Remove(windowInfo);
                    OnRemoveEvent(windowInfo);
                    OnDestroyEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_OBJECT_HIDE)
                {
                    // Window が HIDDEN
                    //Debug.WriteLine("Window hide: " + windowTitle + " : " + windowLong.ToString("x8"));
                    WindowHandles.Remove(windowInfo);
                    OnRemoveEvent(windowInfo);
                    OnHideEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_SYSTEM_MOVESIZESTART)
                {
                    // Window がマウスでドラッグ開始
                    //Debug.WriteLine("Window mouse drag start: " + windowTitle + " : " + windowLong.ToString("x8"));
                    MouseDraggingWindowHandle = windowInfo;
                    OnMouseDragStartEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_SYSTEM_MOVESIZEEND)
                {
                    // Window がマウスでドラッグ終了
                    if (MouseDraggingWindowHandle.Equals(windowInfo))
                    {
                        //Debug.WriteLine("Window mouse drag end: " + windowTitle + " : " + windowLong.ToString("x8"));
                        MouseDraggingWindowHandle = null;
                        OnMouseDragEndEvent(windowInfo);
                    }
                }
                else if (eventName == EventName.EVENT_OBJECT_LOCATIONCHANGE)
                {
                    // ショートカットキーだけで場所移動
                    if (MouseDraggingWindowHandle == null)
                    {
                        //Debug.WriteLine("Window location change: " + windowTitle + " : " + windowLong.ToString("x8"));
                        OnLocationChangeEvent(windowInfo);
                    }
                }
            }

            if (WindowHandles.Contains(windowInfo))
            {
                // Debug.WriteLine(" - " + eventName + " : " + windowTitle);
            }

            /*
             * Debug.WriteLine("----");
             * Debug.WriteLine("hWinEventHook:" + hWinEventHook);
             * Debug.WriteLine("eventType:"+eventName);
             * Debug.WriteLine("hwnd:" + hwnd);
             * Debug.WriteLine("title:" + GetCurrentWindowTitle(hwnd));
             * Debug.WriteLine("idObject:" + idObject);
             * Debug.WriteLine("idChild:" + idChild);
             * Debug.WriteLine("dwEventThread:" + dwEventThread);
             * Debug.WriteLine("dwmsEventTime:" + dwmsEventTime);
             */
        }
Example #2
0
        public override void HookProcedure(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            var eventName = EventMapConverter.CodeToName((int)eventType);

            if ((uint)idObject != OBJID_WINDOW)
            {
                return;
            }
            if (eventName == EventName.UNKNOWN)
            {
                return;
            }
            var needleWindowInfo = new WindowInfoWithHandle(hWnd);
            var windowLong       = GetWindowLong(hWnd, GWL_STYLE);
            var windowLongString = windowLong.ToString("x8");
            var windowTitle      = needleWindowInfo.windowTitle;

            if (windowLong == 0 ||
                ((windowLong & WS_POPUP) == WS_POPUP) ||
                ((windowLong & WS_CHILD) == WS_CHILD))
            {
                return;
            }

            var contains  = this.windowInfos.Contains(needleWindowInfo);
            var processId = needleWindowInfo.ComputeProcessId();

            Logger.DebugWindowInfo($"{eventName} : ProcessId={processId}, Contains={contains}", needleWindowInfo);

            if (!this.windowInfos.Contains(needleWindowInfo))
            {
                if (this.IsValidWindow(needleWindowInfo, windowLong) &&
                    (eventName == EventName.EVENT_OBJECT_SHOW))
                //eventName == EventName.EVENT_OBJECT_SHOW ||
                //eventName == EventName.EVENT_OBJECT_NAMECHANGE))
                {
                    Logger.WriteLine($"TraceWindows.HookProcedure OnAdd : ({hWnd}){windowTitle} : {windowLongString}");
                    this.windowInfos.Add(needleWindowInfo);
                    OnAddEvent(needleWindowInfo);
                    OnShowEvent(needleWindowInfo);
                }
            }
            else
            {
                var windowInfo = this.windowInfos.Find(
                    (WindowInfoWithHandle wi) => { return(wi.windowHandle == needleWindowInfo.windowHandle); });
                if (windowInfo == null)
                {
                    return;
                }

                if (eventName == EventName.EVENT_OBJECT_DESTROY)
                {
                    // Window がなくなった
                    Logger.WriteLine($"TraceWindows.HookProcedure OnDestroy : ({hWnd}){windowTitle} : {windowLongString}");
                    this.windowInfos.Remove(windowInfo);
                    OnRemoveEvent(windowInfo);
                    OnDestroyEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_OBJECT_HIDE)
                {
                    // Window が HIDDEN
                    Logger.WriteLine($"TraceWindows.HookProcedure OnHide : ({hWnd}){windowTitle} : {windowLongString}");
                    this.windowInfos.Remove(windowInfo);
                    OnRemoveEvent(windowInfo);
                    OnHideEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_SYSTEM_MOVESIZESTART)
                {
                    // Window がマウスでドラッグ開始
                    Logger.WriteLine($"TraceWindows.HookProcedure OnMouseDragStart : ({hWnd}){windowTitle} : {windowLongString}");
                    this.mouseDraggingWindowHandle = windowInfo;
                    OnMouseDragStartEvent(windowInfo);
                }
                else if (eventName == EventName.EVENT_SYSTEM_MOVESIZEEND)
                {
                    // Window がマウスでドラッグ終了
                    if (this.mouseDraggingWindowHandle.Equals(windowInfo))
                    {
                        Logger.WriteLine($"TraceWindows.HookProcedure OnMouseDragEnd : ({hWnd}){windowTitle} : {windowLongString}");
                        this.mouseDraggingWindowHandle = null;
                        OnMouseDragEndEvent(windowInfo);
                        OnLocationChangeEvent(windowInfo);
                    }
                }
                else if (eventName == EventName.EVENT_OBJECT_LOCATIONCHANGE)
                {
                    // ショートカットキーだけで場所移動
                    if (this.mouseDraggingWindowHandle == null)
                    {
                        Logger.WriteLine($"TraceWindows.HookProcedure OnLocationChange : ({hWnd}){windowTitle} : {windowLongString}");
                        OnLocationChangeEvent(windowInfo);
                    }
                }
                else if (eventName == EventName.EVENT_SYSTEM_FOREGROUND)
                {
                    // Window がアクティヴになった
                    Logger.WriteLine($"TraceWindows.HookProcedure OnActivate : ({hWnd}){windowTitle} : {windowLongString}");
                    OnActivateEvent(windowInfo);
                }
            }
        }