Windows Message Processing (C #)

Source: Internet
Author: User

For anyProgramPersonnel, the perception of the Windows message mechanism and the processing of the message can be said to be a required content. We know that the two parameters wparam and lparam of Windows messages are sometimes numerical and sometimes pointer type. In particular, the pointer type points to a memory address, so the processing of them varies depending on the development language.

C # has canceled the pointer (except for the non-secure mode), but I personally think that C # has not actually canceled the pointer, but simply made her more charming! It does not require programmers to directly process it through * P or the like.

Next I will take a routine to see how the pointer type parameters in Windows messages are processed in C. This routine was implemented by aogo using MASM assembly, which restricts the change of the size of a form within a permitted range. This example only limits the width to 600.

Two problems are involved:
1. How to obtain the content in the memory address pointed to by the pointer?
2. How to direct the pointer to new content? (Or copy the new content to the memory address pointed to by the pointer)

I know how to deal with these two problems. As for its internal implementation mechanism, I don't understand pai_^ ^_^.
With these two methods, we can process any windows message. C # is amazing !!!

Okay, you don't have to talk about it. I understand everything from the source code.

================================= Form1.cs ========================== ===

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. DaTa;
Using system. runtime. interopservices; // pay attention to this namespace

Namespace winmsgapp
{

Public class form1: system. Windows. Forms. Form
{

// Declare the constant of the winodws message
Private const int wm_sizeing = 0x0214;

// declare the rect structure
public struct rect
{< br> Public int left;
Public int top;
Public int right;
Public int bottom;
}

// The following is the code automatically generated by C # ide
private system. windows. forms. textbox textbox1;
private system. windows. forms. textbox textbox2;
private system. windows. forms. textbox textbox3;
private system. windows. forms. textbox textbox4;
private system. windows. forms. label label1;
private system. windows. forms. label label2;
private system. windows. forms. label label3;
private system. windows. forms. label label4;
private system. componentmodel. container components = NULL;

Public form1 ()
{< br> initializecomponent ();
}

Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region windows Form Designer generated coDe

Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. textbox2 = new system. Windows. Forms. Textbox ();
This. textbox3 = new system. Windows. Forms. Textbox ();
This. textbox4 = new system. Windows. Forms. Textbox ();
This. label1 = new system. Windows. Forms. Label ();
This. label2 = new system. Windows. Forms. Label ();
This. label3 = new system. Windows. Forms. Label ();
This. label4 = new system. Windows. Forms. Label ();
This. suspendlayout ();
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (80, 8 );
This. textbox1.name = "textbox1 ";
This. textbox1.tabindex = 0;
This. textbox1.text = "";
//
// Textbox2
//
This. textbox2.location = new system. Drawing. Point (80, 32 );
This. textbox2.name = "textbox2 ";
This. textbox2.tabindex = 1;
This. textbox2.text = "";
//
// Textbox3
//
This. textbox3.location = new system. Drawing. Point (80, 56 );
This. textbox3.name = "textbox3 ";
This. textbox3.tabindex = 2;
This. textbox3.text = "";
//
// Textbox4
//
This. textbox4.location = new system. Drawing. Point (80, 80 );
This. textbox4.name = "textbox4 ";
This. textbox4.tabindex = 3;
This. textbox4.text = "";
//
// Label1
//
This. label1.location = new system. Drawing. Point (0, 8 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (78, 23 );
This. label1.tabindex = 4;
This. label1.text = "Left :";
This. label1.textalign = system. Drawing. contentalignment. middleright;
//
// Label2
//
This. label2.location = new system. Drawing. Point (0, 32 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (78, 23 );
This. label2.tabindex = 5;
This. label2.text = "Top :";
This. label2.textalign = system. Drawing. contentalignment. middleright;
//
// Label3
//
This. label3.location = new system. Drawing. Point (0, 56 );
This. label3.name = "label3 ";
This. label3.size = new system. Drawing. Size (78, 23 );
This. label3.tabindex = 6;
This. label3.text = "Right :";
This. label3.textalign = system. Drawing. contentalignment. middleright;
//
// Label4
//
This. label4.location = new system. Drawing. Point (0, 80 );
This. label4.name = "label4 ";
This. label4.size = new system. Drawing. Size (78, 23 );
This. label4.tabindex = 7;
This. label4.text = "bottom :";
This. label4.textalign = system. Drawing. contentalignment. middleright;
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (296,117 );
This. Controls. addrange (new system. Windows. Forms. Control [] {
This. label4,
This. label3,
This. label2,
This. label1,
This. textbox4,
This. textbox3,
This. textbox2,
This. textbox1 });
This. Name = "form1 ";
This. Text = "form1 ";
This. resumelayout (false );

}
# Endregion
// The automatically generated code ends here

// the following code processes Windows messages:
protected override void wndproc (Ref system. windows. forms. message m)
{< br> switch (M. MSG)
{< br> case wm_sizeing:
// The first method is concise and clear
// rect rc = (rect) M. getlparam (typeof (rect);

// Method 2, easy to understand and more complex
Rect rc = new rect ();

// Copy the rect structure pointed to by the M. lparam pointer to the RC
Rc = (rect) M. getlparam (RC. GetType ());

// display the values of RC fields in the structure.
textbox1.text = RC. left. tostring ();
textbox2.text = RC. top. tostring ();
textbox3.text = RC. right. tostring ();
textbox4.text = RC. bottom. tostring ();

// Limit the form width to 600
If (RC. Right-RC. Left> 600)
{
RC. Right = RC. Left + 600;
}

// Copy the structure rc to the rect structure pointed to by the M. lparam pointer
Marshal. structuretoptr (RC, M. lparam, true );

Break;
Default:
Base. wndproc (ref m); // call the base class function to process other messages.
Break;
}
}


[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
}
}

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.