Guest User

Untitled

a guest
Dec 18th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace Experiment_VS_Graphics_FromHwnd
  9. {
  10.     public class Form1 : Form
  11.     {
  12.         private IContainer components = null;
  13.         private Timer timer1;
  14.         System.Diagnostics.Process visualStudio;
  15.         HashSet<IntPtr> windowHandles = new HashSet<IntPtr>();
  16.  
  17.         protected override void Dispose(bool disposing)
  18.         {
  19.             if (disposing && (components != null))
  20.             {
  21.                 components.Dispose();
  22.             }
  23.             base.Dispose(disposing);
  24.         }
  25.  
  26.         private void InitializeComponent()
  27.         {
  28.             this.components = new System.ComponentModel.Container();
  29.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  30.             this.SuspendLayout();
  31.             //
  32.             // timer1
  33.             //
  34.             this.timer1.Enabled = true;
  35.             this.timer1.Interval = 5000;
  36.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  37.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  38.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  39.             this.ClientSize = new System.Drawing.Size(284, 262);
  40.             this.Name = "Form1";
  41.             this.Text = "Form1";
  42.             this.ResumeLayout(false);
  43.  
  44.         }
  45.  
  46.         public Form1()
  47.         {
  48.             InitializeComponent();
  49.             visualStudio = System.Diagnostics.Process.GetProcessesByName("devenv")[0];
  50.             EnumWindows(WindowReceiver, IntPtr.Zero); //populate windowHandles with top level windows
  51.         }
  52.  
  53.         void EnumerateWindowsRecursivelyImpl(IntPtr hWnd)
  54.         {
  55.             if (hWnd == IntPtr.Zero || !windowHandles.Add(hWnd)) return;
  56.             EnumerateWindowsRecursivelyImpl(GetWindow(hWnd, (uint)GetWindow_Cmd.GW_CHILD));
  57.             EnumerateWindowsRecursivelyImpl(GetWindow(hWnd, (uint)GetWindow_Cmd.GW_HWNDNEXT));
  58.         }
  59.  
  60.         bool WindowReceiver(IntPtr hWnd, IntPtr dontCare)
  61.         {
  62.             uint pid;
  63.             GetWindowThreadProcessId(hWnd, out pid);
  64.             if (pid == (uint)visualStudio.Id)
  65.             {
  66.                 windowHandles.Add(hWnd);
  67.                 EnumerateWindowsRecursivelyImpl(GetWindow(hWnd, (uint)GetWindow_Cmd.GW_CHILD));
  68.             }
  69.             return true;
  70.         }
  71.  
  72.         private void timer1_Tick(object sender, EventArgs e)
  73.         {
  74.             windowHandles.Clear();
  75.             EnumWindows(WindowReceiver, IntPtr.Zero); //populate windowHandles with top level windows
  76.             foreach (var window in windowHandles)
  77.             {
  78.                 var gfx = Graphics.FromHwnd(window);
  79.                 gfx.DrawString("Have fun!", Font, Brushes.Black, 10, 10);
  80.                 gfx.Dispose();
  81.             }
  82.         }
  83.  
  84.         private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  85.  
  86.         [DllImport("user32.dll")]
  87.         [return: MarshalAs(UnmanagedType.Bool)]
  88.         static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
  89.  
  90.         [DllImport("user32.dll", SetLastError = true)]
  91.         static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  92.  
  93.         [DllImport("user32.dll", SetLastError = true)]
  94.         static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
  95.  
  96.         enum GetWindow_Cmd : uint
  97.         {
  98.             GW_HWNDFIRST = 0,
  99.             GW_HWNDLAST = 1,
  100.             GW_HWNDNEXT = 2,
  101.             GW_HWNDPREV = 3,
  102.             GW_OWNER = 4,
  103.             GW_CHILD = 5,
  104.             GW_ENABLEDPOPUP = 6
  105.         }
  106.  
  107.  
  108.     }
  109.  
  110.     static class Program
  111.     {
  112.         /// <summary>
  113.         /// The main entry point for the application.
  114.         /// </summary>
  115.         [STAThread]
  116.         static void Main()
  117.         {
  118.             Application.EnableVisualStyles();
  119.             Application.SetCompatibleTextRenderingDefault(false);
  120.             Application.Run(new Form1());
  121.         }
  122.     }
  123. }
Add Comment
Please, Sign In to add comment