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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace winamax_winapi
{
class Program
{
#region WINApi function
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
public delegate bool CallBackPtr(int hwnd, int lParam);
private CallBackPtr callBackPtr;
public class EnumReport
{
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBackPtr callPtr, int lPar);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
}
#endregion
static void Main(string[] args)
{
String patternFenetre1 = "([0-9]*)#";
Regex rgx = new Regex(patternFenetre1, RegexOptions.IgnoreCase);
List<String> AllId = new List<String>();
while (true)
{
//On récupère la fenetre en front
IntPtr windowForeground = GetForegroundWindow();
//On récupère le titre de cette fenetre
StringBuilder windowTitle = new StringBuilder(256);
GetWindowText(new HandleRef(null, windowForeground), windowTitle, windowTitle.Capacity);
// On regarde si le type de fenetre que l'on veut suivre est devant (foreground)
MatchCollection matches = rgx.Matches(windowTitle.ToString());
if (matches.Count > 0)
{
String identifiant = null;
// On selection l'identifiant de la fenetre 1 qui va nous permettre de trouver la fenetre 2
identifiant = windowTitle.ToString().Substring(windowTitle.ToString().IndexOf("("), windowTitle.ToString().IndexOf(")") - windowTitle.ToString().IndexOf("(") + 1);
//On check si la fenetre 2 correpondante est ouverte
IntPtr fenetre2Handle = WndSearcher.SearchForWindow("ApolloRuntimeContentWindow", identifiant);
if (fenetre2Handle != new IntPtr(0))
{
// On garde en mémoire le tournamentID
if (!AllId.Contains(identifiant))
AllId.Add(identifiant);
//On passe notre fenetre 2 devat
Boolean resultSetForeground = SetForegroundWindow(fenetre2Handle);
//Et On la place par dessus la fenetre 1
WINDOWPLACEMENT info = new WINDOWPLACEMENT();
GetWindowPlacement(windowForeground, out info);
info.NormalPosition.top -= 20;
SetWindowPlacement(fenetre2Handle, ref info);
}
}
//Si notre fenetre 1 n est pas devant
else
{
if (AllId.Count > 0)
{
//On boucle sur les ID en mémoire donc ouvert
foreach (String currentID in AllId)
{
IntPtr currentHandle = WndSearcher.SearchForWindow("ApolloRuntimeContentWindow", currentID);
#region Si ma fenetre 1 n'existe plus on ferme la fenetre 2 associée
if (currentHandle == new IntPtr(0))
{
AllId.Remove(currentID);
IntPtr currentFenetre2Handle = WndSearcher.SearchForWindow("ApolloRuntimeContentWindow", currentID);
SendMessage(int.Parse(currentFenetre2Handle.ToString()), WM_SYSCOMMAND, SC_CLOSE, 0);
break;
}
#endregion
#region Si elle existe mais pas en premier plan on la replace correctement par dessus la fenetre 1
else
{
WINDOWPLACEMENT info = new WINDOWPLACEMENT();
GetWindowPlacement(currentHandle, out info);
IntPtr currentFenetre2Handle = WndSearcher.SearchForWindow("ApolloRuntimeContentWindow", currentID);
SetWindowPlacement(currentFenetre2Handle, ref info);
}
#endregion
}
}
}
//Stop
System.Threading.Thread.Sleep(1300);
}
}
}
#region Struct
struct Point
{
public int x;
public int y;
public override string ToString()
{
return (String.Format("({0}, {1})",
x, y));
}
}
[Flags()]
enum SetWindowPosFlags : uint
{
AsynchronousWindowPosition = 0x4000,
DeferErase = 0x2000,
DrawFrame = 0x0020,
FrameChanged = 0x0020,
HideWindow = 0x0080,
DoNotActivate = 0x0010,
DoNotCopyBits = 0x0100,
IgnoreMove = 0x0002,
DoNotChangeOwnerZOrder = 0x0200,
DoNotRedraw = 0x0008,
DoNotReposition = 0x0200,
DoNotSendChangingEvent = 0x0400,
IgnoreResize = 0x0001,
IgnoreZOrder = 0x0004,
ShowWindow = 0x0040,
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPLACEMENT
{
public int Length;
public int Flags;
public ShowWindowCommands ShowCmd;
public Point MinPosition;
public Point MaxPosition;
public Rect NormalPosition;
public static WINDOWPLACEMENT Default
{
get
{
WINDOWPLACEMENT result = new WINDOWPLACEMENT();
result.Length = Marshal.SizeOf(result);
return result;
}
}
}
struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return (String.Format("({0}, {1})\n ({2}, {3})",
left, top, right, bottom));
}
}
struct WindowPlacement
{
public uint length;
public uint flags;
public uint showCmd;
public Point minPosition;
public Point maxPosition;
public Rect normalPosition;
public override string ToString()
{
return (String.Format("min, max, normal:\n{0}\n{1}\n{2}", minPosition, maxPosition, normalPosition));
}
}
enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
ShowMinimized = 2,
Maximize = 3, // is this the right value?
ShowMaximized = 3,
ShowNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActive = 7,
ShowNA = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimize = 11
}
struct WINDOWINFO
{
public uint cbSize;
public Rect rcWindow;
public Rect rcClient;
public uint dwStyle;
public uint dwExStyle;
public uint dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public ushort atomWindowType;
public ushort wCreatorVersion;
public WINDOWINFO(Boolean? filler)
: this() // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
{
cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
}
}
[StructLayout(LayoutKind.Sequential)]
struct TITLEBARINFO
{
public const int CCHILDREN_TITLEBAR = 5;
public uint cbSize;
public Rect rcTitleBar;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]
public uint[] rgstate;
}
#endregion
#region WndSearcher
public class WndSearcher
{
public static IntPtr SearchForWindow(string wndclass, string title)
{
SearchData sd = new SearchData { Wndclass = wndclass, Title = title };
EnumWindows(new EnumWindowsProc(EnumProc), ref sd);
Console.WriteLine("__________________________________________________________________");
return sd.hWnd;
}
public static bool EnumProc(IntPtr hWnd, ref SearchData data)
{
// Check classname and title
// This is different from FindWindow() in that the code below allows partial matches
StringBuilder sb = new StringBuilder(1024);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString().StartsWith(data.Wndclass))
{
sb = new StringBuilder(1024);
GetWindowText(hWnd, sb, sb.Capacity);
String currentResult = sb + "";
Console.WriteLine(currentResult);
if (currentResult.Contains(data.Title))
{
data.hWnd = hWnd;
return false;
}
}
return true;
}
public class SearchData
{
public string Wndclass;
public string Title;
public IntPtr hWnd;
}
private delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref SearchData data);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
}
#endregion
} |
Partager