C # Passing custom struct

Source: Internet
Author: User

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 );
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.