03/07/12 20:21
-MG-
grazie, evidentemente era proprio la parola winform che mi avrebbe aperto tute le porte.
Comunque cercavo una guida, data una risposta così non è che abbia capito molto, comunque cercherò di trovare una spiegazione da solo come è giusto che sia, grazie mille ancora.
Comunque cercavo una guida, data una risposta così non è che abbia capito molto, comunque cercherò di trovare una spiegazione da solo come è giusto che sia, grazie mille ancora.
Ultima modifica effettuata da -MG- 03/07/12 20:37
aaa
06/07/12 14:24
pierotofy
Questo potrebbe interessarti... l'ho scritto un po' di tempo fa:
ScreenshotTaker.cs
Window.cs
ScreenshotTaker.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Imaging; using System.Diagnostics; using System.Runtime.InteropServices; namespace PokerMuck { public class ScreenshotTaker { /* PrintWindow: uses the PrintWindow function to take the screenshot. This is the best way to take a screenshot * as other windows around it will not cause overlapping, but it's not supported by applications that use OpenGL or * other libraries to do the canvas drawing * PrintScreen: takes a normal screenshot (like you would by using the print screen key) and cuts it out using the size of the window */ public enum Mode { PrintWindow, PrintScreen }; [DllImport("User32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); [DllImport("user32", EntryPoint = "SendMessageA")] private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); const int WM_PRINT = 0x0317; const uint WM_PRINTCLIENTONLY = 1; const int WM_SETREDRAW = 0xB; public ScreenshotTaker() { } public Bitmap Take(){ Rectangle bounds = Screen.GetBounds(Point.Empty); return Take(bounds); } /* @param size: if different than window size, resizes the window before taking the screenshot * note that this is different than resizing a bitmap, we are literally changing the window size */ public Bitmap Take(Window window, bool clientOnly, Size size, Mode mode) { // We cannot use this method if the window is minimized if (window.Minimized) return null; Size originalWindowSize = window.Size; bool needResize = !originalWindowSize.Equals(size); try { if (needResize) { // If we are taking the client only, we don't need the extra repaint window.Resize(size, clientOnly ? false : true); } Rectangle screenshotRect = clientOnly ? window.ClientRectangle : window.Rectangle; Bitmap result = new Bitmap(screenshotRect.Width, screenshotRect.Height, PixelFormat.Format24bppRgb); if (mode == Mode.PrintWindow) { GetScreenshotUsingPrintWindow(window, clientOnly, result); } else if (mode == Mode.PrintScreen) { GetScreenshotUsingPrintScreen(window, clientOnly, result); } // Restore original dimension if (needResize) { window.Resize(originalWindowSize, true); // OK, repaint now! } return result; } catch (Exception e) { Trace.WriteLine("Failed to take screenshot of " + window.Title + ": " + e.Message); return null; } } private void GetScreenshotUsingPrintWindow(Window window, bool clientOnly, Bitmap buffer){ Graphics g = Graphics.FromImage(buffer); IntPtr hdc = g.GetHdc(); uint nFlags = (clientOnly ? WM_PRINTCLIENTONLY : 0); PrintWindow(window.Handle, hdc, nFlags); g.ReleaseHdc(hdc); } private void GetScreenshotUsingPrintScreen(Window window, bool clientOnly, Bitmap buffer) { Rectangle bounds = clientOnly ? window.ClientRectangle : window.Rectangle; using (Graphics g = Graphics.FromImage(buffer)) { g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size); } } public Bitmap Take(Window window, bool clientOnly, Mode mode = Mode.PrintScreen) { Rectangle rect = window.Rectangle; Size winSize = new Size(rect.Width, rect.Height); return Take(window, clientOnly, winSize, mode); } public Bitmap Take(Rectangle bounds) { Bitmap result = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(result)) { g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size); } return result; } public static Bitmap Slice(Bitmap screenshot, Rectangle bounds) { if (screenshot == null) return null; Bitmap result = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(result)) { g.DrawImage(screenshot, 0, 0, bounds, GraphicsUnit.Pixel); } return result; } } }
Window.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Drawing; using System.Diagnostics; namespace PokerMuck { /* This class helps us keep track of information about a specific window * Plus it provides some nice static methods for Win32 windows handling */ public class Window { /* Get the text of a window */ [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); /* Additionally we'll need this one to find the X-Y coordinate of the window */ [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ClientToScreen(IntPtr hWnd, out POINT lpPoint); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); /* We need also to define a RECT structure */ [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } /* And POINT */ [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; } /* This one will help us detect when a window gets closed */ [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); private IntPtr handle; public IntPtr Handle { get { return (IntPtr)handle; } } // Keep track of the latest valid window title private String latestValidWindowTitle = String.Empty; /* Returns the most current, valid window title * An empty title is not a valid window title */ public String Title { get { String newTitle = GetWindowTitleFromHandle(handle); if (newTitle != String.Empty) latestValidWindowTitle = newTitle; return latestValidWindowTitle; } } public Size Size { get{ Rectangle rect = this.Rectangle; return new Size(rect.Width, rect.Height); } } public Rectangle Rectangle { get { return GetWindowRectFromHandle(handle); } } /* Returns the rectangle in terms of absolute screen coordinates, not relative to the container */ public Rectangle ClientRectangle{ get { return GetClientRectFromHandle(handle); } } public bool HasBeenMinimized { get; set; } public bool Minimized { get { // On MS Windows, when a window is resized it is moved to -32000 both on X and Y coordinate // Is there a better way to code this? bool ret = (Rectangle.X == -32000 && Rectangle.Y == -32000); // Keep track of the ever been minimized variable if (ret) HasBeenMinimized = true; return ret; } } /* User defined handle */ public Window(IntPtr handle) { this.handle = handle; Initialize(); } /* Find the handle given the window title */ public Window(String windowTitle) { this.handle = Window.FindWindowByCaption(IntPtr.Zero, windowTitle); Initialize(); } private void Initialize() { this.HasBeenMinimized = false; } public bool Exists() { return Title != String.Empty && Exists(Title); } public bool Resize(Size newSize, bool repaint = true) { return Window.ResizeWindow(handle, newSize, repaint); } // Static members public static bool Exists(String windowTitle) { return FindWindowByCaption(IntPtr.Zero, windowTitle) != IntPtr.Zero; } public static String GetWindowTitleFromHandle(IntPtr handle) { const int maxChars = 256; StringBuilder buffer = new StringBuilder(maxChars); if (GetWindowText(handle, buffer, maxChars) > 0) { return buffer.ToString(); } else { return ""; } } public static bool ResizeWindow(IntPtr handle, Size newSize, bool repaint) { Rectangle windowRect = GetWindowRectFromHandle(handle); return MoveWindow(handle, windowRect.X, windowRect.Y, newSize.Width, newSize.Height, repaint); } public static Rectangle GetWindowRectFromWindowTitle(String windowTitle) { IntPtr handle = FindWindowByCaption(IntPtr.Zero, windowTitle); return GetWindowRectFromHandle(handle); } public static Rectangle GetClientRectFromHandle(IntPtr handle) { RECT rct; // C++ style Rectangle clientRect = new Rectangle(); // C# style if (GetClientRect(handle, out rct)) { // Translate to screen cordinates POINT topleft; topleft.x = rct.Left; topleft.y = rct.Top; POINT bottomright; bottomright.x = rct.Right; bottomright.y = rct.Bottom; ClientToScreen(handle, out topleft); ClientToScreen(handle, out bottomright); clientRect.X = topleft.x; clientRect.Y = topleft.y; clientRect.Width = bottomright.x - topleft.x; clientRect.Height = bottomright.y - topleft.y; } else { Trace.WriteLine("I couldn't figure out client position and size of window handle " + handle.ToString()); } return clientRect; } public static Rectangle GetWindowRectFromHandle(IntPtr handle) { // Ok, let's figure out it's position RECT rct; // C++ style Rectangle windowRect = new Rectangle(); // C# style if (GetWindowRect(handle, out rct)) { windowRect.X = rct.Left; windowRect.Y = rct.Top; windowRect.Width = rct.Right - rct.Left; windowRect.Height = rct.Bottom - rct.Top; } else { Trace.WriteLine("I couldn't figure out position and size of window handle " + handle.ToString()); } return windowRect; } } }
Il mio blog: piero.dev