123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using PTMedicalInsurance.Variables;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMedicalInsurance.Common
- {
- class WinApi
- {
- private bool IsWindowExist(IntPtr handle)
- {
- return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
- public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern bool IsWindowVisible(HandleRef hWnd);
-
-
-
-
-
- private IntPtr GetWindowHandle(int processId)
- {
- var windowHandle = IntPtr.Zero;
- EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
- EnumWindows(windowsCallback, IntPtr.Zero);
-
- GC.KeepAlive(windowsCallback);
- bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
- {
- int num;
- GetWindowThreadProcessId(new HandleRef(this, handle), out num);
- if (num == processId && IsWindowExist(handle))
- {
- windowHandle = handle;
- return true;
- }
- return false;
- }
- return windowHandle;
- }
- public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
- public static IntPtr GetWindowsHandle(string WindowName)
- {
- IntPtr hwnd = IntPtr.Zero;
- Process[] procs = Process.GetProcessesByName(WindowName);
- if (procs.Length != 0)
- {
- hwnd = procs[0].MainWindowHandle;
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return hwnd;
- }
- private static Hashtable processWnd = null;
- public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
-
-
-
-
-
-
-
- [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
- public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
- [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
- public static extern IntPtr GetParent(IntPtr hWnd);
- [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
- public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
- [DllImport("user32.dll", EntryPoint = "IsWindow")]
- public static extern bool IsWindow(IntPtr hWnd);
- [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
- public static extern void SetLastError(uint dwErrCode);
- public static IntPtr GetCurrentWindowHandle()
- {
- IntPtr ptrWnd = IntPtr.Zero;
- uint uiPid = (uint)Process.GetCurrentProcess().Id;
- object objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))
- {
- return ptrWnd;
- }
- else
- {
- ptrWnd = IntPtr.Zero;
- }
- }
- bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
-
- if (!bResult && Marshal.GetLastWin32Error() == 0)
- {
- objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- }
- }
- return ptrWnd;
- }
- private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
- {
- uint uiPid = 0;
- if (GetParent(hwnd) == IntPtr.Zero)
- {
- GetWindowThreadProcessId(hwnd, ref uiPid);
- if (uiPid == lParam)
- {
- processWnd[uiPid] = hwnd;
- SetLastError(0);
- return false;
- }
- }
- return true;
- }
- public static IntPtr GetCurrentWindowHandle(uint proid)
- {
- IntPtr ptrWnd = IntPtr.Zero;
- uint uiPid = proid;
- object objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))
- {
- return ptrWnd;
- }
- else
- {
- ptrWnd = IntPtr.Zero;
- }
- }
- bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
-
- if (!bResult && Marshal.GetLastWin32Error() == 0)
- {
- objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- }
- }
- return ptrWnd;
- }
- }
- }
|