Exemple #1
0
        /// <summary>
        /// Handles the MouseUp event of the picTarget control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void picTarget_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            IntPtr hWnd;
            IntPtr hTemp;

            // End targeting
            isTargetingWindow = false;

            // Unhighlight window
            if (targetedWindowHandle != IntPtr.Zero)
            {
                Win32Ex.HighlightWindow(targetedWindowHandle);
            }
            targetedWindowHandle = IntPtr.Zero;

            // Reset capture image and cursor
            picTarget.Cursor = Cursors.Default;
            picTarget.Image  = imageList.Images[0];

            // Get screen coords from client coords and window handle
            hWnd = Win32Ex.WindowFromPoint(picTarget.Handle, e.X, e.Y);

            // Get owner
            while ((hTemp = Win32.GetParent(hWnd)) != IntPtr.Zero)
            {
                hWnd = hTemp;
            }

            ShowWindowInfo(hWnd, true);

            // Release capture
            Win32.SetCapture(IntPtr.Zero);
        }
Exemple #2
0
        /// <summary>
        /// Highlights the specified window, if valid.
        /// </summary>
        private void HighlightValidWindow(IntPtr hWnd, IntPtr hOwner)
        {
            // Check for valid highlight
            if (targetedWindowHandle == hWnd)
            {
                return;
            }

            // Check for relative
            if (Win32Ex.IsRelativeWindow(hWnd, hOwner, true))
            {
                // Unhighlight last window
                if (targetedWindowHandle != IntPtr.Zero)
                {
                    Win32Ex.HighlightWindow(targetedWindowHandle);
                    targetedWindowHandle = IntPtr.Zero;
                }

                return;
            }

            // Unhighlight last window
            Win32Ex.HighlightWindow(targetedWindowHandle);

            // Set as current target
            targetedWindowHandle = hWnd;

            // Highlight window
            Win32Ex.HighlightWindow(hWnd);
        }
Exemple #3
0
        static IntPtr FindWindow(Process process, string title, bool quiet)
        {
            // Attach to app's main window
            if (process.HasExited)
            {
                if (!quiet)
                {
                    MessageBox.Show("Application exited before it could be trayed.", "TrayMe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return(IntPtr.Zero);
            }

            if (!string.IsNullOrEmpty(title))
            {
                var hWnd = TryMany <IntPtr>(() => Win32Ex.FindWindowByPartialTitle(title));
                if (hWnd == IntPtr.Zero && !quiet)
                {
                    MessageBox.Show("Could not find application's \"" + title + "\" window.", "TrayMe", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return(hWnd);
            }

            var hMainWindow = process.MainWindowHandle;

            if (hMainWindow == IntPtr.Zero && !quiet)
            {
                MessageBox.Show("Could not find application's main window.", "TrayMe", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(hMainWindow);
        }
Exemple #4
0
        /// <summary>
        /// Shows the information of the window with the specified handle.
        /// </summary>
        private void ShowWindowInfo(IntPtr hWnd, bool handle)
        {
            if ((Win32.IsWindow(hWnd) == 0) || (Win32Ex.IsRelativeWindow(hWnd, Handle, true)))
            {
                if (handle)
                {
                    textHandle.Text = "";
                }
                textCaption.Text = "";
                textCharset.Text = "";
                return;
            }

            // Set window information
            if (handle)
            {
                textHandle.Text = Convert.ToString(hWnd.ToInt32(), 16).ToUpper().PadLeft(8, '0');
            }
            textCaption.Text = Win32Ex.GetWindowText(hWnd);
            textCharset.Text = ((Win32.IsWindowUnicode(hWnd) != 0) ? ("Unicode") : ("Ansi"));
        }
Exemple #5
0
        /// <summary>
        /// Handles the MouseMove event of the picTarget control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void picTarget_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            IntPtr hWnd;
            IntPtr hTemp;

            Win32.POINTAPI pt;

            // TODO: Draw border around EVERY window

            pt.x = e.X;
            pt.y = e.Y;
            Win32.ClientToScreen(picTarget.Handle, ref pt);

            // Make sure targeting before highlighting windows
            if (!isTargetingWindow)
            {
                return;
            }

            // Get screen coords from client coords and window handle
            hWnd = Win32Ex.WindowFromPoint(picTarget.Handle, e.X, e.Y);

            // Get real window
            if (hWnd != IntPtr.Zero)
            {
                hTemp = IntPtr.Zero;

                while (true)
                {
                    Win32.MapWindowPoints(hTemp, hWnd, ref pt, 1);
                    hTemp = (IntPtr)Win32.ChildWindowFromPoint(hWnd, pt.x, pt.y);
                    if (hTemp == IntPtr.Zero)
                    {
                        break;
                    }
                    if (hWnd == hTemp)
                    {
                        break;
                    }
                    hWnd = hTemp;
                }

                // TODO: Work with ALL windows

                /*
                 * Win32.ScreenToClient(hWnd, ref pt);
                 * Win32.MapWindowPoints(IntPtr.Zero, hWnd, ref pt, 2);
                 * if ((hTemp = (IntPtr)Win32.ChildWindowFromPoint(hWnd, pt.x, pt.y)) != IntPtr.Zero)
                 * {
                 * hWnd = hTemp;
                 * }
                 */
            }

            // Get owner
            while ((hTemp = Win32.GetParent(hWnd)) != IntPtr.Zero)
            {
                hWnd = hTemp;
            }

            // Show info
            ShowWindowInfo(hWnd, true);

            // Highlight valid window
            HighlightValidWindow(hWnd, Handle);
        }