热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

非API程序模仿QQ截图。初学探讨。C#

本程序也是根据网上的代码学习模仿,完善了下。感谢原创网友。不能记得它的出处了。特别说明:本程序的思路和源程序一样,可能局部可以修改

本程序也是根据网上的代码学习模仿,完善了下。感谢原创网友。不能记得它的出处了。特别说明:本程序的思路和源程序一样,可能局部可以修改,但是为了保存原创成果,没有修改只做了添加。原创应该尊重。说本程序:

1,建立一个项目(windows窗体),默认的FORM1窗体中加入一个按钮控件。并且把窗体缩小。如图:2010062222053713.png

2,添加单击按钮控件代码如下:(整个程序,包括自动生成的代码,在FORM1内)

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

namespace jinyujietu
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        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);
            IntPtr dc = g.GetHdc();
            g.ReleaseHdc(dc);
            ScreenBody body = new ScreenBody();
            body.BackgroundImage = img;
            //body.Parent = this;
            body.Show();
        }
    }
}

3,添加一个窗体,名称为ScreenBody.cs;

设置窗体样式为none.在窗体上添加一个panel控件。在panel控件内添加2个标签控件label和2个按钮控件button。可以添加背景图片。还有保存控件一个和提示控件tooltip.再进行相应的设置。窗体界面设计如图:

2010062222153347.png

4,整个窗体中代码如下(没有添加注释,有网友的原创,可以参考。)

代码:

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

namespace jinyujietu
{
    public partial class ScreenBody : Form
    {
        public ScreenBody()
        {
            InitializeComponent();
        }
        private Graphics MainPainter;
        private Pen pen;
        private bool isDowned;
        private Image baseImage;
        private Rectangle Rect;
        private bool RectReady;
        private Point downPoint;
        private bool change;
        Rectangle[] Rectpoints;
        int point;
        int tmpx;
        int tmpy;
        int myfirstx;//记录截取的第一个点的x坐标
        int myfirsty;//记录截取的第一个点的y坐标
        int mylastx;//记录截取的最后个点的x坐标
        int mylasty;//记录截取的最后个点的y坐标

 

