using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Reflection; namespace WpfApp2 { public partial class MainWindow : Window { [StructLayout(LayoutKind.Sequential)] public struct RECT { public uint Left; public uint Top; public uint Right; public uint Bottom; }; [StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO { public uint cbSize; public uint flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rcCaret; }; Point startPosition = new Point(); // Point required for ToolTip movement by Mouse GUITHREADINFO guiInfo; // To store GUI Thread Information Point caretPosition; // To store Caret Position [DllImport("user32.dll")] static extern int GetWindowText(int hWnd, StringBuilder text, int count); [DllImport("user32.dll", SetLastError = true)] static extern uint GetWindowThreadProcessId(int hWnd, out uint lpdwProcessId); [DllImport("user32.dll", EntryPoint = "GetGUIThreadInfo")] public static extern bool GetGUIThreadInfo(uint tId, out GUITHREADINFO threadInfo); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern bool ClientToScreen(IntPtr hWnd, out Point position); private void AdjustUI() { this.Left = caretPosition.X; this.Top = caretPosition.Y; } public void GetCaretPosition() { guiInfo = new GUITHREADINFO(); guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo); GetGUIThreadInfo(0, out guiInfo); } private string GetActiveProcess() { const int nChars = 256; int handle = 0; StringBuilder Buff = new StringBuilder(nChars); handle = (int)GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { uint lpdwProcessId; uint dwCaretID = GetWindowThreadProcessId(handle, out lpdwProcessId); uint dwCurrentID = (uint)Thread.CurrentThread.ManagedThreadId; return Process.GetProcessById((int)lpdwProcessId).ProcessName; } return String.Empty; } private void EvaluateCaretPosition() { caretPosition = new Point(); GetCaretPosition(); caretPosition.X = (int)guiInfo.rcCaret.Left + 25; caretPosition.Y = (int)guiInfo.rcCaret.Bottom + 25; ClientToScreen(guiInfo.hwndCaret, out caretPosition); } public MainWindow() { InitializeComponent(); while (true) { //if (GetForegroundWindow() ==MainWindow.GetForegroundWindow()) //{ // return; //} string activeProcess = GetActiveProcess(); if ((activeProcess.ToLower().Contains("explorer") | (activeProcess == string.Empty))) { m.Visibility = Visibility.Hidden; } else { EvaluateCaretPosition(); AdjustUI(); m.Visibility = Visibility.Visible; } MessageBox.Show("caret position {0}", caretPosition.X.ToString()); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)