using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using sharpcapture.Properties; using sharphook; namespace sharpcapture { public partial class MainWindow : Form { private enum Action { None, CapturingSelection, CapturingWindow, CapturingDesktop } private Action action; private Rectangle? area; Form zoomForm; CapturingWindow capturingWindow; SharpHook hook; [DllImport("user32.dll")] static extern int GetForegroundWindow(); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); [DllImport("User32.dll")] public static extern IntPtr GetDC(IntPtr hwnd); [DllImport("User32.dll")] public static extern void ReleaseDC(IntPtr dc); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } public MainWindow() { InitializeComponent(); toolStrip.Visible = true; SetClientSizeCore(Width, toolStrip.Height); } void hook_Click(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ManageAction(e.Location); } } void ManageAction(Point point) { switch (action) { case Action.CapturingSelection: if (area == null) { area = new Rectangle(point, new Size()); } else { CaptureScreenshot(area.Value); area = null; btnSelection.Checked = false; action = Action.None; if (capturingWindow != null) capturingWindow.Close(); capturingWindow = null; if (hook != null) hook.Dispose(); hook = null; } break; case Action.CapturingWindow: int handle = GetForegroundWindow(); RECT rect; if (GetWindowRect(new HandleRef(this, new IntPtr(handle)), out rect)) { int width = rect.Right - rect.Left; int height = rect.Bottom - rect.Top; area = new Rectangle(rect.Left, rect.Top, width, height); Debug.WriteLine("Width: " + area.Value.Width + " Height: " + area.Value.Height); capturingWindow.Area = area.Value; } break; } } void hook_DoubleClick(object sender, MouseEventArgs e) { switch (action) { case Action.CapturingWindow: CaptureScreenshot(area.Value); area = null; btnWindow.Checked = false; action = Action.None; if (capturingWindow != null) capturingWindow.Close(); capturingWindow = null; if (hook != null) hook.Dispose(); hook = null; break; } } void hook_MouseMove(object sender, MouseEventArgs e) { if (action == Action.CapturingSelection) { if (area.HasValue) { Point p1 = area.Value.Location; Point p2 = e.Location; area = new Rectangle(p1.X < p2.X ? p1 : p2, new Size(Math.Abs(p2.X - p1.X), Math.Abs(p2.Y - p1.Y))); capturingWindow.Area = area.Value; } } else if (action == Action.CapturingWindow) { } } void hook_KeyPress(object sender, KeyPressEventArgs e) { Debug.WriteLine(e.KeyChar); if (e.KeyChar == 'p') { ManageAction(Control.MousePosition); } } private void CaptureScreenshot(int x, int y, int width, int height) { if (width == 0 || height == 0) return; Hide(); //while (Visible) ; Thread.Sleep(1000); Bitmap capturedBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(capturedBitmap); g.CopyFromScreen(x, y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy); Show(); ScreenshotWindow window = new ScreenshotWindow(capturedBitmap); window.Show(this); } private void CaptureScreenshot(Rectangle rectangle) { CaptureScreenshot(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } private void MainWindow_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { Close(); } else if (e.Control && e.KeyCode == Keys.Z) { if (zoomForm == null) { zoomForm = new Form(); zoomForm.Text = "Zoom"; zoomForm.FormBorderStyle = FormBorderStyle.SizableToolWindow; zoomForm.Width = 100; zoomForm.Height = 100; PictureBox zoomArea = new PictureBox(); zoomArea.Name = "zoomArea"; zoomArea.Dock = DockStyle.Fill; zoomForm.Controls.Add(zoomArea); zoomForm.FormClosed += new FormClosedEventHandler(zoomForm_FormClosed); zoomForm.Show(); } else { zoomForm.Close(); zoomForm = null; } } // Pin/unpin else if (e.Control && e.KeyCode == Keys.P) { TopMost = !TopMost; } } void zoomForm_FormClosed(object sender, FormClosedEventArgs e) { zoomForm = null; } private void RefreshZoomArea(int mouseX, int mouseY) { if (zoomForm != null && zoomForm.Controls["zoomArea"] != null) { Bitmap zoomBmp = new Bitmap(50, 50, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(zoomBmp); g.CopyFromScreen( Location.X + (Width - ClientRectangle.Width) / 2 + mouseX - 25, Location.Y + Height - ClientRectangle.Height - (Width - ClientRectangle.Width) / 2 + mouseY - 25, 0, 0, new Size(50, 50), CopyPixelOperation.SourceCopy); Bitmap bmp2 = new Bitmap(100, 100, PixelFormat.Format32bppArgb); Graphics g2 = Graphics.FromImage(bmp2); g2.InterpolationMode = InterpolationMode.HighQualityBicubic; g2.DrawImage(zoomBmp, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, 50, 50), GraphicsUnit.Pixel); ((PictureBox)zoomForm.Controls["zoomArea"]).Image = (Image)bmp2; } } private void toolStripButton_Click(object sender, EventArgs e) { if (sender == btnSelection) { if (action != Action.CapturingSelection) { action = Action.CapturingSelection; hook = new SharpHook(SharpHook.HookType.Mouse | SharpHook.HookType.Keyboard); hook.MouseMove += new MouseEventHandler(hook_MouseMove); hook.Click += new MouseEventHandler(hook_Click); hook.KeyPress += new KeyPressEventHandler(hook_KeyPress); capturingWindow = new CapturingWindow(); capturingWindow.Show(this); } else { action = Action.None; if (capturingWindow != null) capturingWindow.Close(); capturingWindow = null; if (hook != null) hook.Dispose(); hook = null; } } else if (sender == btnWindow) { if (action != Action.CapturingWindow) { action = Action.CapturingWindow; hook = new SharpHook(SharpHook.HookType.Mouse); hook.Click += new MouseEventHandler(hook_Click); hook.DoubleClick += new MouseEventHandler(hook_DoubleClick); capturingWindow = new CapturingWindow(); capturingWindow.Show(this); } else { action = Action.None; if (capturingWindow != null) capturingWindow.Close(); capturingWindow = null; if (hook != null) hook.Dispose(); hook = null; } } else if (sender == btnDesktop) { CaptureScreenshot(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); } } private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.Location = Location; Settings.Default.Save(); } private void MainWindow_Load(object sender, EventArgs e) { Location = Settings.Default.Location; } } }