SetParent is an API function that specifies a new parent window for an object. (The parent window should be strict, it should be a container, I understand it so much). That is to turn an object to another object.
SetParent usage is quite simple
The syntax is:
SetParent object handle, target handle
However, if you want to make it really have practical value, you need a certain knowledge and skill.
For example, put a button on another form (another form can be in other programs):
SetParent Command1.hWnd, & HXXXXX '(assuming & hxxxxx is another form of handle)
Then you will find that you click on the button on the desktop will not perform any code in the original Command_Click event!
The way to solve is a variety of ways
If you just want it to execute the code, don't want to learn more about WIN's message operation mechanism, then I suggest that you put this button into a container, such as PictureBox, or farme, then go to another form. Instead of turning the button on the form. Then this button will perform code in the original CLIC. As long as the size of the container is the same as the button size, then it feels that there is no container exists.
If you want to know the WIN's message operation mechanism, then I suggest you use subclasses to solve this problem.
In this way, let's talk about the Message Mechanism of Win, and simply, "the event is based on the message"
In fact, WIN is not recognized. The so-called "event" is only some of the development environments to simplify the number of newspapers in order to simplify the number of developers. Memorant of image: If there is a "eating" The event. Then there is a "end bowl" / "Take the chopstick" / "pick the rice" / "put the bowl" / "put the chopstick" and other news. We will deal with this series of news And attribute it into an event.
I will briefly explain what news will happen when a button clicks the event: (there are too many messages that have occurred, only a few more important :)
First, when the mouse is downward, the system sends a message to the button to the button, and then sends a message changed to it (the state of the button is concave). Send a heavy draw object Message (change the original bump appearance, make the button to draw the appearance of the concave). In this way, we see that the button is next to the left mouse button. Then send a message to the left mouse button, (the mouse movement may occur in the middle, but we can continue the message-changing message ", which will continue to send object style changes (so that the button is protruding, that is, the reply is normal), Then send a button to redraw a message. At the end, the button will send a command message to the end of the container as the end of the clicking event, ie, click to success. (In the message in which the mouse is lifted, the parameters of some messages are used to determine if the mouse is in the button area coordinate. This we can do not have to pay attention to it)
In the above message, you can take a message as the determination button as a point, such as cutting the WM_LBUTTONDOWN message under the left mouse left mouse, or cut the WM_LBUTTONUP pop-up message, where again use the API to get bumble The mouse coordinates can know if it is clicked.
However, the direct judgment button will be sent out when the button is finally clicked, but it can directly determine whether the button is clicked! WM_COMMAND message
But since this message is not sent to the button, it is sent to the system by the button, so many people will ignore, because many people have become accustomed to blocking messages on the button, but because subclasses can only intercept objects The resulting message cannot intercept the message sent by the object, so this WM_COMMAND message is not much.
So we only need to intercept the message on its container to know whether it is clicked!
Such as: setParent command1.hwnd, & hxxxx '(assuming & hxxxxx is another form of handle value) then we only need to intercept "& HXXXXX", when you find a WM_COMMAND message, it is judged that its WPRARM parameters and LPRARM parameters, if The WPRARM parameter is not 0, and when lprarm is command1.hwnd, then we know that command1 is clicked. By judging that the LPRARM can also determine whether other buttons are clicked! In this way, we can put multiple buttons in one time, and then determine which one is clicked on multiple buttons.
Note: The message sent after the button is not sent to it, but the container sent to it!
I have seen a master because I don't know this, I only have the interception of this message, and the result is that I always stop causing a function that cannot be implemented.
So you should first judge the container in which this button is located. In general, the designated new parent form is the container.
Also, intercepted some command messages to the object's container, such as WM_COMMAND messaging is widely used.
For example, use the API to add some menus, you need to let these menus perform actions, you need to block the form's WM_SYSCMD (I have forgotten this or WM_SYSCOMMAND). By determining the two parameter values behind it, it can perform the corresponding operation. Because menus cannot be placed in a container such as PictureBox, a message that is typically only cut.