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 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能