using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ScreenShot_CS
{
    public partial class Form1 : Form
    {
        [DllImport("coredll.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("coredll.dll", EntryPoint = "GetDC")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("coredll.dll", EntryPoint = "ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("coredll.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

        const int SRCCOPY = 0x00CC0020;


        public Form1()
        {
            InitializeComponent();
            #if DEBUG
                MinimizeBox = false;
            #else       
                MinimizeBox = true;
            #endif
        }

        private void ScreenShot(string filename, Graphics gx, Rectangle rect)
        {
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);

            Graphics g = Graphics.FromImage(bmp);
            
            BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, gx.GetHdc(), rect.Left, rect.Top, SRCCOPY);
            bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
            
            bmp.Dispose();
            g.Dispose();
        }

        private void butPartOfWindow_Click(object sender, EventArgs e)
        {
            // Делаем снимок списка
            ScreenShot(@"\My Documents\listbox.bmp", this.CreateGraphics(), listBox1.Bounds);
        }

        private void butScreen_Click(object sender, EventArgs e)
        {
            // Делаем снимок экрана
            Rectangle rect = new Rectangle(0,0,240,240);
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            
            Graphics g = Graphics.FromImage(bmp);

            IntPtr hwnd = GetDesktopWindow();
            IntPtr hdc = GetDC(hwnd);
            
            BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, hdc, rect.Left, rect.Top, SRCCOPY);
            bmp.Save(@"\My Documents\screen.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            // Освобождаем ресурсы
            ReleaseDC(hwnd, hdc);
            bmp.Dispose();
            g.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Делаем снимок экрана через 5 секунд
            Rectangle rect = new Rectangle(0, 0, 240, 240);
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            
            Graphics g = Graphics.FromImage(bmp);

            IntPtr hwnd = GetDesktopWindow();
            IntPtr hdc = GetDC(hwnd);
            
            BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, hdc, rect.Left, rect.Top, SRCCOPY);
            bmp.Save(@"\My Documents\5sec.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            // Освобождаем ресурсы
            ReleaseDC(hwnd, hdc);
            bmp.Dispose();
            g.Dispose();
            timer1.Enabled = false;
        }

        private void but5Sec_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
    }
}