        private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
        {
            int width = 0;
            int heigth = 0;
            if (Mouse_y             {
                Rect.Y = Mouse_y;
                heigth = downPoint.Y - Mouse_y;
            }
            else
            {
                heigth = Mouse_y - downPoint.Y;
            }
            if (Mouse_x             {
                Rect.X = Mouse_x;
                width = downPoint.X - Mouse_x;
            }
            else
            {
                width = Mouse_x - downPoint.X;
            }
            Rect.Size = new Size(width, heigth);
            DrawRects(Painter);
        }
        private void DrawRects(Graphics Painter)
        {
            Painter.DrawRectangle(pen, Rect);
            /*
            Rectpoints[0].X = Rect.X - 2;
            Rectpoints[0].Y = Rect.Y - 2;
            Rectpoints[1].X = Rect.X - 2;
            Rectpoints[1].Y = Rect.Y - 2 + Rect.Height / 2;
            Rectpoints[2].X = Rect.X - 2;
            Rectpoints[2].Y = Rect.Y - 2 + Rect.Height;
            Rectpoints[3].X = Rect.X - 2 + Rect.Width / 2;
            Rectpoints[3].Y = Rect.Y - 2;
            Rectpoints[4].X = Rect.X - 2 + Rect.Width / 2;
            Rectpoints[4].Y = Rect.Y - 2 + Rect.Height;
            Rectpoints[5].X = Rect.X - 2 + Rect.Width;
            Rectpoints[5].Y = Rect.Y - 2;
            Rectpoints[6].X = Rect.X - 2 + Rect.Width;
            Rectpoints[6].Y = Rect.Y - 2 + Rect.Height / 2;
            Rectpoints[7].X = Rect.X - 2 + Rect.Width;
            Rectpoints[7].Y = Rect.Y - 2 + Rect.Height;
            Painter.FillRectangles(Brushes.Blue, Rectpoints);
             * */
        }

        private Image DrawScreen(Image back, int Mouse_x, int Mouse_y)
        {
            Graphics Painter = Graphics.FromImage(back);
            DrawRect(Painter, Mouse_x, Mouse_y);
            return back;
        }

        private void ScreenBody_DoubleClick(object sender, EventArgs e)
        {
            if (((MouseEventArgs)e).Button == MouseButtons.Right )
                this.Close();
           // if (((MouseEventArgs)e).Button == MouseButtons.Left && Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
      
            else// if(Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
           {
                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);
                //IntPtr dc = g.GetHdc();
                //g.ReleaseHdc(dc);
                Clipboard.SetImage(memory);
                System.Windows.Forms.DialogResult dlg;
                this.saveFileDialog1.DefaultExt = "jpg";
                this.saveFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF ";
                this.saveFileDialog1.FileName = "*.jpg";
                dlg = this.saveFileDialog1.ShowDialog();
                if (dlg == System.Windows.Forms.DialogResult.OK)
                 memory.Save(this.saveFileDialog1.FileName);
                 this.Close();
           }
           


        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ScreenBody_DoubleClick(sender, e);
        }

        private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
        {
            myfirstx = e.X;
            myfirsty = e.Y;
            if (e.Button == MouseButtons.Left)
            {
                myfirstx = e.X;
               myfirsty = e.Y;
                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;
                }
                for (int i = 0; i                 {
                    if (Rectpoints[i].Contains(e.X, e.Y))
                    {
                        change = true;
                        point = i + 1;
                    }
                }

            }
            if (e.Button == MouseButtons.Right)
            {
                if (RectReady != true)
                {
                    this.Close();
                    return;
                }
                this.CreateGraphics().DrawImage(baseImage, 0, 0);
                RectReady = false;
            }
        }

        private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDowned = false;
                RectReady = true;
                change = false;
                mylastx = e.X;
                mylasty = e.Y;
               this.panel1.Visible = 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 && change == false)
                    {
                        //和上一次的位置比较获取偏移量
                        Rect.X = Rect.X + e.X - tmpx;
                        Rect.Y = Rect.Y + e.Y - tmpy;
                        //记录现在的位置
                        tmpx = e.X;
                        tmpy = e.Y;
                        MoveRect((Image)baseImage.Clone(), Rect);
                    }
                }
                if (change == true && isDowned == true)
                {
                    switch (point)
                    {
                        case 1:

                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        case 4:
                            break;
                        case 5:
                            break;
                        case 6:
                            ChangeRect((Image)baseImage.Clone(), e.X, e.Y, ChangeSide.RightTop);
                            break;
                        case 7:
                            ChangeRect((Image)baseImage.Clone(), e.X, e.Y, ChangeSide.Right);
                            break;
                        case 8:
                            ChangeRect((Image)baseImage.Clone(), e.X, e.Y, ChangeSide.RightBottom);
                            break;
                    }

                }
            }
           
        }

        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;
            change = false;
            Rectpoints = new Rectangle[8];
            for (int i = 0; i             {
                Rectpoints[i].Size = new Size(4, 4);
            }
            //myRect mRect = new myRect();
           
            this.panel1.Visible = false;
        }
        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();
        }

        private void ChangeRect(Image image, int Position_x, int Position_y, ChangeSide Side)
        {
            int width = 0;
            int height = 0;
            Graphics Painter = Graphics.FromImage(image);
            switch (Side)
            {
                case ChangeSide.Left:
                    break;
                case ChangeSide.LeftBottom:
                    break;
                case ChangeSide.LeftTop:
                    Rect.Y = Position_y;
                    break;
                case ChangeSide.Bottom:
                    break;
                case ChangeSide.Top:
                    break;
                case ChangeSide.Right:
                    if (Position_x                     {
                        Rect.Size = new Size(tmpx - Position_x + Rect.Width, Rect.Height);
                        Rect.X = Position_x;
                        //记录现在的位置
                        tmpx = Position_x;
                    }
                    else
                        Rect.Size = new Size(Position_x - Rect.X, Rect.Height);
                    break;
                case ChangeSide.RightBottom:
                    Rect.Size = new Size(Position_x - Rect.X, Position_y - Rect.Y);
                    break;
                case ChangeSide.RightTop:
                    //Rect.Y = Position_y;
                    Rect.Size = new Size(Position_x - Rect.X, Rect.Height + Rectpoints[5].Y - Position_y);
                    break;
            }
            //Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);
            DrawRects(Painter);
            MainPainter.DrawImage(image, 0, 0);
            image.Dispose();
            /*
            MainPainter.DrawImage(New, 0, 0);
            New.Dispose();*/
        }

        private void ChangeRect(Image image, int Position, ChangeSide Side)
        {

        }

        enum ChangeSide
        {
            Left,
            LeftTop,
            LeftBottom,
            Right,
            RightTop,
            RightBottom,
            Top,
            Bottom
        }

        struct myRect
        {
            public int x;
            public int y;
            public int width;
            public int height;
            public Rectangle[] RectPoints;

            public void Init(int x, int y, int width, int height, int number)
            {
                this.x = x;
                this.y = y;
                this.width = width;
                this.height = height;
                RectPoints = new Rectangle[number];
            }
        }

        private void panel1_VisibleChanged(object sender, EventArgs e)
        {
            this.panel1.Top = mylasty;
            this.panel1.Left = mylastx - myfirstx;

            this.label1.Text = "(" + (mylastx - myfirstx).ToString() + "," + (mylasty - myfirsty).ToString() + ")";
            this.label2.Text = "完成";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      

    }
}

