热门标签 | 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



推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
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社区 版权所有