Program
Because I recently wanted to write a program similar to Remote Desktop monitoring, screen capture is used in this program. To achieve the program's part of the function, made a small demo. The program is very simple, the technology used is not much, can only achieve similar QQ screenshot function (although the method is very stupid)
The program flow is as follows:
1. Intercept the entire screen and save
2. Open a new Full-screen window, the saved screen as the background
3. Drag the mouse to change the scope of interception, right key cancellation
4. Double click Intercept, save on Pasteboard, full screen window closed
Okay, here's the Code section.
First create a new project Screencutter (VS2005), change the form name to MainForm, and then create a new form screenbody.
Add a button btncutter to Screencutter and add a button event:
private void Btncutter_click (object sender, EventArgs e)
{
Image img = new Bitmap (Screen.allscreens[0]. Bounds.width, Screen.allscreens[0]. Bounds.height);
Graphics g = Graphics.fromimage (IMG);
G.copyfromscreen (new point (0, 0), new Point (0, 0), Screen.allscreens[0]. Bounds.size);
Screenbody BODY = new Screenbody ();
Body. BackgroundImage = img;
Body. Show ();
}screen.allscreens[0] is the first to get all the current device Windows, I have only one monitor here, of course I am the first one.
Use the graphics CopyFromScreen function to get the current screen.
OK, now press the button and the Full screen window will come out.
The Full screen window screenbody, first set the form FormBorderStyle to none, and then declare the following variables
Private Graphics Mainpainter; Main brushes
Private Pen pen; It's a pen.
private bool isdowned; Determine if the mouse is pressed
private bool Rectready; Whether the rectangle is drawn complete
Private Image baseimage; Basic graphics (original picture)
Private Rectangle Rect; is the rectangle to be saved
Private point Downpoint; Mouse Down Point
int tmpx;
int tmpy;
After that is the form of the mouse function, which many of the code did not make a collation, looked at, the collation of the code should be less and more concise
private void Screenbody_doubleclick (object sender, EventArgs e)
{
if (((MouseEventArgs) e). Button = = MouseButtons.Left &&rect.contains ((MouseEventArgs) e). X, ((MouseEventArgs) e). Y))
{
There are many ways to save it ... I only use this here.
Image memory = new Bitmap (rect.width, rect.height);
Graphics g = graphics.fromimage (memory);
G.copyfromscreen (rect.x + 1, rect.y + 1, 0, 0, rect.size);
Clipboard.setimage (memory);
This. Close ();
}
}
private void Screenbody_mousedown (object sender, MouseEventArgs e)
{
if (E.button = = MouseButtons.Left)
{
Isdowned = true;
if (Rectready = = False)
{
Rect.x = e.x;
Rect.y = e.y;
Downpoint = new Point (e.x, e.y);
}
if (Rectready = = True)
{
TMPX = e.x;
Tmpy = e.y;
}
}
if (E.button = = mousebuttons.right)
{
if (Rectready!= true)
{
This. Close ();
Return
}
Mainpainter.drawimage (baseimage, 0, 0);
Rectready = false;
}
}
private void Screenbody_mouseup (object sender, MouseEventArgs e)
{
if (E.button = = MouseButtons.Left)
{
isdowned = false;
Rectready = true;
}
}
private void Screenbody_mousemove (object sender, MouseEventArgs e)
{
if (Rectready = = False)
{
if (isdowned = = True)
{
Image New = Drawscreen ((Image) Baseimage.clone (), e.x, e.y);
Mainpainter.drawimage (New, 0, 0);
New.dispose ();
}
}
if (Rectready = = True)
{
if (Rect.contains (e.x, e.y))
{
This. Cursor = Cursors.hand;
if (isdowned = = True)
{
Compare the previous position to get the offset
Rect.x = Rect.x + e.x-tmpx;
Rect.y = Rect.y + e.y-tmpy;
Record the current position
TMPX = e.x;
Tmpy = e.y;
Moverect ((Image) Baseimage.clone (), Rect);
}
}
}
}
private void Screenbody_load (object sender, EventArgs e)
{
This. WindowState = formwindowstate.maximized;
Mainpainter = this. CreateGraphics ();
Pen = new Pen (brushes.blue);
isdowned = false;
Baseimage = this. BackgroundImage;
Rect = new Rectangle ();
Rectready = false;
}
Auxiliary functions
Should write more auxiliary functions, the form response function inside the code into the inside, but I am very lazy, it will be done. hehe
private void DrawRect (Graphics painter, int mouse_x, int mouse_y)
{
int width = 0;
int heigth = 0;
if (Mouse_y < RECT.Y)
{
Rect.y = mouse_y;
Heigth = downpoint.y-mouse_y;
}
Else
{
Heigth = Mouse_y-downpoint.y;
}
if (Mouse_x < rect.x)
{
Rect.x = mouse_x;
width = downpoint.x-mouse_x;
}
Else
{
width = mouse_x-downpoint.x;
}
Rect.size = new Size (width, heigth);
Painter.drawrectangle (pen, Rect);
}
Private image Drawscreen (image back, int mouse_x, int mouse_y)
{
Graphics painter = Graphics.fromimage (back);
DrawRect (painter, mouse_x, mouse_y);
return to back;
}
private void Moverect (image image, Rectangle Rect)
{
Graphics painter = Graphics.fromimage (image);
Painter.drawrectangle (pen, Rect.x, Rect.y, Rect.width, rect.height);
Drawrects (painter);
Mainpainter.drawimage (image, 0, 0);
Image. Dispose ();
}
Here, the code, even if it's finished, runs
The interception of the result, there is no control of the boundary, so there are boundaries can be seen, a little set up on it
All right, this thing is finished, next to make use of the hook .... Hope to finish quickly, tired Ah ~ ~ ~ ~ ~