Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am want to implement screenshot use mouse to click as start point to click as target point, or get a application window, but my start point must in form...

Is another method to solve this

follow as my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaptureWindow
{
    public partial class Form1 : Form
    {
        public string sSavePath = string.Empty;
        public struct sMouseStatus {
            public bool bDown;
            public bool bUp;
            public Point pUp;
            public Point pDown;
        }
        public sMouseStatus sMouse = new sMouseStatus();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sSavePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Logical Desktop
            tbSavePath.Text = sSavePath;
            InitMouseStatus();
        }

        private void InitMouseStatus()
        {
            sMouse.bDown = false;
            sMouse.bUp = false;
            sMouse.pDown = Point.Empty;
            sMouse.pUp = Point.Empty;
        }

        private void rdoMouse_Click(object sender, EventArgs e)
        {
            InitMouseStatus();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {

        }

        private Bitmap CaptureScreen(Point pSource, Point pTarget, int iWidth, int iHeight)
        {
            //// Size size is how big an area to capture
            //// pointOrigin is the upper left corner of the area to capture
            //int width = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width;
            //int height = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height;
            //Size size = new Size(iWidth, iHeight);
            //Point pointOfOrigin = new Point(pSource.X, pSource.Y);

            //Bitmap bitmap = new Bitmap(size.Width, size.Height);
            //{
            //    using (Graphics graphics = Graphics.FromImage(bitmap))
            //    {
            //        graphics.CopyFromScreen(pointOfOrigin, new Point(0, 0), size);
            //    }
            //    Clipboard.SetImage(bitmap);
            //    return bitmap;
            //}
            //Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            if (iWidth <= 0 || iHeight <= 0)
            {
                return null;
            }

            Bitmap myImage = new Bitmap(iWidth, iHeight);
            Graphics g = Graphics.FromImage(myImage);
            //g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            g.CopyFromScreen(pSource, new Point(0, 0), new Size(iWidth, iHeight), CopyPixelOperation.SourceCopy);
            IntPtr dc1 = g.GetHdc();
            g.ReleaseHdc(dc1);
            Clipboard.SetImage(myImage);
            return myImage;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            sMouse.bDown = true;
            sMouse.pDown = e.Location;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (sMouse.bDown == false)
            {
                sMouse.bDown = false;
                sMouse.bUp = false;
                return;
            }
            sMouse.bUp = true;
            sMouse.pUp = e.Location;

            if (sMouse.bDown && sMouse.bUp)
            {
                int iSizeX = Math.Abs(sMouse.pUp.X - sMouse.pDown.X);
                int iSizeY = Math.Abs(sMouse.pUp.Y - sMouse.pDown.Y);

                CaptureScreen(((Control)sender).PointToScreen(sMouse.pDown), ((Control)sender).PointToScreen(sMouse.pUp), iSizeX, iSizeY);
            }
            else
            {
                sMouse.bDown = false;
                sMouse.bUp = false;
            }

            InitMouseStatus();
        }
    }
}


What I have tried:

I try to use this link method, but I cant


[^]
Posted
Updated 29-Mar-22 15:53pm

1 solution

I find the solution, the Hook method

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaptureWindow
{
    public class Win32Api
    {
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
    }

    public class MouseHook
    {
        private Point point;
        private Point Point
        {
            get { return point; }
            set
            {
                if (point != value)
                {
                    point = value;
                    if (MouseMoveEvent != null)
                    {
                        var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
                        MouseMoveEvent(this, e);
                    }
                }
            }
        }
        private int hHook;
        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_MBUTTONDBLCLK = 0x209;
        public const int WH_MOUSE_LL = 14;
        public Win32Api.HookProc hProc;
        public MouseHook()
        {
            this.Point = new Point();
        }
        public int SetHook()
        {
            hProc = new Win32Api.HookProc(MouseHookProc);
            hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
            return hHook;
        }
        public void UnHook()
        {
            Win32Api.UnhookWindowsHookEx(hHook);
        }
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
            if (nCode < 0)
            {
                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                if (MouseClickEvent != null)
                {
                    MouseButtons button = MouseButtons.None;
                    int clickCount = 0;
                    switch ((Int32)wParam)
                    {
                        case WM_LBUTTONDOWN:
                            button = MouseButtons.Left;
                            clickCount = 1;
                            MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                        case WM_RBUTTONDOWN:
                            button = MouseButtons.Right;
                            clickCount = 1;
                            MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                        case WM_MBUTTONDOWN:
                            button = MouseButtons.Middle;
                            clickCount = 1;
                            MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                        case WM_LBUTTONUP:
                            button = MouseButtons.Left;
                            clickCount = 1;
                            MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                        case WM_RBUTTONUP:
                            button = MouseButtons.Right;
                            clickCount = 1;
                            MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                        case WM_MBUTTONUP:
                            button = MouseButtons.Middle;
                            clickCount = 1;
                            MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                            break;
                    }

                    var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
                    MouseClickEvent(this, e);
                }
                this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
        }

        public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
        public event MouseMoveHandler MouseMoveEvent;

        public delegate void MouseClickHandler(object sender, MouseEventArgs e);
        public event MouseClickHandler MouseClickEvent;

        public delegate void MouseDownHandler(object sender, MouseEventArgs e);
        public event MouseDownHandler MouseDownEvent;

        public delegate void MouseUpHandler(object sender, MouseEventArgs e);
        public event MouseUpHandler MouseUpEvent;


    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900