コード例 #1
0
        private static Rectangle GetFrameRect(IntPtr hWnd)
        {
            Rectangle windowRect;

            User32Interop.RECT            rect;
            User32Interop.POINT           point = new User32Interop.POINT(0, 0);
            User32Interop.WINDOWPLACEMENT wp    = User32Interop.WINDOWPLACEMENT.Default;
            // Get window border information
            User32Interop.WINDOWINFO wi = new User32Interop.WINDOWINFO(null);
            User32Interop.GetWindowInfo(hWnd, ref wi);
            // Get window placement information (maximized, minimized, etc)
            User32Interop.GetWindowPlacement(hWnd, out wp);
            // Get window rectangle
            User32Interop.GetWindowRect(hWnd, out rect);
            if (wp.ShowCmd == User32Interop.ShowWindowCommands.Maximize)
            {
                rect.left   += (int)wi.cxWindowBorders;
                rect.top    += (int)wi.cyWindowBorders;
                rect.right  -= (int)wi.cxWindowBorders;
                rect.bottom -= (int)wi.cyWindowBorders;
                windowRect   = new Rectangle(rect.left, rect.top, rect.right, rect.bottom);
            }
            else
            {
                windowRect = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
            }
            return(windowRect);
        }
コード例 #2
0
        public bool RegisterNewInstance()
        {
            // Get guid of the executing assembly
            Assembly      assembly      = Assembly.GetExecutingAssembly();
            GuidAttribute guidAttribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
            string        guid          = guidAttribute.Value;

            // Create mutext
            this.mutex = new Mutex(false, guid);

            // Register notify message
            if (notifyInstanceMessage == 0)
            {
                notifyInstanceMessage = User32Interop.RegisterWindowMessage(notifyMessageName);
            }

            // Wait on the mutext, false return value means that another instance is running
            if (!this.mutex.WaitOne(0, false))
            {
                if (notifyInstanceMessage != 0)
                {
                    User32Interop.PostMessage((IntPtr)User32Interop.HWND_BROADCAST, notifyInstanceMessage, 0, 0);
                }
                return(false);
            }

            // Create a window to receive notifications
            if (this.siWindow == null)
            {
                this.siWindow = new SIWindow(this);
            }
            return(true);
        }
コード例 #3
0
        private static string GetText(IntPtr hWnd)
        {
            int           length = (int)User32Interop.SendMessage(hWnd, User32Interop.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
            StringBuilder sb     = new StringBuilder(length + 1);

            User32Interop.SendMessage(hWnd, User32Interop.WM_GETTEXT, (IntPtr)sb.Capacity, sb);
            return(sb.ToString());
        }
コード例 #4
0
        public IntPtr Find()
        {
            if (!this.isFinding)
            {
                return(IntPtr.Zero);
            }
            IntPtr hNewWnd;

            User32Interop.POINT pt = new User32Interop.POINT();
            if (false == User32Interop.GetCursorPos(out pt))
            {
                hNewWnd = IntPtr.Zero;
            }
            else
            {
                hNewWnd = User32Interop.WindowFromPoint(pt);
            }
            // Check if it is the previous handle
            if (hNewWnd == this.hWnd)
            {
                Debug.WriteLine("WindowFinder Process: [Same]");
                return(hNewWnd);
            }
            // Make sure the window does not belog to current process
            if (hNewWnd != IntPtr.Zero)
            {
                uint processId;
                User32Interop.GetWindowThreadProcessId(hNewWnd, out processId);
                int currentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;
                Debug.WriteLineIf(processId != 0,
                                  "WindowFinder Process: " + Process.GetProcessById((int)processId).ProcessName);
                if (processId == currentProcessId)
                {
                    hNewWnd = IntPtr.Zero;
                }
            }
            // Clear previous frame
            if (this.hWnd != IntPtr.Zero)
            {
                DrawFrame(this.hWnd);
            }
            if (hNewWnd != IntPtr.Zero)
            {
                // Get window text
                this.text = GetText(hNewWnd);
                // Draw new frame
                DrawFrame(hNewWnd);
            }
            else
            {
                this.text = string.Empty;
            }
            // Return new handle
            this.hWnd = hNewWnd;
            return(this.hWnd);
        }
コード例 #5
0
 /// <summary>
 /// Unregisters a hot key from the system.
 /// </summary>
 /// <param name="keys">The keys that are associated with the hot key.</param>
 public void UnregisterHotKey(Keys hotKey)
 {
     lock (syncRoot) {
         if (!this.registredKeys.ContainsKey(hotKey))
         {
             throw new ArgumentException("Specified hot key is not registered.");
         }
         int id = this.registredKeys[hotKey];
         User32Interop.UnregisterHotKey(window.Handle, id);
         this.registredKeys.Remove(hotKey);
     }
 }
コード例 #6
0
 /// <summary>
 /// Registers a hot key in the system.
 /// </summary>
 /// <param name="keys">The keys that are associated with the hot key.</param>
 public void RegisterHotKey(Keys hotKey)
 {
     lock (syncRoot) {
         if (this.registredKeys.ContainsKey(hotKey))
         {
             throw new ArgumentException("Specified hot key already registered.");
         }
         this.currentId++;
         this.registredKeys.Add(hotKey, this.currentId);
         // register the hot key.
         ModifierKeys modifiers;
         Keys         extractedKeys;
         ExtractKeys(hotKey, out modifiers, out extractedKeys);
         if (!User32Interop.RegisterHotKey(window.Handle, this.currentId, (uint)modifiers, (uint)extractedKeys))
         {
             throw new InvalidOperationException("Couldn’t register the hot key.", new Win32Exception());
         }
     }
 }
コード例 #7
0
        private static void DrawFrame(IntPtr hWnd)
        {
            IntPtr hDC = User32Interop.GetWindowDC(hWnd);

            if (hDC == IntPtr.Zero)
            {
                return;
            }
            try {
                using (Graphics graphics = Graphics.FromHdc(hDC)) {
                    Rectangle rect = GetFrameRect(hWnd);
                    ControlPaint.DrawReversibleFrame(rect, frameColor, FrameStyle.Thick);
                    rect.Inflate(-2, -2);
                    ControlPaint.DrawReversibleFrame(rect, frameColor, FrameStyle.Thick);
                }
            }
            finally {
                User32Interop.ReleaseDC(hWnd, hDC);
            }
        }
コード例 #8
0
 private void Dispose(bool disposing)
 {
     // Check to see if Dispose has already been called.
     if (!this.disposed)
     {
         // If disposing equals true, dispose all managed
         // and unmanaged resources.
         if (disposing)
         {
             // unregister all the registered hot keys.
             foreach (var item in this.registredKeys)
             {
                 User32Interop.UnregisterHotKey(window.Handle, item.Value);
             }
             this.registredKeys.Clear();
             // dispose the inner native window.
             window.Dispose();
         }
         // Note disposing has been done.
         disposed = true;
     }
 }