using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; internal static class OtzarFast { [StructLayout(LayoutKind.Sequential)] struct POINT { public int X,Y; } [StructLayout(LayoutKind.Sequential)] struct RECT { public int Left,Top,Right,Bottom; } [StructLayout(LayoutKind.Sequential)] struct GUI { public uint Size,Flags; public IntPtr Active,Focus,Capture,Menu,Move,Caret; public RECT CaretRect; } [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr h); [DllImport("user32.dll")] static extern bool IsIconic(IntPtr h); [DllImport("user32.dll")] static extern bool ShowWindowAsync(IntPtr h,int mode); [DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h,out RECT r); [DllImport("user32.dll")] static extern bool ClientToScreen(IntPtr h,ref POINT p); [DllImport("user32.dll")] static extern bool GetCursorPos(out POINT p); [DllImport("user32.dll")] static extern bool SetCursorPos(int x,int y); [DllImport("user32.dll")] static extern IntPtr WindowFromPoint(POINT p); [DllImport("user32.dll")] static extern IntPtr GetAncestor(IntPtr h,uint flags); [DllImport("user32.dll")] static extern void mouse_event(uint flags,uint x,uint y,uint data,UIntPtr extra); [DllImport("user32.dll",SetLastError=true)] static extern bool GetGUIThreadInfo(uint thread,ref GUI info); [DllImport("user32.dll",SetLastError=true)] static extern bool PostMessage(IntPtr h,uint msg,UIntPtr key,IntPtr data); static void CheckFocus(IntPtr root) { if(GetForegroundWindow()!=root) throw new Exception("המיקוד עבר לחלון אחר. נסה שוב בלי לעבור לחלון אחר."); } static void F1(IntPtr root) { CheckFocus(root); GUI info=new GUI(); info.Size=(uint)Marshal.SizeOf(typeof(GUI)); if(!GetGUIThreadInfo(0,ref info) || info.Focus==IntPtr.Zero || GetAncestor(info.Focus,2)!=root) throw new Exception("לא ניתן לזהות את הרכיב הפעיל באוצר החכמה."); if(!PostMessage(info.Focus,0x100,new UIntPtr(0x70u),new IntPtr(0x003B0001)) || !PostMessage(info.Focus,0x101,new UIntPtr(0x70u),new IntPtr(unchecked((int)0xC03B0001)))) throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } [STAThread] static void Main() { try { string book=Path.GetFileNameWithoutExtension(Application.ExecutablePath); if(!Regex.IsMatch(book,@"^[0-9]+$")) throw new Exception("שם הקובץ חייב להיות מספר הספר, למשל 198605.exe"); IntPtr root=IntPtr.Zero; foreach(Process p in Process.GetProcesses()) { using(p) { try { IntPtr h=p.MainWindowHandle; string title=Regex.Replace(p.MainWindowTitle,@"\p{Cf}","").Trim(); if(h==IntPtr.Zero || !Regex.IsMatch(title,@"^otzar$|אוצר\s*החכמה|Otzar\s*Ha(?:ch|h)ochma",RegexOptions.IgnoreCase)) continue; if(root!=IntPtr.Zero) throw new InvalidOperationException("נמצאו כמה חלונות של אוצר החכמה. השאר רק את חלון התוכנה הרצוי פתוח."); root=h; } catch(System.ComponentModel.Win32Exception) {} catch(InvalidOperationException e) { if(e.Message.StartsWith("נמצאו")) throw; } } } if(root==IntPtr.Zero) throw new Exception("לא נמצא חלון אוצר החכמה. פתח את התוכנה ונסה שוב."); if(IsIconic(root)) { ShowWindowAsync(root,9); for(int i=0;i<50 && IsIconic(root);i++) Thread.Sleep(10); } for(int i=0;i<30 && GetForegroundWindow()!=root;i++) { SetForegroundWindow(root); Thread.Sleep(10); } CheckFocus(root); RECT r; if(!GetClientRect(root,out r) || r.Right-r.Left<100 || r.Bottom-r.Top<100) throw new Exception("לא ניתן לקרוא את שטח החלון. הגדל את אוצר החכמה ונסה שוב."); POINT point=new POINT {X=(r.Right-r.Left)/2,Y=(int)((r.Bottom-r.Top)*0.55)}; if(!ClientToScreen(root,ref point) || GetAncestor(WindowFromPoint(point),2)!=root) throw new Exception("שטח הלחיצה מוסתר. הצג את חלון אוצר החכמה ונסה שוב."); POINT old; bool restore=GetCursorPos(out old); try { CheckFocus(root); if(!SetCursorPos(point.X,point.Y)) throw new Exception("לא ניתן למקד את שטח התוכנה."); mouse_event(2,0,0,0,UIntPtr.Zero); mouse_event(4,0,0,0,UIntPtr.Zero); } finally { if(restore) SetCursorPos(old.X,old.Y); } Thread.Sleep(30); F1(root); Thread.Sleep(150); CheckFocus(root); SendKeys.SendWait(book); CheckFocus(root); SendKeys.SendWait("{ENTER}"); } catch(Exception e) { MessageBox.Show(e.Message,"פתיחת ספר באוצר החכמה",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } }