weixin_44131832
weixin_44131832
2019-01-08 11:29

使用钩子功能的C#程序运行十次左右后会失灵是什么原因

  • c#

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

namespace HookINCS
{
public partial class Form1 : Form
{
private Hook hook;

    public Form1()
    {
        InitializeComponent();
        hook = Hook.GetInstance();
        hook.OnKeyPress += new KeyEventHandler(OnKeyDown);
        hook.InstallKeyBoardHook();
    }

    //截图功能
    public static void Snap(object source, System.Timers.ElapsedEventArgs e)
    {
        int x = 300;
        int y = 200;
        int width = 500;
        int height = 500;
        Image image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);
        g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
        image.Save(@"C:\Users\q1120\Desktop\jietuzanliu\newpng.Png", System.Drawing.Imaging.ImageFormat.Png);

    }

    public static void phase2(object source, System.Timers.ElapsedEventArgs e)
    {
        MouseFlag.MouseLefClickEvent(380, 340, 0);
    }


    private void OnKeyDown(object sender, KeyEventArgs e) 
    {

        textBox1.Text = e.KeyCode.ToString();
        if (textBox1.Text == "Return")
        {
            //定时器
            System.Timers.Timer t = new System.Timers.Timer(12700);
            t.Elapsed += new System.Timers.ElapsedEventHandler(Snap);
            t.AutoReset = false;
            t.Enabled = true;

            //等待12秒
            System.Threading.Thread.Sleep(13000);

            // 图像处理
            Bitmap bmp = new Bitmap(@"C:\Users\q1120\Desktop\jietuzanliu\newpng.Png");

            for (int i = 1; i < 400; i++)
            {
                for (int j = 1; j < 400; j++)
                {

                    Color col = bmp.GetPixel(i, j);
                    if (col.R < 200 && col.G > 200 && col.B < 120)
                    {
                        MouseFlag.MouseLefClickEvent(480, 460, 0);
                        System.Timers.Timer t1 = new System.Timers.Timer(23000);
                        t1.Elapsed += new System.Timers.ElapsedEventHandler(phase2);
                        t1.AutoReset = false;
                        t1.Enabled = true;

                        break;
                    }

                }
            }
            textBox1.Text = "";
            bmp.Dispose();
        }
    }



}

}
刚学C#写的非常粗糙,但是程序是可以正常运行的,功能也都能实现,但是当运行十次左右后程序就无法再获得enter键按下的消息,这应该是什么原因

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐

换一换