金沙国际官网_金沙国际平台登录

因为这个金沙国际官网_金沙国际平台登录网站与很多的大型澳门赌场都有合作,金沙国际官网_金沙国际平台登录尽职尽责,高效执行,保持好奇心,不断学习,追求卓越,点击进入金沙国际官网_金沙国际平台登录马上体验吧,所以现在也正式地开始了营业。

您的位置:金沙国际官网 > 编程 > 非托管代码,仿站工具

非托管代码,仿站工具

发布时间:2019-11-02 02:11编辑:编程浏览(116)

    对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:
    1、首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要通过这个按钮来拖动窗体)

    前两天朋友叫我模仿一个网站,刚刚开始,我一个页面一个页面查看源码并复制和保存,花了我很多时间,一个字“累”,为了减轻工作量,我写了个网站“克隆工具”,一键克隆,比起人工操作, 
    效率提高了200%以上,精确度也大大提高,虽然网上也很多网站克隆工具,但我觉得作为一个程序员,要有点研究精神,哈哈哈,可以根据自己的需要随意编写自己需要的功能。

    .Net Framework 是由彼此独立又相关的两部分组成:CLR 和 类库, CLR是它为我们提供的服务,类库是它实现的功能.
    .NET的大部分特性----垃圾收集,版本控制,线程管理等,都使用了CLR提供的服务
    

    2、然后对按钮添加鼠标按下事件、鼠标移动事件和鼠标抬起事件,事件里面是对窗体和鼠标坐标的操作,整体代码如下:
    using System.Drawing;
    using System.Windows.Forms;
    namespace 窗体越界鼠标还能回到初始坐标
    {
        public partial class Form1 : System.Windows.Forms.Form
        {
            #region 变量定义
            public Point mainFormPosition;//主窗口左上角位置
            int workingAreaHeight = SystemInformation.WorkingArea.Height;
            int workingAreaWidth = SystemInformation.WorkingArea.Width;
            private Point relativetMousePosition;//鼠标相对窗体的相对坐标
            bool FirstLeft = false;//是否左边第一次按下时坐标
            bool FirstRight = false;//是否右边第一次按下时坐标
            bool FirstUp = false;//是否上边第一次按下时坐标
            bool FirstDown = false;//是否下边第一次按下时坐标
            bool isFirstDown = true;//是否第一次按下,鼠标越界纠正时判断是否是第一次按下,不是按下后又拖动了
            bool isMoveOut = false;//是否拖出
            //这两个变量做差,差值为拖出的距离时软件弹出侧边
            int oldMove_X = 0;//侧边停靠拖出时起始坐标
            //上一个时刻、鼠标的位置
            private Point oldMousePosition;
            //当前时刻、鼠标的位置
            private Point currentMousePosition;
            //鼠标事件
            [System.Runtime.InteropServices.DllImport("user32")]
            private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
            //移动鼠标
            const int MOUSEEVENTF_MOVE = 0x0001;
            //模拟鼠标左键按下
            const int MOUSEEVENTF_LEFTDOWN = 0x0002;
            //模拟鼠标左键抬起
            const int MOUSEEVENTF_LEFTUP = 0x0004;
            //模拟鼠标右键按下
            const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
            //模拟鼠标右键抬起
            const int MOUSEEVENTF_RIGHTUP = 0x0010;
            //模拟鼠标中键按下
            const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
            //模拟鼠标中键抬起
            const int MOUSEEVENTF_MIDDLEUP = 0x0040;
            //标示是否采用绝对坐标
            const int MOUSEEVENTF_ABSOLUTE = 0x8000;
            #endregion

    下面我将我写的“网站克隆工具”实现方法分享给大家,源码在文末有下载链接,有需要的朋友可以下载来玩,也可以根据自己的需要做相应的修改或优化。

    托管代码

            #region 窗体初始化
            public Form1()
            {
                InitializeComponent();
            }
            #endregion

    一睹为快,先看看界面:

     

            //-------------------------按钮button1鼠标事件---------------------------------------------------------------
            #region 鼠标左键按下Button1按钮事件
            private void Button1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    currentMousePosition = Control.MousePosition;
                    oldMousePosition = currentMousePosition;   //鼠标按下时、两个时刻的坐标变成一样,防止错误移动            
                    mainFormPosition = Location;
                    if (isFirstDown)//鼠标按下时,记录第一次鼠标按下相对主窗体的坐标
                    {
                        relativetMousePosition.X = currentMousePosition.X - Location.X;
                        relativetMousePosition.Y = currentMousePosition.Y - Location.Y;
                        isFirstDown = false;
                    }
                    if (isMoveOut)//鼠标按下时,记录拖出时的初始坐标
                    {
                        oldMove_X = currentMousePosition.X;
                    }
                }
            }
            #endregion

    图片 1

    托管代码(Managed Code)实际上就是中间语言(IL)代码。代码编写完毕后进行编译,此时编译器把代码编译成中间语言(IL),而不是能直接在你的电脑上运行的机器码。程序集(Assembly)的文件负责封装中间语言,程序集中包含了描述所创建的方法、类以及属性的所有元数据。
    托管代码在公共语言运行库(CLR)中运行。CLR提供了一个实时编译器,用来把IL代码编译为本机机器代码.这样一来,CLR能够使代码变得可移植,因为.NET应用程序的源代码必须被编译为IL代码,这些IL代码可以运行在任何提供CLR服务的平台上.从CLR的角度来看,所有的语言都是平等的,只要有一个能生成IL代码的编译器就行,这就确保了各种语言的互操性.

            #region 鼠标左键按住Button1按钮移动事件
            private void Button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    currentMousePosition = Control.MousePosition;
                    if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左边窗体越界坐标纠正
                    {
                        mainFormPosition.X = 0;
                        mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                        //保存上一个时刻的鼠标位置
                        oldMousePosition.Y = currentMousePosition.Y;
                        FirstLeft = true;
                    }
                    else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右边窗体越界坐标纠正
                    {
                        mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                        mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                        //保存上一个时刻的鼠标位置
                        oldMousePosition.Y = currentMousePosition.Y;
                        FirstRight = true;
                    }
                    else if (currentMousePosition.Y < relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth

     

    非托管代码

    • this.ClientSize.Width + relativetMousePosition.X)//上边窗体越界坐标纠正
                      {
                          mainFormPosition.Y = 0;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          FirstUp = true;
                      }
                      else if (currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X)//下边窗体越界坐标纠正
                      {
                          mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          FirstDown = true;
                      }
                      else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//左上角窗体越界坐标纠正
                      {
                          mainFormPosition.X = 0;
                          mainFormPosition.Y = 0;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          oldMousePosition.Y = currentMousePosition.Y;
                          FirstLeft = true;
                          FirstUp = true;
                      }
                      else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//右上角窗体越界坐标纠正
                      {
                          mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                          mainFormPosition.Y = 0;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          oldMousePosition.Y = currentMousePosition.Y;
                          FirstRight = true;
                          FirstUp = true;
                      }
                      else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左下角窗体越界坐标纠正
                      {
                          mainFormPosition.X = 0;
                          mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          oldMousePosition.Y = currentMousePosition.Y;
                          FirstLeft = true;
                          FirstDown = true;
                      }
                      else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右下角窗体越界坐标纠正
                      {
                          mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                          mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                          mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                          mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                          //保存上一个时刻的鼠标位置
                          oldMousePosition.X = currentMousePosition.X;
                          oldMousePosition.Y = currentMousePosition.Y;
                          FirstRight = true;
                          FirstDown = true;
                      }
                      if (FirstLeft)
                      {
                          currentMousePosition.X = relativetMousePosition.X;
                          FirstLeft = false;
                      }
                      if (FirstRight)
                      {
                          currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                          FirstRight = false;
                      }
                      if (FirstUp)
                      {
                          currentMousePosition.Y = relativetMousePosition.Y;
                          FirstUp = false;
                      }
                      if (FirstDown)
                      {
                          currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                          FirstDown = false;
                      }
                      if (FirstLeft && FirstUp)
                      {
                          currentMousePosition.X = relativetMousePosition.X;
                          currentMousePosition.Y = relativetMousePosition.Y;
                          FirstLeft = false;
                          FirstUp = false;

    简单的工作流程:

    在公共语言运行库环境的外部,由操作系统直接执行的代码。非托管代码必须提供自己的垃圾回收、类型检查、安全支持等服务,它与托管代码不同,后者从公共语言运行库中获得这些服务,而非托管代码是在运行库之外运行的代码。例如COM 组件、ActiveX 接口和 Win32 API 函数都是非托管代码的示例。

                    }
                    if (FirstRight && FirstUp)
                    {
                        currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                        currentMousePosition.Y = relativetMousePosition.Y;
                        FirstUp = false;
                        FirstRight = false;
                    }
                    if (FirstLeft && FirstDown)
                    {
                        currentMousePosition.X = relativetMousePosition.X;
                        currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                        FirstLeft = false;
                        FirstDown = false;
                    }
                    if (FirstRight && FirstDown)
                    {
                        currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                        currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                        FirstRight = false;
                        FirstDown = false;
                    }
                    //对窗体进行移动
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition = currentMousePosition;

    图片 2

    区别:

                    #region 越界处理
                    if (mainFormPosition.X < 0)//左边越界
                    {
                        mainFormPosition.X = 0;
                    }
                    else if (mainFormPosition.Y < 0)//上边越界
                    {
                        mainFormPosition.Y = 0;
                    }
                    else if (mainFormPosition.X > workingAreaWidth - this.ClientSize.Width)//右边越界
                    {
                        mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    }
                    else if (mainFormPosition.Y > workingAreaHeight - this.ClientSize.Height)//下边越界
                    {
                        mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    }
                    //更新主窗口位置
                    Location = mainFormPosition;
                    #endregion
                }
            }
            #endregion

     

         1、托管代码是一种中间语言,运行在CLR上;

            #region 鼠标在按钮Button1抬起事件
            private void Button1_MouseUp(object sender, MouseEventArgs e)
            {
                isFirstDown = true;
                //如果主窗体在这个位置,就判断允许主窗体在鼠标抬起后执行移动事件
                if (mainFormPosition.X == workingAreaWidth - this.ClientSize.Width)
                {
                    isMoveOut = false;
                }
                if (mainFormPosition.X == 0)
                {
                    isMoveOut = false;
                }
            }
            #endregion
        }
    }

    项目代码目录结构:

              非托管代码被编译为机器码,运行在机器上。

    动画效果如下:

    图片 3

         2、托管代码独立于平台和语言,能更好的实现不同语言平台之间的兼容;

     图片 4

     

              非托管代码依赖于平台和语言。

    这是本人在一个项目中遇到的问题,所以特意总结出来分享给大家。

    下面一步步实现程序功能:

         3、托管代码可享受CLR提供的服务(如安全检测、垃圾回收等),不需要自己完成这些操作;

     

              非托管代码需要自己提供安全检测、垃圾回收等操作。

    1.新建主界面窗体(MainForm.cs):

    .net的堆就是托管堆.没有非托管堆.引用类型的引用目标就是在堆里.

    图片 5

    值类型的值就在栈里.

    2.新建模型类(UrlModel.cs)

    所谓的系统资源.是指:网络连接,数据库连接.文件流.这种东西.

    public class UrlModel
        {
            public string RelatedPath { get; set; }
            public string AbsoluteUri { get; set; }
            public string CurrPath { get; set; }
            public string RootPath { get; set; }
    
            public string Host { get; set; }
            public int Port { get; set; }
            public string Scheme { get; set; }
        }
    

    这里的托管就是指被CLR管理,托管堆就是被CLR管理的堆。非托管资源需要手动释放,托管资源由GC帮你打理。

    3.新建服务类(Services)

    using可以跟踪非托管资源周期内的活动,一旦发现非托管资源生命结束了,就会强制调用dispose方法去释放在该作用域的非托管资源的内存。

    本文由金沙国际官网发布于编程,转载请注明出处:非托管代码,仿站工具

    关键词: