Using the Windows message loop mechanism to pass custom structs may be frequently used during programming. Here I write a simple struct for passingCodeIt should be noted that the class cannot be passed and only the struct can be passed.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Namespace customtest
{
Public struct student
{
Public string name;
Public String ID;
}
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private const int wm_usermsg = 0x0400 + 1;
[Dllimport ("user32.dll", entrypoint = "sendmessage")]
Private Static extern int sendmessage (
Intptr hwnd, // handle to destination window
Int MSG, // message
Int wparam, // first Message Parameter
Intptr lparam // second Message Parameter
);
protected override void defwndproc (ref message m)
{< br> switch (M. MSG)
{< br> case wm_usermsg:
Student s = (student) Marshal. ptrtostructure (M. lparam, typeof (student);
MessageBox. show (S. name, S. ID);
break;
}< br> base. defwndproc (ref m);
}
private void button#click (Object sender, eventargs e)
{< br> Student s = new student ();
S. name = "zhouweigang";
S. id = "11103132";
intptr handle = intptr. zero;
handle = marshal. allochglobal (marshal. sizeof (typeof (student);
marshal. structuretoptr (S, handle, true);
If (handle! = Intptr. Zero)
{
Sendmessage (this. Handle, wm_usermsg, 0, handle );
}
}
}
}