onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

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

namespace test
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern int GlobalAddAtom(string lpString);

        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern int GlobalDeleteAtom(int atom);

        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern uint GlobalGetAtomName(int nAtom, string lpBuffer, int nSize);

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

        /// <summary>
        /// 定义了一个专用通知消息
        /// </summary>
        private const int WM_TextNotify = 1090;
        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="pHandle">接收句柄</param>
        /// <param name="sText">发送的文本</param>
        private static void SendFormMessage(int pHandle, string sText)
        {
            int atom = GlobalAddAtom(sText);
            SendMessage(pHandle, WM_TextNotify, 0, atom);
            GlobalDeleteAtom(atom);
        }

        public Form1()
        {
            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

            this.textBox1.Text = this.Handle.ToInt32().ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendFormMessage(int.Parse(textBox1.Text.Trim()), textBox2.Text);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg.Equals(WM_TextNotify))
            {
                string cmd = string.Empty;
                GlobalGetAtomName(m.LParam.ToInt32(), cmd, 1024);

                this.textBox3.Text = cmd;
            }
        }
    }
}

 

 

demo: https://files.cnblogs.com/files/onlyou13/testSendAtom.rar

posted on 2019-12-11 22:05  onlyou13  阅读(481)  评论(0编辑  收藏  举报