public static Bitmap CaptureWindow(IntPtr hWnd, CaptureMethod Method) { if (hWnd == IntPtr.Zero) { return(null); } int style = ExternalAPI.GetWindowLong(hWnd, -16); //int Exstyle = ExternalAPI.GetWindowLong(hWnd, -20); // Ignore Popup windows if ((style & 0x80000000) != 0) { return(null); } var winrect = new ExternalAPI.Rect(); ExternalAPI.GetWindowRect(hWnd, ref winrect); var clientrec = new ExternalAPI.Rect(); ExternalAPI.GetClientRect(hWnd, ref clientrec); int width = clientrec.right - clientrec.left; int height = clientrec.bottom - clientrec.top; if (width <= 0 || height <= 0) { return(null); } var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(bmp)) { if (Method == CaptureMethod.PaintWindow) { IntPtr Handle = graphics.GetHdc(); ExternalAPI.PrintWindow(hWnd, Handle, 1); graphics.ReleaseHdc(Handle); } else if (Method == CaptureMethod.DesktopCapture) { var tempPoint = new Point(); tempPoint.x = clientrec.left; tempPoint.y = clientrec.top; ClientToScreen(hWnd, ref tempPoint); graphics.CopyFromScreen(tempPoint.x, tempPoint.y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy); } } return(bmp); }
private bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam) { int style = ExternalAPI.GetWindowLong(hWnd, -16); int Exstyle = ExternalAPI.GetWindowLong(hWnd, -20); if (!ExternalAPI.IsWindowVisible(hWnd)) { return(true); } // Ignore Popup windows if ((style & 0x80000000) != 0) { return(true); } uint ProcessID; StringBuilder TempString = new StringBuilder(); ExternalAPI.GetWindowThreadProcessId(hWnd, out ProcessID); IntPtr ProcessHandle = ExternalAPI.OpenProcess(0x0400, false, ProcessID); TempString.EnsureCapacity(1024); ExternalAPI.GetModuleFileNameEx(ProcessHandle, IntPtr.Zero, TempString, TempString.Capacity); if (TempString.ToString() == System.Reflection.Assembly.GetExecutingAssembly().Location) { return(true); } WindowData Entry; Entry.Handle = hWnd; Entry.Name = System.IO.Path.GetFileNameWithoutExtension(TempString.ToString()); ExternalAPI.CloseHandle(ProcessHandle); int size = ExternalAPI.GetWindowTextLength(hWnd); if (size != 0) { TempString.EnsureCapacity(size + 1); ExternalAPI.GetWindowText(hWnd, TempString, TempString.Capacity); Entry.Title = TempString.ToString(); } else { Entry.Title = ""; } WindowDropdown.Items.Add(Entry); return(true); }
public bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam) { int style = ExternalAPI.GetWindowLong(hWnd, -16); int Exstyle = ExternalAPI.GetWindowLong(hWnd, -20); if (!ExternalAPI.IsWindowVisible(hWnd)) { return(true); } // Ignore Popup windows if ((style & 0x80000000) != 0) { return(true); } uint ProcessID; StringBuilder TempString = new StringBuilder(); ExternalAPI.GetWindowThreadProcessId(hWnd, out ProcessID); IntPtr ProcessHandle = ExternalAPI.OpenProcess(0x0400, false, ProcessID); TempString.EnsureCapacity(1024); ExternalAPI.GetModuleFileNameEx(ProcessHandle, IntPtr.Zero, TempString, TempString.Capacity); ExternalAPI.CloseHandle(ProcessHandle); string ExeName = System.IO.Path.GetFileNameWithoutExtension(TempString.ToString()); if (Name.Equals(ExeName, StringComparison.InvariantCultureIgnoreCase)) { if (MatchTitle) { int size = ExternalAPI.GetWindowTextLength(hWnd); if (size != 0) { TempString.EnsureCapacity(size + 1); ExternalAPI.GetWindowText(hWnd, TempString, TempString.Capacity); if (TempString.ToString() == Title) { Handle = hWnd; return(false); } } } else { Handle = hWnd; return(false); } } return(true); }