5,运行效果图:

2010062222202449.png2010062222203320.png2010062222205240.png

6,程序的截图效果图:

2010062222220056.jpg

 

转:https://www.cnblogs.com/jinyuttt/archive/2010/06/22/1762967.html



推荐阅读
  • 本文介绍了如何使用 Python 的 Pyglet 库加载并显示图像。Pyglet 是一个用于开发图形用户界面应用的强大工具,特别适用于游戏和多媒体项目。 ... [详细]
  • 在AngularJS中,有时需要在表单内包含某些控件,但又不希望这些控件导致表单变为脏状态。例如,当用户对表单进行修改后,表单的$dirty属性将变为true,触发保存对话框。然而,对于一些导航或辅助功能控件,我们可能并不希望它们触发这种行为。 ... [详细]
  • iOS如何实现手势
    这篇文章主要为大家展示了“iOS如何实现手势”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS ... [详细]
  • Kubernetes Services详解
    本文深入探讨了Kubernetes中的服务(Services)概念,解释了如何通过Services实现Pods之间的稳定通信,以及如何管理没有选择器的服务。 ... [详细]
  • 本文探讨了一个Web工程项目的需求,即允许用户随时添加定时任务,并通过Quartz框架实现这些任务的自动化调度。文章将介绍如何设计任务表以存储任务信息和执行周期,以及如何通过一个定期扫描机制自动识别并加载新任务到调度系统中。 ... [详细]
  • 使用 ModelAttribute 实现页面数据自动填充
    本文介绍了如何利用 Spring MVC 中的 ModelAttribute 注解,在页面跳转后自动填充表单数据。主要探讨了两种实现方法及其背后的原理。 ... [详细]
  • 我在尝试将组合框转换为具有自动完成功能时遇到了一个问题,即页面上的列表框也被转换成了自动完成下拉框,而不是保持原有的多选列表框形式。 ... [详细]
  • STM32代码编写STM32端不需要写关于连接MQTT服务器的代码,连接的工作交给ESP8266来做,STM32只需要通过串口接收和发送数据,间接的与服务器交互。串口三配置串口一已 ... [详细]
  • Android 开发技巧:使用 AsyncTask 实现后台任务与 UI 交互
    本文详细介绍了如何在 Android 应用中利用 AsyncTask 来执行后台任务,并及时将任务进展反馈给用户界面,提高用户体验。 ... [详细]
  • 本文详细介绍了如何在PyQt5中创建简易对话框,包括对话框的基本结构、布局管理以及源代码实现。通过实例代码,展示了如何设置窗口部件、布局方式及对话框的基本操作。 ... [详细]
  • 本文探讨了如何使用Scrapy框架构建高效的数据采集系统,以及如何通过异步处理技术提升数据存储的效率。同时,文章还介绍了针对不同网站采用的不同采集策略。 ... [详细]
  • 一、使用Microsoft.Office.Interop.Excel.DLL需要安装Office代码如下:2publicstaticboolExportExcel(S ... [详细]
  • 使用Python构建网页版图像编辑器
    本文详细介绍了一款基于Python开发的网页版图像编辑工具,具备多种图像处理功能,如黑白转换、铅笔素描效果等。 ... [详细]
  • 本文探讨了Android系统中联系人数据库的设计,特别是AbstractContactsProvider类的作用与实现。文章提供了对源代码的详细分析,并解释了该类如何支持跨数据库操作及事务处理。源代码可从官方Android网站下载。 ... [详细]
  • 本文分享了作者在使用LaTeX过程中的几点心得,涵盖了从文档编辑、代码高亮、图形绘制到3D模型展示等多个方面的内容。适合希望深入了解LaTeX高级功能的用户。 ... [详细]
author-avatar
杀你哥_52544
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有