Home > Article > Backend Development > Detailed explanation of how FormDragger implements a form dragger in C#
This article mainly introduces the C# component FormDragger in detail, which has certain reference value. Interested friends can refer to it
Applicable: .net2.0+ winform project
Introduction:
Software that emphasizes UI experience such as QQ and Thunder supports dragging in multiple places within the window. You can move the window without having to go to the top title bar and drag it. This component allows Winform to drag and drop it casually, or even more casually. Take a look at the effect first:
The places that can be dragged include but are not limited to:
Containers such as forms, Panels, GroupBox, TabControl etc. The blank area of the control;
The blank area of the menu bar, toolbar, status bar and other bars, as well as invalid items;
Label, PictureBox , ProgressBar and other controls that usually do not interact with the mouse;
All invalid controls (Enabled is false);
Basically You can drag it anywhere you think it should be towable.
Usage:
Look at the public members first:
//拖拽器开关 bool Enabled { get; set; } //排除列表。可向其中添加或移除控件实例,处于列表中的控件不接受拖拽 List<Control> ExcludeControls { get; } //事件:准备拖拽时发生,可用e.Cancel = true取消拖拽,e还携带其它信息 event EventHandler<FormDraggingCancelEventArgs> Dragging; //事件:拖拽器开关状态改变后 event EventHandler EnabledChanged;
It is very simple to use, anytime and anywhereFormDragger.Enabled = true/false can turn on and off the drag and drop function. For example, it can be turned on in the Main function. After finishing all the self-built forms in the program, you can happily drag and drop. However, like the message box MessageBox, various dialog boxes (such as the file open dialog box) and other forms provided by the system cannot be dragged. Dragging these requires hooks, which is not possible (in fact, a DialogDragger.cs has been implemented in the plan, which is used to drag system dialog boxes. , but it is known that there is a problem with the color selection dialog box ColorDialog, so it is not integrated for the time being. I will update it later if it is necessary and solved. It is recommended to watch). Regarding the message box, you can also choose this. Since it is homemade, it can be dragged.
For controls that apply drag and drop rules, the left mouse button click message (such as MouseDown) cannot reach it. Because it was intercepted, this was registered. Class events will not be triggered. If you want a draggable control not to be dragged, such as a picture box, and you want it to have the function of "hyperlink" and execute the registered MouseDown event processing method when you click on it, then there is There are two ways to achieve exceptions:
1. Add the control to the exception list: FormDragger.ExcludeControls.Add(pictureBox1);//Note that the control instance is accepted here, not the control type;
2 . Register the FormDragger.Dragging event. In the event processing method, the incoming e has a Control attribute, which represents the clicked control, so you can determine whether e.Control is the control you want to exception. If so, set e.Cancel = true. Yes; in addition, e also carries other information, such as mouse position, coordinate type, etc. for auxiliary judgment;
There are examples of the above scenarios in the source code for your reference.
Principle:
Use Application.AddMessageFilter to add a message filter to the program, intercept and process the left-click message sent to the program form, If the logic is met, the message will be intercepted and the message of clicking the title bar will be sent to the form where the control is located, so that when the control is clicked, the system will think that the title bar of the form is clicked. For processing details, please go to the address below.
The solution is:
github.com/ahdung/FormDraggerDemo [Priority]
coding.net/u/ahdung/p/FormDraggerDemo/git
git.oschina .net/ahdung/FormDraggerDemo
The tester has been written in the solution, just like the screenshot. You are welcome to download and experience it.
The above is the detailed content of Detailed explanation of how FormDragger implements a form dragger in C#. For more information, please follow other related articles on the PHP Chinese website!