Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Windows.Forms;
- namespace System
- {
- /// <summary>
- /// Highlights the specified window just like Spy++
- /// </summary>
- /// <param name="hWnd"></param>
- ///
- public static class WindowHighlighter
- {
- private static float m_DefaultPenWidth = 3;
- private static Color m_DefaultBoarderColor = Color.Red;
- public static float DefaultPenWidth
- {
- get { return WindowHighlighter.m_DefaultPenWidth; }
- set { WindowHighlighter.m_DefaultPenWidth = value; }
- }
- public static Color DefaultBoarderColor
- {
- get { return WindowHighlighter.m_DefaultBoarderColor; }
- set { WindowHighlighter.m_DefaultBoarderColor = value; }
- }
- public static void Highlight()
- {
- if (Form.ActiveForm == null)
- return;
- Control cnt = Form.ActiveForm.ActiveControl;
- if (cnt == null)
- return;
- ValidateControlHandle(cnt);
- Highlight(cnt.Handle,DefaultBoarderColor,DefaultPenWidth);
- }
- public static void Highlight(Control cnt)
- {
- Highlight(cnt.Handle,DefaultBoarderColor,DefaultPenWidth);
- }
- public static void Highlight(Control cnt, Color boarderColor)
- {
- Highlight(cnt, boarderColor, DefaultPenWidth);
- }
- public static void Highlight(Control cnt, Color boarderColor, float penWidth)
- {
- ValidateControlHandle(cnt);
- Highlight(cnt.Handle, boarderColor, penWidth);
- }
- private static void ValidateControlHandle(Control cnt)
- {
- if (!cnt.IsHandleCreated)
- throw new Exception("Handle not created yet");
- }
- public static void Highlight(IntPtr hWnd)
- {
- Highlight(hWnd, DefaultBoarderColor,DefaultPenWidth);
- }
- public static void Highlight(IntPtr hWnd, Color boarderColor)
- {
- Highlight(hWnd, boarderColor, DefaultPenWidth);
- }
- public static void Highlight(IntPtr hWnd, Color boarderColor, float penWidth)
- {
- Win32.Rect rc = new Win32.Rect();
- Win32.GetWindowRect(hWnd, ref rc);
- IntPtr hDC = Win32.GetWindowDC(hWnd);
- if (hDC != IntPtr.Zero)
- {
- using (Pen pen = new Pen(boarderColor, penWidth))
- {
- using (Graphics g = Graphics.FromHdc(hDC))
- {
- g.DrawRectangle(pen, 0, 0, rc.right - rc.left - (int)penWidth, rc.bottom - rc.top - (int)penWidth);
- }
- }
- }
- Win32.ReleaseDC(hWnd, hDC);
- }
- /// <summary>
- /// Forces a window to refresh, to eliminate our funky highlighted border
- /// </summary>
- /// <param name="hWnd"></param>
- ///
- public static void Refresh(Control cnt)
- {
- ValidateControlHandle(cnt);
- Refresh(cnt.Handle);
- }
- public static void Refresh(IntPtr hWnd)
- {
- Win32.InvalidateRect(hWnd, IntPtr.Zero, 1 /* TRUE */);
- Win32.UpdateWindow(hWnd);
- Win32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, Win32.RDW_FRAME | Win32.RDW_INVALIDATE | Win32.RDW_UPDATENOW | Win32.RDW_ALLCHILDREN);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment