Use custom messages in C #

Source: Internet
Author: User

 

C # In WinForm programming, have you ever encountered such a problem? click the button in Form1 in the main window to bring up a subwindow Form2. You want to call the Form1 function in Form2, changed the display of Form1, but found that Form1 was not obedient and could not achieve the expected results. Some people say that I debugged the function and it was actually executed. Why not show it?

Case:

In winform, I am clicking a button in from1 to bring up from3 to execute a function. In fact, form3 is a self-made password input box. Then I call it in from3.

If (password = PassWord)

{

Comfirm = true;

Form1.updateMac (row, col );

This. Close ();

}

In updateMac (row, col), I have a maid [row]. cells [col]. style. backColor = Color. green; but the color remains unchanged. It is particularly strange that it can be changed in other functions of form1, that is, in which form3 calls the form1 function, it cannot change color. Why?

 

In fact, Form1 is inevitable. In Form2, you create a new window object, which is different from its parent window, how can you expect object B to change as you change object?

Is there any way to achieve our expected results?

Some, that is, using the win32 function and using custom messages, C ++ users should be familiar with this. Let's talk about C # custom messages.

Message sender

1. Introduce namespace

 

Using System. Runtime. InteropServices;

Ii. Message Definition

 

// Custom message

 

Public const int USER = 0x500;

 

Public const int MYMESSAGE = USER + 1;

 

3. Custom struct

 

Public struct My_lParam

 

{

 

Public int I;

 

Public string s;

 

}

 

4. Rewrite the message sending Function

 

// Message sending API

 

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

 

Private static extern int SendMessage (

 

IntPtr hWnd, // handle of the window to which information is sent

 

Int Msg, // Message ID

 

Int wParam, // parameter 1

 

Ref My_lParam lParam

 

);

 

 

5. Introduction of functions for obtaining the form handle

 

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

 

Private extern static IntPtr FindWindow (string lpClassName, string lpWindowName );

 

6. Send messages

 

IntPtr = FindWindow (null, "Form1"); // obtain the form handle for receiving messages. Note that Form1 must be unique; otherwise, messages cannot be correctly sent by windows.

 

// Message Construction

 

My_lParam m = new My_lParam ();

 

M. s = textBox1.Text;

 

M. I = m. s. Length;

 

SendMessage (ptr, MYMESSAGE, 1, ref m); // send a message

 

 

Message recipient

1. Introduce namespace

 

Using System. Runtime. InteropServices;

 

Ii. Message Definition

 

// Custom message

 

Public const int USER = 0x500;

 

Public const int MYMESSAGE = USER + 1;

 

3. Rewrite the message processing function of the form

 

/// Rewrite the form's message processing function DefWndProc, and add the processing entry for detecting the custom message MYMESSAGE.

 

Protected override void DefWndProc (ref Message m)

 

{

 

Switch (m. Msg)

 

{

 

// Receive custom message MYMESSAGE and display its parameters

 

Case MYMESSAGE:

 

Form2.My _ lParam ml = new Form2.My _ lParam ();

 

Type t = ml. GetType ();

 

Ml = (Form2.My _ lParam) m. GetLParam (t );

 

Label1.Text = ml. s;

 

Break;

 

Default:

 

Base. DefWndProc (ref m );

 

Break;

 

}

 

}

 

As shown above, we have implemented a C # custom message program and pasted a program: http://download.csdn.net/detail/bdstjk/2707423.

I don't know if there are any better implementation methods in C.

After research, C # does have a way to implement this function more easily. For details, refer to C # delegated instance (cross-form operation control)

 

Finally, we will introduce you to the simplest C # Cross-form operation.

 

Running the program:

 

 

 


From tu jiankai's column

Related Article

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.