weixin_44131832 2019-01-08 11:29
浏览 702

使用钩子功能的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条回答

    报告相同问题?

    悬赏问题

    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