binghe811215 2019-05-14 16:54 采纳率: 0%
浏览 642
已结题

c#如何hook拦截并屏蔽textbox里接收的消息,替换成自己指定的字符串?

1.一个程序里的窗口会接收com口或随机的字符串,想学习一下如何劫持这个窗口让它在接收前,先审核一下条件,后台运算后(比如+100)在到文本框。
2.小程序代码如下:

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;
using System.Threading.Tasks;
using System.IO.Ports;

namespace text
{
    public partial class Form1 : Form
    {
        public string receiveing = string.Empty;



        public Form1()
        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            textBox1.Text = r.Next(1000, 10000).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            label1.Text = textBox1.Text+"保存成功!";
        }
        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            receiveing = serialPort2.ReadLine();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           serialPort2.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            textBox1.Text = receiveing;


        }



        }
    }


3.要求大神提供c#hook程序的源代码,小白再次万分感谢
4.补充一下是通过您自己的程序代码起到作用,而不是修改我上面的代码,我的代码就是个测试的小程序

  • 写回答

1条回答

  • threenewbee 2019-05-14 17:20
    关注

    不需要hook

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Q761397
    {
        public partial class Form1 : Form
        {
    
            private string preText = "";
            private int selstart = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.TextChanged += TextBox1_TextChanged;
            }
    
            private void TextBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text.ToLower().Contains('a'))
                {
                    listBox1.Items.Add("试图修改为 " + textBox1.Text + ",已经拦截");
                    textBox1.Text = preText;
                    textBox1.SelectionStart = selstart;
                }
                else
                {
                    preText = textBox1.Text;
                    selstart = textBox1.SelectionStart;
                }
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = Microsoft.VisualBasic.Interaction.InputBox("input:");
            }
        }
    }
    
    

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?