Drawing on computer screen

Source: Internet
Author: User

There are some software on the window that can be painted on the computer screen. How is this done? I checked some relevant information and found that, in fact, in the window system, it is impossible to directly output the picture I painted to the computer's display screen, those practices of system interruption in the DOS era cannot be performed on Windows !!! So how did those software be drawn? In fact, there are a lot of software that I have adopted here. If you are interested, try it :)

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;

Namespace bitbltscreen
{
/// <Summary>
/// Summary of showscreen.
/// </Summary>
Public class showscreen: system. Windows. Forms. Form
{
Private const int srccopy = 0xcc0020;

[System. runtime. interopservices. dllimportattribute ("user32.dll")]
Private Static extern intptr getasktopwindow ();

[System. runtime. interopservices. dllimport ("gdi32.dll")]
Public static extern long bitblt (intptr hdcdest, int nxdest, int nydest, int nwidth, int nheight, intptr hdcsrc, int nxsrc, int nysrc, int dwrop );

[System. runtime. interopservices. dllimportattribute ("user32.dll")]
Private Static extern intptr getdc (intptr hwnd );

[System. runtime. interopservices. dllimportattribute ("user32.dll")]
Private Static extern int releasedc (intptr hwnd, intptr HDC );

Private bitmap m_image; // temporary bitmap
Private point m_spoint; // the starting point of the draw line.
Private point m_epoint; // the end point of the draw line.
Private graphics m_graphics; // graphic object used for draw line
Private color m_linecolor;
Private system. Windows. Forms. contextmenu contextmenu1;
Private system. Windows. Forms. menuitem menuitem1;
Private system. Windows. Forms. menuitem menuitem2;

/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;

Public showscreen ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
This. Location = new point (0, 0 );
This. size = new size (screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height );

M_linecolor = color. fromargb (, 0, 0); // initialized red
}

# Region code generated by Windows Form Designer
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. contextmenu1 = new system. Windows. Forms. contextmenu ();
This. menuitem1 = new system. Windows. Forms. menuitem ();
This. menuitem2 = new system. Windows. Forms. menuitem ();
//
// Contextmenu1
//
This. contextmenu1.menuitems. addrange (new system. Windows. Forms. menuitem [] {
This. menuitem1,
This. menuitem2 });
//
// Menuitem1
//
This. menuitem1.index = 0;
This. menuitem1.text = "copyscreen (& C )";
This. menuitem1.click + = new system. eventhandler (this. menuitem#click );
//
// Menuitem2
//
This. menuitem2.index = 1;
This. menuitem2.text = "exit (& E )";
This. menuitem2.click + = new system. eventhandler (this. menuitem2_click );
//
// Showscreen
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,270 );
This. contextmenu = This. contextmenu1;
This. formborderstyle = system. Windows. Forms. formborderstyle. None;
This. Name = "showscreen ";

}
# Endregion

Private void menuitem1_click (Object sender, system. eventargs E)
{
Loadscreen ();
}

Private void menuitem2_click (Object sender, system. eventargs E)
{
This. Close ();
}

Public void loadscreen ()
{
Intptr desk = getdesktopwindow (); // obtain the top-level window of the computer.
Intptr screendc = getdc (DESK); // get the drawing device handle of the window.

// Generate a bitmap of the same size as the screen
M_image = new Bitmap (screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height );
Graphics G = graphics. fromimage (m_image); // create a drawing object from this bitmap.
Intptr thedc = G. gethdc (); // obtain the device handle of the Drawing Object of the figure.

// Copy the top-level window to the bitmap for reuse.
Long tmplong = bitblt (thedc, 0, 0, screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height, screendc, 0, 0, srccopy );

G. releasehdc (thedc); // Delete the handle of the bitmap drawing device.
Releasedc (this. Handle, screendc); // Delete the top-level window drawing device handle
}

Protected override void onpaint (painteventargs E)
{
Base. onpaint (E );
// Draw a bitmap when repainting a window
If (m_image! = NULL)
{
E. Graphics. drawimage (m_image, 0, 0 );
}
}

Protected override void onmousedown (mouseeventargs E)
{
Base. onmousedown (E );
// Write down the starting point of the draw line when you press the mouse.
M_spoint = new point (E. X, E. y );
// Create an object for drawing
If (m_graphics = NULL)
{
M_graphics = This. creategraphics ();
}
}
Protected override void onmousemove (mouseeventargs E)
{
Base. onmousemove (E );
If (E. Button = mousebuttons. Left)
{
// If you click the left mouse button to draw a picture, it is a red line
M_graphics.drawimage (m_image,); // the call here is to clear the original line. I don't know how to do better, so...
Point tmppoint = new point (E. X, E. y );
M_graphics.drawline (new pen (m_linecolor), m_spoint, tmppoint); // draw a new line.

M_epoint = tmppoint; // write down the new vertex
}
}
Protected override void onmouseup (mouseeventargs E)
{
Base. onmouseup (E );
// Write down the mouse key when it is lifted to the in-place chart
Graphics tmpgraphics = graphics. fromimage (m_image );
Tmpgraphics. drawline (new pen (m_linecolor), m_spoint, m_epoint );
}
}
}

In this case, you can copy the code and try again ~ Ha ~

However, you need to take down the screen before using it. Otherwise, it will be ineffective :)
Private void button#click (Object sender, system. eventargs E)
{
Showscreen Ss = new showscreen ();
SS. loadscreen ();
SS. Show ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.