C#自定義結構體的(用SendMessage)傳遞


要傳遞結構體

public struct STUDENT
    {
        public int id;                //ID
        public string name;      //姓名
    }    

要引用Win32api函數FindWindow,SendMessage

 /// <summary>
        /// 查找窗體句柄
        /// </summary>
        /// <param name="lpClassName">指定的類名</param>
        /// <param name="lpWindowName">指定的窗口名稱</param>
        /// <returns>返回窗口句柄</returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public  static extern IntPtr FindWindow(
            string lpClassName,
            string lpWindowName);

        /// <summary>
        /// 查找窗體句柄
        /// </summary>
        /// <param name="hwndParent">父窗口句柄</param>
        /// <param name="hwndChildAfter">子窗口句柄</param>
        /// <param name="lpClassName">窗口類名</param>
        /// <param name="lpWindowName">窗口名稱</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(
            IntPtr hwndParent,
            IntPtr hwndChildAfter, 
            string lpClassName,
            string lpWindowName);


        [DllImport("User32.dll", EntryPoint = "SendMessage")]

        public static extern int SendMessage(
            IntPtr hWnd,    // 窗口句柄
            int Msg,     // message
            int wParam,    // first message parameter
            IntPtr lParam);      // second message parameter

3,還要定義個消息

public const int WM_COPYDATA = 0X004A;

 也可以發送自定義消息   public const int WM_USER = 1024;        //自定義消息

4,發送消息

//先查找要發送的窗口句柄
IntPtr nRet = Win32API.FindWindowEx(IntPtr.Zero, IntPtr.Zero,null , "Form2");
            if (nRet == IntPtr.Zero)
                MessageBox.Show("沒有找到指定的窗口");

STUDENT stu = new STUDENT();
            stu.id = 101;
            stu.name = "我是發送者";
            IntPtr stuPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(STUDENT)));
            Marshal.StructureToPtr(stu, stuPtr, false);

            SendMessage(nRet, WM_COPYDATA, 0, stuPtr);
      //用PostMessage發送消息
      //PostMessage(nRet, WM_USER, 0, stuPtr);

 

 

5,在接收窗口中重寫DefWndProc函數

 

public const int WM_COPYDATA = 0X004A;
public const int WM_USER = 1024;        //自定義消息
protected override void DefWndProc(ref Message m) { if(m.Msg==WM_COPYDATA) { STUDENT stu = (STUDENT)Marshal.PtrToStructure(m.LParam, typeof(STUDENT)); textBox1.Text = stu.name;
          return; }         

if(m.Msg==WM_USER)
{
  STUDENT stu = (STUDENT)Marshal.PtrToStructure(m.LParam, typeof(STUDENT));
  textBox1.Text = stu.name;
  return;
}

            base.DefWndProc(ref m);
        }

 6,在接收窗口也要定義WM_COPYDATA消息,和結構體,

 

 

7,在WM_COPYDATA傳遞COPYDATASTRUCT結構體也是可行的

IntPtr cdsPtr = IntPtr.Zero;
cdsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT)));
Marshal.StructureToPtr(cds, cdsPtr, false);

SendMessage(nRet, WM_COPYDATA, 0, cdsPtr);

 

接收窗口中

COPYDATASTRUCT cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
textBox1.Text = cds.lpData;

 

注意:要用Marshal類要加命名空間 using System.Runtime.InteropServices;

 

今天就弄到這了,其他的API以后有時間再弄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM