Exemple #1
0
 public void Startup()
 {
     // Subscribe the windows events which tell us a title was changed
     _winEventObservable = WinEventHook.WindowTileChangeObservable()
                           .Select(info => User32Api.GetText(info.Handle))
                           .Where(title => !string.IsNullOrEmpty(title))
                           .Subscribe(MonitorTitleChangeEvent);
 }
 /// <summary>
 ///     Get the Windows caption (title)
 /// </summary>
 /// <param name="interopWindow">InteropWindow</param>
 /// <param name="forceUpdate">set to true to make sure the value is updated</param>
 /// <returns>string with the caption</returns>
 public static string GetCaption(this IInteropWindow interopWindow, bool forceUpdate = false)
 {
     if (interopWindow.Caption == null || forceUpdate)
     {
         var caption = User32Api.GetText(interopWindow.Handle);
         interopWindow.Caption = caption;
     }
     return(interopWindow.Caption);
 }
        /// <summary>
        ///     Get the current "ClipboardOwner" but only if it isn't us!
        /// </summary>
        /// <returns>current clipboard owner</returns>
        private static string GetClipboardOwner()
        {
            string owner = null;

            try
            {
                var hWnd = ClipboardNative.CurrentOwner;
                if (hWnd != IntPtr.Zero)
                {
                    try
                    {
                        User32Api.GetWindowThreadProcessId(hWnd, out var pid);
                        using (var me = Process.GetCurrentProcess())
                            using (var ownerProcess = Process.GetProcessById(pid))
                            {
                                // Exclude myself
                                if (me.Id != ownerProcess.Id)
                                {
                                    // Get Process Name
                                    owner = ownerProcess.ProcessName;
                                    // Try to get the starting Process Filename, this might fail.
                                    try
                                    {
                                        owner = ownerProcess.Modules[0].FileName;
                                    }
                                    catch (Exception)
                                    {
                                        // Ignore
                                    }
                                }
                            }
                    }
                    catch (Exception e)
                    {
                        Log.Warn().WriteLine(e, "Non critical error: Couldn't get clipboard process, trying to use the title.");
                        owner = User32Api.GetText(hWnd);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Warn().WriteLine(e, "Non critical error: Couldn't get clipboard owner.");
            }
            return(owner);
        }
Exemple #4
0
        /// <summary>
        ///     Get the Windows caption (title)
        /// </summary>
        /// <param name="interopWindow">InteropWindow</param>
        /// <param name="forceUpdate">set to true to make sure the value is updated</param>
        /// <returns>string with the caption</returns>
        public static string GetCaption(this IInteropWindow interopWindow, bool forceUpdate = false)
        {
            if (interopWindow.Caption != null && !forceUpdate)
            {
                return(interopWindow.Caption);
            }

            // Calling User32Api.GetText (GetWindowText) for the current Process will hang, deadlock, this should be ignored
            if (interopWindow.IsOwnedByCurrentThread())
            {
                // TODO: it might have a value, but can't get it. Returning null would be bad... so return empty
                interopWindow.Caption = string.Empty;
                Log.Warn().WriteLine("Do not call GetWindowText for a Window ({0}) which belongs the current thread! An empty string is returned.", interopWindow.Handle);
            }
            else
            {
                var caption = User32Api.GetText(interopWindow.Handle);
                interopWindow.Caption = caption;
            }
            return(interopWindow.Caption);
        }