It is the same effect as dragging in a Windows program.
After analysis, the mouse drag is actually to press the mouse on a form, change the mouse's cursor icon, then press and hold, move the mouse, and release the mouse button on any form.
There are two major actions: one is to press the mouse, the other is to release, will generate an event in the corresponding form.
One problem to be solved is that when we click the mouse, we change the mouse's cursor icon, drag the mouse to a form, and release the button. Maybe this form is not our target form, at this time, we need to change the mouse's cursor icon back to the original, and we cannot write a cursor icon process to process the mouse in all forms. So we can change the cursor icon when the mouse is released in one place.
All mouse events in ucgui go through wmtouch. the wm_handlepid process in c is used for processing. In this case, the corresponding event needs to be intercepted. Actually, the wm_pid_state_changed event is called in a corresponding place to restore the cursor icon of the mouse, I think the wm_hbkwin form is a good place. In this way, when wm_pid_state_changed event is generated in wm_handlepid, all events are forwarded to the wm_hbkwin form.
We add our own message:
# Define wm_pid_state_changed_all 45
Add the following code:
If (chwin. hwin! = Wm_hbkwin)
{
Wm_pid_state_changed_info info2;
Info2.x = info. X;
Info2.y = info. Y;
Info2.state = info. State;
Info2.stateprev = info. stateprev;
Wm_message msg2;
Msg2.data. P = & info2;
Msg2.hwinsrc = msg. hwinsrc;
Msg2.msgid = wm_pid_state_changed_all;
Wm_sendmessage (wm_hbkwin, & msg2 );
}
In this way, all the mouse events of the form will be sent to the wm_hbkwin form. We will process them in the callback of the wm_hbkwin form:
Case wm_pid_state_changed_all: // The wm_pid_state_changed event in all forms will be sent here
{
Wm_pid_state_changed_info * Cinfo = (wm_pid_state_changed_info *) (PMSG-> data. P );
// Click the release button once.
If (Cinfo-> state = 0 ){
Dropdevselectid =-1;
Gui_cursor_select (& gui_cursorarrows); // change the default icon
}
Break;
}
Dropdevselectid is a variable used to record information. When you press the mouse on the source form, I use dropdevselectid to record the selected image as follows:
// Drag a button
If (ncode = wm_icationication_clicked ){
If (ID = gui_id_listbox0 ){
Dropdevselectid = getselectpdevid (); // sets the currently selected ID
If (dropdevselectid =-1)
Break;
Gui_cursor_select (& gui_cursorcrossl); // you can change the Mouse icon.
}
}
The above dropdevselectid = getselectpdevid () is to record a selected object ID, so that we can know what to choose when releasing the mouse above.
Now, we have to handle the mouse release event in the target form:
Case wm_pid_state_changed:
// If this variable records an object, it is considered as a drag from another form.
If (dropdevselectid! =-1)
{
...; // Process
}
In this way, the dockdrop function of a window is implemented. Of course, there are multiple dockdrop functions, so we have to deal with the number of variables similar to dropdevselectid. In fact, in an application,
Our mouse has only one drag action at a time point. In this way, we can define a common object for processing. What should we do!
For the Mouse icon, you can define a gui_cursor object and specify your own icon.
The previous code implementation is now available.