andrew4582

Window Highlighter

Aug 15th, 2010
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace System
  7. {
  8.     /// <summary>
  9.     /// Highlights the specified window just like Spy++
  10.     /// </summary>
  11.     /// <param name="hWnd"></param>
  12.     ///
  13.     public static class WindowHighlighter
  14.     {
  15.         private static float m_DefaultPenWidth = 3;
  16.         private static Color m_DefaultBoarderColor = Color.Red;
  17.  
  18.         public static float DefaultPenWidth
  19.         {
  20.             get { return WindowHighlighter.m_DefaultPenWidth; }
  21.             set { WindowHighlighter.m_DefaultPenWidth = value; }
  22.         }
  23.         public static Color DefaultBoarderColor
  24.         {
  25.             get { return WindowHighlighter.m_DefaultBoarderColor; }
  26.             set { WindowHighlighter.m_DefaultBoarderColor = value; }
  27.         }
  28.         public static void Highlight()
  29.         {
  30.             if (Form.ActiveForm == null)
  31.                 return;
  32.             Control cnt = Form.ActiveForm.ActiveControl;
  33.             if (cnt == null)
  34.                 return;
  35.             ValidateControlHandle(cnt);
  36.             Highlight(cnt.Handle,DefaultBoarderColor,DefaultPenWidth);
  37.         }
  38.         public static void Highlight(Control cnt)
  39.         {
  40.             Highlight(cnt.Handle,DefaultBoarderColor,DefaultPenWidth);
  41.         }
  42.         public static void Highlight(Control cnt, Color boarderColor)
  43.         {
  44.             Highlight(cnt, boarderColor, DefaultPenWidth);
  45.         }
  46.         public static void Highlight(Control cnt, Color boarderColor, float penWidth)
  47.         {
  48.             ValidateControlHandle(cnt);
  49.             Highlight(cnt.Handle, boarderColor, penWidth);
  50.         }
  51.         private static void ValidateControlHandle(Control cnt)
  52.         {
  53.             if (!cnt.IsHandleCreated)
  54.                 throw new Exception("Handle not created yet");
  55.         }
  56.         public static void Highlight(IntPtr hWnd)
  57.         {
  58.             Highlight(hWnd, DefaultBoarderColor,DefaultPenWidth);
  59.         }
  60.         public static void Highlight(IntPtr hWnd, Color boarderColor)
  61.         {
  62.             Highlight(hWnd, boarderColor, DefaultPenWidth);
  63.         }
  64.  
  65.         public static void Highlight(IntPtr hWnd, Color boarderColor, float penWidth)
  66.         {
  67.             Win32.Rect rc = new Win32.Rect();
  68.             Win32.GetWindowRect(hWnd, ref rc);
  69.  
  70.             IntPtr hDC = Win32.GetWindowDC(hWnd);
  71.             if (hDC != IntPtr.Zero)
  72.             {
  73.                 using (Pen pen = new Pen(boarderColor, penWidth))
  74.                 {
  75.                     using (Graphics g = Graphics.FromHdc(hDC))
  76.                     {
  77.                         g.DrawRectangle(pen, 0, 0, rc.right - rc.left - (int)penWidth, rc.bottom - rc.top - (int)penWidth);
  78.                     }
  79.                 }
  80.             }
  81.             Win32.ReleaseDC(hWnd, hDC);
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Forces a window to refresh, to eliminate our funky highlighted border
  86.         /// </summary>
  87.         /// <param name="hWnd"></param>
  88.         ///
  89.         public static void Refresh(Control cnt)
  90.         {
  91.             ValidateControlHandle(cnt);
  92.             Refresh(cnt.Handle);
  93.         }
  94.         public static void Refresh(IntPtr hWnd)
  95.         {
  96.             Win32.InvalidateRect(hWnd, IntPtr.Zero, 1 /* TRUE */);
  97.             Win32.UpdateWindow(hWnd);
  98.             Win32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, Win32.RDW_FRAME | Win32.RDW_INVALIDATE | Win32.RDW_UPDATENOW | Win32.RDW_ALLCHILDREN);
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment