Here's how to use it:
private void Button1_Click (object sender, EventArgs e) { s.gerscreenformrectangle (); } Private Zgke.copyscreen S; private void Form1_Load (object sender, EventArgs e) { s = new Zgke.copyscreen (); S.getscreenimage+=new Zgke.CopyScreen.GetImage (s_getscreenimage); } void S_getscreenimage (Image p_image) { picturebox1.image = p_image; }
When you press BUTTON1, you can select a rectangle on the screen to
All of the classes
Start by building a new project for the Windows Library class. Here because of the need for global hooks. I don't know why I can't set the global hooks in the WinForm. But in the DLL you can ... Hateful C # .....
Copy the following code into CLASS1.CS.
Using system;using system.collections.generic;using system.text;using system.runtime.interopservices;using System.drawing;using system.windows.forms;namespace zgke{public class API {[DllImport ("User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetDesktopWindow (); [DllImport ("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowsHookEx (int hookid, HookProc Pfnhook, IntPtr hinst, int threadid); Public delegate IntPtr HookProc (int nCode, IntPtr wParam, IntPtr lParam); [DllImport ("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int GetCurrentThread Id (); [DllImport ("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool Unhookwindowshook Ex (IntPtr hhook); public enum Windowshookcodes {wh_msgfilter = ( -1), Wh_journalrecord = 0, Wh_journ Alplayback = 1, Wh_keyboard = 2, Wh_getmessage = 3, Wh_callwndproc = 4, WH_CBT = 5, Wh_sysmsgfilter = 6, Wh_mouse = 7, Wh_hardware = 8, Wh_debug = 9, Wh_shell = ten, Wh_fore Groundidle = one, Wh_callwndprocret = 14, Wh_keyboard_ll = Wh_mouse_ll }///<summary>///A according to the rectangular class//[email protected]///qq:116149//</summary> PU Blic class Copyscreen {//<summary>///Screen size//</summary> private size Scre Ensize {get {return Screen.PrimaryScreen.Bounds.Size;}} <summary>///mouse position///</summary> private point Mousepoint {get {return Cursor.posi tion; }}///<summary>///Private method Get screen graphics (all graphics)///</summary> public Bitmap Screenimage {get {BitmapM_backbitmap = new Bitmap (screensize.width, screensize.height); Graphics _graphics = Graphics.fromimage (M_backbitmap); _graphics.copyfromscreen (new point (0, 0), new Point (0, 0), screensize); _graphics.dispose (); return m_backbitmap; }}///<summary>//hooks//</summary> private hookmessage m_hookmessage; <summary>////Screen handle///</summary> private IntPtr m_screenform; <summary>//graphics///</summary> private Bitmap m_image; public delegate void GetImage (Image p_image); <summary>///Get screen///</summary> public event GetImage Getscreenimage; <summary>//construction///</summary> public Copyscreen () {M_screenform = API. GetDesktopWindow (); M_hookmessage = new Hookmessage (Api. Windowshookcodes.wh_mouse_ll, True); M_hookmessage.gethook + = new Hookmessage.gethookmessage (M_hookmessage_gethook); }///<summary>//Hook events///</summary>//<param name= "P_code" ></param& Gt <param name= "P_wparam" ></param>//<param name= "P_lparam" ></param>//<para M name= "P_send" ></param> void M_hookmessage_gethook (int p_code, IntPtr p_wparam, IntPtr P_lparam, ref bool P_send) {if (m_starmouse) {switch (P_wparam.toint32 ()) { Case://move MouseMove (); Break Case 513://down MouseDown (); P_send = false; Break Case 514://up MouseUp (); P_send = false; Break Default:m_starmouse = false; Break }}}////<summary>//depending on the rectangle if width is being directly returned if the-will convert to a positive rectangle to ensure that the size position remains the same//</sum mary>//<param name= "P_rectangle" > Rectangle </param>//<returns> Positive Rectangle </returns> public static Rectangle Getuprightrectangle (Rectangle p_rectangle) {Rectangle _rect = P_rectangle; if (_rect.width < 0) {int _x = _rect.x; _rect.x = _rect.width + _rect.x; _rect.width = _x-_rect.x; } if (_rect.height < 0) {int _y = _RECT.Y; _rect.y = _rect.height + _rect.y; _rect.height = _y-_rect.y; } return _rect; } private Rectangle M_mouserectangle = new Rectangle (0, 0, 0, 0); private bool M_drawStar = false; private void MouseDown () {m_mouserectangle.x = Mousepoint.x; M_mouserectangle.y = Mousepoint.y; M_drawstar = true; } private void MouseMove () {if (M_drawstar) {Controlpaint.drawreversi Bleframe (M_mouserectangle, color.transparent, framestyle.dashed); M_mouserectangle.width = mousepoint.x-m_mouserectangle.x; M_mouserectangle.height = Mousepoint.y-m_mouserectangle.y; Controlpaint.drawreversibleframe (M_mouserectangle, Color.White, framestyle.dashed); }} private void MouseUp () {controlpaint.drawreversibleframe (M_mouserectangle, Color.tran Sparent, framestyle.dashed); M_drawstar = false; M_starmouse = false; Rectangle _screenrectangle = Getuprightrectangle (M_mouserectangle); m_mouserectangle.x = 0; m_mouserectangle.y = 0; m_mouserectangle.width = 0; m_mouserectangle.height = 0; if (getscreenimage! = null) {if (_screenrectangle.width! = 0 && _screenrectangle.height ! = 0) getscreenimage (M_image.clone (_screenrectangle, M_image.pixelformat)); }} private bool M_starmouse = false; <summary>///Get graphics///</summary> public void Gerscreenformrectangle () { M_image = Screenimage; M_starmouse = true; }///<summary>//Get graphics///</summary> public void Getscreen () { if (getscreenimage! = null) getscreenimage (screenimage); }}///<summary>/////[email protected]///</summary> public class Hookmes Sage {private INTPTR M_hookex; <summary>///Set your own process hooks///</summary>//<param name= "p_hookcodes "> Hook type </param> public hookmessage (API. Windowshookcodes p_hookcodes) {M_hookex = API. SetWindowsHookEx (int) p_hookcodes, new API. HookProc (Sethookproc), IntPtr.Zero, API. GetCurrentThreadID ()); }///<summary>///Set process hooks///</summary>//<param name= "P_hookcodes" > Hook class Type </param>//<param name= "P_threadid" > Global hooks </param> public hookmessage (API. Windowshookcodes P_hookcodes, bool P_zero) {IntPtr _value = System.Runtime.InteropServices.Marshal.GetH INSTANCE (System.Reflection.Assembly.GetExecutingAssembly (). GetModules () [0]); M_hookex = API. SetWindowsHookEx (int) p_hookcodes, new API. HookProc (Sethookproc), _value, 0); }//<summary>///close hook///</summary> public void Unhookmessage () { if (API. UnhookWindowsHookEx (M_hookex)) {M_hookex = IntPtr.Zero; }} public delegate void Gethookmessage (int p_code, IntPtr p_wparam, IntPtr p_lparam, ref bool P_ Send); public event Gethookmessage Gethook; Private INTPTR sethookproc (int p_code, IntPtr p_wparam, IntPtr p_lparam) {GC. Collect (); Gc. WaitForPendingFinalizers (); Gc. Collect (); bool _sendmessage = true; if (Gethook! = null) Gethook (P_code, P_wparam, P_lparam, ref _sendmessage); if (!_sendmessage) return new INTPTR (1); return IntPtr.Zero; } }}
It would be nice to cite the project in other projects.
WinForm Implementing a rectangular box