Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 2.36 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C#: Invoke Windows Magnifier
  2. private void ZoomIn()
  3. {
  4.     // Make sure the Magnifier is running, and get its handle
  5.     if (Process.GetProcesses().Where(p => p.ProcessName.ToLower() == "magnify").Count() == 0) {
  6.         Process.Start("magnify.exe");
  7.         System.Threading.Thread.Sleep(500); }
  8.     IntPtr handle = Process.GetProcesses().Where(p => p.ProcessName.ToLower() == "magnify").First().Handle;
  9.  
  10.     // Figure out what size ("mode") it's in
  11.     WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
  12.     placement.length = Marshal.SizeOf(placement);
  13.     GetWindowPlacement(handle, ref placement);
  14.  
  15.     // Move the Magnifier to a predetermined location so we know where to click
  16.     MoveWindow(handle, new Point(0, 0));
  17.  
  18.     // If Magnifier is in small mode, click it to put it in big mode.
  19.     if (placement.rcNormalPosition.Size.Width == 132) {
  20.         SetCursorPos(15, 15);
  21.         mouse_event((int)HardwareEvents.MouseLeftDown, 15, 15, 0, 0);
  22.         mouse_event((int)HardwareEvents.MouseLeftUp, 15, 15, 0, 0); }
  23.  
  24.     // Click the zoom in button.  Yeah, I know, hackish. Isn't Win32 awesome?
  25.     SetCursorPos(25, 25);
  26.     mouse_event((int)HardwareEvents.MouseLeftDown, 25, 25, 0, 0);
  27.     mouse_event((int)HardwareEvents.MouseLeftUp, 25, 25, 0, 0);
  28. }
  29. private void MoveWindow(IntPtr handle, Point point)
  30. {
  31.     // TODO: Figure out how to look up the target window's size
  32.     SetWindowPos(handle, new IntPtr((int)SpecialWindowHandles.HWND_TOP), (int)point.X, (int)point.Y,  500, 500, SetWindowPosFlags.ShowWindow);
  33. }
  34.  
  35. private struct WINDOWPLACEMENT {
  36.     public int length;
  37.     public int flags;
  38.     public int showCmd;
  39.     public System.Drawing.Point ptMinPosition;
  40.     public System.Drawing.Point ptMaxPosition;
  41.     public System.Drawing.Rectangle rcNormalPosition; }
  42.  
  43. [DllImport("user32.dll")]
  44. static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndl);
  45.  
  46. [DllImport("user32.dll")]
  47. static extern bool SetCursorPos(int X, int Y);
  48.  
  49.  
  50. public static class HardwareEvents
  51. {
  52.     public static int MouseMove = 0x0001;
  53.     public static int MouseLeftDown = 0x0002, MouseLeftUp = 0x0004;
  54.     public static int MouseRightDown = 0x0008, MouseRightUp = 0x0010; // best guess on the 0010
  55.     public static int MiddleMouseDown = 0x20, MiddleMouseUp = 0x40;
  56.     public static int MouseWheel = 0x800;
  57. }
  58.        
  59. keybd_event(0x5B, 0x45, 0, new UIntPtr(0));
  60.         keybd_event(0xBB, 0x45, 1, new UIntPtr(0));