Merhabalar,
c# kanadında , tarayıcıda açık olan whatsapp’ı simge durumundan kaldırmam veya eğer simge durumunda değilse ekranda açık olan pencerelerin tümünün önüne getirmem gerekiyordu, kendime basit bir sınıf yazarak hallettim sizlere de yararı olabileceğini düşünerek buraya ekliyorum.
1 2 3 4 5 |
new MyWindowsAPI().doWindow("WhatsApp - Google Chrome"); |
doWindow metodunu kullanırken Pencerenenin tam adını vermeniz gerekir “WhatsApp – Google Chrome” gibi.
doMaxWindowLike ve doMinWindowLike metodarında ise Pencere içerisinde geçen kelimeyi verebilirsiniz sadece “WhatsApp” yazmanız yeterli , ilk bulduğuna pencereyi yakalayacaktır.
Saygılar.
1 2 3 4 5 6 7 |
new MyWindowsAPI().doMaxWindowLike("WhatsApp"); new MyWindowsAPI().doMinWindowLike("WhatsApp"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using System.Threading.Tasks; namespace PWA { class MyWindowsAPI { private string iCaption; IntPtr xHandle = IntPtr.Zero; private readonly int SW_RESTORE = 9; private readonly int SW_MINIMIZE = 6; private readonly int SW_MAXIMIZE = 3; [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")] public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId); [DllImport("user32.dll", EntryPoint = "AttachThreadInput")] public static extern bool AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach); [DllImport("Kernel32.dll", EntryPoint = "GetCurrentThreadId")] public static extern IntPtr GetCurrentThreadId(); [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", EntryPoint = "IsIconic")] public static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [ResourceExposure(ResourceScope.Process)] public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); internal delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "EnumWindowsProc")] public static extern bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Unicode, BestFitMapping = true)] [ResourceExposure(ResourceScope.None)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] bool MyEnumWindowsCallback(IntPtr handle, IntPtr extraParameter) { StringBuilder stringBuilder = new StringBuilder(256); GetWindowText(handle, stringBuilder, stringBuilder.Capacity); string ss = stringBuilder.ToString(); if (!string.IsNullOrEmpty(ss)) { if (ss.ToLower().Contains(iCaption.ToLower())) { xHandle = handle; return false; } } return true; } [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); public void doMaxWindowLike(string caption) { if (string.IsNullOrEmpty(caption)) throw new Exception("Caption cannot be null or empty..!"); iCaption = caption; doWindowLike(); } public void doMinWindowLike(string caption) { if (string.IsNullOrEmpty(caption)) throw new Exception("Caption cannot be null or empty..!"); iCaption = caption; doWindowLike(true); } public bool IsFind(string Caption) { IntPtr AHandle = FindWindow(null, Caption); return AHandle.ToInt32() != 0; } // private Methods // private void maximize(IntPtr hWnd) { ShowWindow(xHandle, SW_RESTORE); ShowWindow(xHandle, SW_MAXIMIZE); } private void minimize(IntPtr hWnd) { ShowWindow(xHandle, SW_RESTORE); ShowWindow(xHandle, SW_MAXIMIZE); } // Caption İçeren private void doWindowLike(bool minimized = false) { xHandle = IntPtr.Zero; EnumWindows(MyEnumWindowsCallback, IntPtr.Zero); if (xHandle != IntPtr.Zero) { var AThreadID = GetWindowThreadProcessId(xHandle, IntPtr.Zero); AttachThreadInput(GetCurrentThreadId(), AThreadID, true); try { if (minimized) { minimize(xHandle); } else { if (IsIconic(xHandle)) maximize(xHandle); SetForegroundWindow(xHandle); } } finally { AttachThreadInput(GetCurrentThreadId(), AThreadID, false); } } } // Tam eşleşme public void doWindow(string Caption , bool minimized = false) { IntPtr AHandle = FindWindow(null, Caption); if (AHandle.ToInt32() != 0) { var AThreadID = GetWindowThreadProcessId(AHandle, IntPtr.Zero); AttachThreadInput(GetCurrentThreadId(), AThreadID, true); try { if (minimized) { minimize(xHandle); } else { if (IsIconic(xHandle)) maximize(xHandle); SetForegroundWindow(xHandle); } } finally { AttachThreadInput(GetCurrentThreadId(), AThreadID, false); } } } } } |
Yürüyen Windows Api Kütüphanesi Tuğrul Abim’e teşekkürler 🙂