C#实现 -- 自动获取剪贴板的文字并且自动发送

〇、背景

  在某个时刻,总有那么些时刻或者那么些人恶搞一下,就是无情的进行消息轰炸,所以就想着是不是可以搞一个工具出来。

  在 windows 下用 C# 编写的一个自动发消息的工具。

一、方法

  主要的功能就是模拟 Ctrl + V 粘贴,然后模拟 Enter (回车)按键将消息发送出去。

二、实现

2.1、界面效果

  开发界面显示如下图所示。

界面效果图

2.2、实现代码

  实现的代码如下(纯 C# )。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);

        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);


        public const int WM_CLOSE = 0x10;

        #region 预定义参数

        #endregion
        // 键盘虚拟键值
        private const int VK_CONTROL = 0X11;
        private const int VK_MENU = 0X12;        private const int VK_V = 0X56;          // 'S'
        private const int VK_F4 = 0X73;

        private const int KEYEVENTF_EXTENDEDKEY = 0x1;
        private const int KEYEVENTF_KEYUP = 0x2;

        // WindowMessage 参数
        private const int WM_KEYDOWN = 0X100;
        private const int WM_KEYUP = 0X101;
        private const int WM_SYSCHAR = 0X106;
        private const int WM_SYSKEYUP = 0X105;
        private const int WM_SYSKEYDOWN = 0X104;
        private const int WM_CHAR = 0X102;
        private const int WM_DOWN = 0X0028;
        private const int WM_ENTER = 0X000D;
        private const int WM_QUIT = 0X0012;

        // keybd_event 状态参数
        public const int WM_SYSCOMMAND = 0x111;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_MAXIMIZE = 0xF030;
        public const int VK_RETURN = 0x0D;


        UInt32 time = 3000;
        public Form1()
        {
            InitializeComponent();

            btn_Statt_Stop.ForeColor = Color.DarkGreen;
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.Show(); // 将此界面显示在最前端
            // this.Activate(); // 激活此界面
            //this.Focus(); // 界面获取焦点
            keybd_event(VK_CONTROL, 0, 0, 0);                   // 模拟 Ctrl 按键被按下
            keybd_event(VK_V, 0, 0, 0);
            Thread.Sleep(1);

            keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0); // 模拟 V 键被按下
            keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
            Thread.Sleep(1);

            keybd_event(VK_RETURN, 0, 0, 0);     // 模拟 Enter 按键被按下 
            keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
        }

        private void btn_Statt_Stop_Click(object sender, EventArgs e)
        {
            // 转换定时器的设置时间
            time = Convert.ToUInt32(numericUpDown1.Text) * 1000;
            timer1.Interval = (int)time; // 设置定时器的时间

            if (btn_Statt_Stop.ForeColor == Color.DarkGreen) //判断当前定时器是否启动
            {
                timer1.Enabled = true;
                btn_Statt_Stop.ForeColor = Color.DarkRed;
                btn_Statt_Stop.Text = "停  止";
            }
            else
            {
                timer1.Enabled = false;
                btn_Statt_Stop.ForeColor = Color.DarkGreen;
                btn_Statt_Stop.Text = "启  动";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

三、测试

  已经经过严格的测试,并且因为软件没有出问题导致接收消息的人将我直接拉黑了屏蔽了。

四、改进

  目前存在一个缺点,就是先需要将发送的数据要手动进行复制(CTRL+C或者手动复制就可),当然改进的话可以在界面上新增一个输入框,将要发送的数据输入,即可。

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现自动登录并获取 cookie 的方法可以分为以下几步: 1. 创建一个 `HttpWebRequest` 对象,设置请求的 URL 和方法(POST) 2. 设置请求的头部信息,比如 User-Agent 和 Content-Type 3. 设置请求的参数,如用户名和密码 4. 发送请求并获取响应 5. 从响应中获取 cookie 下面是一个示例代码: ```csharp using System; using System.IO; using System.Net; using System.Text; namespace AutoLogin { class Program { static void Main(string[] args) { string url = "https://www.example.com/login"; string username = "your_username"; string password = "your_password"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // 设置请求方法为 POST request.Method = "POST"; // 设置请求头部信息 request.UserAgent = "Mozilla/5.0"; request.ContentType = "application/x-www-form-urlencoded"; // 设置请求参数 string postData = "username=" + username + "&password=" + password; byte[] postDataBytes = Encoding.UTF8.GetBytes(postData); request.ContentLength = postDataBytes.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postDataBytes, 0, postDataBytes.Length); requestStream.Close(); // 发送请求并获取响应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader responseReader = new StreamReader(responseStream); string responseText = responseReader.ReadToEnd(); responseReader.Close(); responseStream.Close(); // 获取 cookie string cookie = response.Headers.Get("Set-Cookie"); Console.WriteLine(cookie); } } } ``` 这个示例代码模拟了一个登录请求,并获取了响应中的 cookie。你可以根据实际情况修改代码中的参数来实现你自己的自动登录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青椒*^_^*凤爪爪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值