Formdragger How to implement the form drag-and-drop device in C #

Source: Internet
Author: User
This article is mainly for you to introduce the C # component Formdragger form drag-and-drop, with a certain reference value, interested in small partners can refer to

applicable:. net2.0+ WinForm Project

Introduced:

Similar to QQ, Thunder and other UI experience of the software, are supported in the window in many places to drag the window, without wussy to the top of the title bar drag, this component is to let the WinForm can also be dragged, with the degree of sexual or even more. First look at the effect:

The places to be dragged include not limited to:

    • Blank areas of form, Panel, GroupBox, TabControl, and other container controls;

    • Blank areas of bar, such as menu bar, toolbar, status bar, and invalid items;

    • Labels, PictureBox, ProgressBar, and so on, usually do not interact with the mouse control;

    • All invalid controls (enabled is false);

It's basically the place you think you should be able to drag.

Usage:

See Public members first:

Drag switch bool Enabled {get; set;} Exclusion list. The control instance can be added to or removed from, and the control in the list does not accept drag list<control> excludecontrols {get;} Event: Occurs when a drag is ready, available E. Cancel = True to cancel drag, E also carry other information event eventhandler<formdraggingcanceleventargs> dragging;//events: Drag-and-drop switch status changed event EventHandler enabledchanged;

Easy to use, anytime, anywhere formdragger.enabled = True/false can open and close the drag function, such as in the main function can be opened, the program of all the self-built form can be happily dragged, but, System-provided forms such as message box MessageBox, various dialog boxes (such as open file dialog boxes) cannot be dragged, and these need to be checked, Not necessary (in fact, the scheme has implemented a DialogDragger.cs, is used to drag the System dialog box, but the known color selection dialog ColorDialog There is a problem, so there is no integration, the back feel necessary and resolved the update, recommended watch). About the message box, you can also choose this, because it is self-made, so you can drag.

for the control that applies drag rules, the left mouse button click on the message (such as MouseDown) is not to it, because it was intercepted, so the registration of such events will not be triggered, if you want a draggable control is not dragged, such as a picture frame, you want it to have "super-chain" function, To execute the registered MouseDown event handling method at the point up, there are two ways to implement the exception:

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 handling method, the incoming E has a control property, which represents the point to the controls, so you can determine whether the E.control you want the exception of the control, if, so E.cancel = True In addition, E also carries other information, such as mouse position, coordinate type and so on for auxiliary judgment;
The above scenes in the source code are examples, 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 it satisfies the logic, block the message, and send the message to the control's form by clicking on the title bar, reaching the effect that the system thinks is point to the title bar of the form when the control is clicked. For details of processing, please go to the address below to view.

Scenario:
Github.com/ahdung/formdraggerdemo "Priority"

Coding.net/u/ahdung/p/formdraggerdemo/git

Git.oschina.net/ahdung/formdraggerdemo

The program has written the tester, that's what it looks like, welcome to the download experience.

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.