Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ПоискОкна
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Diagnostics;
  12.     using System.Runtime.InteropServices;
  13.     using System.Text;
  14.  
  15.     public static class НайтиОкно
  16.  
  17.     {
  18.         private const uint WM_GETTEXT = 0x000D;
  19.  
  20.         [DllImport("user32.dll", SetLastError = true)]
  21.  
  22.         private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
  23.  
  24.         private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
  25.         {
  26.             var handles = new List<IntPtr>();
  27.  
  28.             foreach (ProcessThread thread in process.Threads)
  29.  
  30.             {
  31.                 EnumThreadWindows(thread.Id, (hWnd, lParam) =>
  32.                 {
  33.                     handles.Add(hWnd);
  34.                     return true;
  35.                 }, IntPtr.Zero);
  36.             }
  37.  
  38.             return handles;
  39.         }
  40.  
  41.         [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  42.  
  43.         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
  44.  
  45.         [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  46.         private static extern int GetClassName(
  47.  
  48.                 IntPtr hWnd,
  49.                 StringBuilder lpClassName,
  50.                 int nMaxCount
  51.                 );
  52.         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  53.  
  54.         public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  55.  
  56.         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  57.  
  58.         public static extern bool IsWindowVisible(IntPtr hWnd);
  59.  
  60.  
  61.         public static void ПоказатьОкно(string НазваниеОкна)
  62.         {
  63.  
  64.            string Заголовок="1С:Предприятие";
  65.  
  66.             foreach (var processRunning in System.Diagnostics.Process.GetProcesses())
  67.  
  68.             {
  69.  
  70.                 if (processRunning.ProcessName.StartsWith("1cv7"))
  71.                     foreach (IntPtr handle in EnumerateProcessWindowHandles(processRunning))
  72.             {
  73.                 var wndCaption = new StringBuilder(1000);
  74.  
  75.                 SendMessage(handle, WM_GETTEXT, wndCaption.Capacity, wndCaption);
  76.                 if (wndCaption.Length >= Заголовок.Length)
  77.                         if (wndCaption.ToString(0,Заголовок.Length)==Заголовок)
  78.                             if (!IsWindowVisible(handle))
  79.                              ShowWindow(handle, 5);
  80.  
  81.         //       var wndClass = new StringBuilder(255);
  82.  
  83.         //       GetClassName(handle, wndClass, wndClass.Capacity);
  84.  
  85.  
  86.             //    Console.WriteLine("[{0:X8}] '{1}' '{2}'", handle, wndCaption, wndClass);
  87.  
  88.             }
  89.           // Console.ReadLine();
  90.  
  91.             }
  92.  
  93.         }
  94.  
  95.         #region Nested type: EnumThreadDelegate
  96.  
  97.         private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
  98.  
  99.         #endregion
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement