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

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.补充一下是通过您自己的程序代码起到作用,而不是修改我上面的代码,我的代码就是个测试的小程序

c#

1个回答

不需要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:");
        }
    }
}

图片说明

binghe811215
binghe811215 您这个刚才我看了一下好像是在同一程序里拦截自己的textbox
6 个月之前 回复
binghe811215
binghe811215 您理解错了吧,我的意思是通过您做的程序hook我上面发的那个代码里的textbox1
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!