Home>

Be applicable:.net2.0 + winform project

Introduction:

Similar to qq, Thunder and other software that pay attention to ui experience,Both support dragging windows in multiple places within the window,Instead of dragging to the top title bar honestly,This component allows winform to drag and drop like this,Randomness or worse.First look at the effect:

The places that can be dragged include, but are not limited to:

Blank areas of container controls such as forms, panels, groupbox, and tabcontrol; Blank area of ​​bar such as menu bar, toolbar, status bar,And invalid items; Controls such as label, picturebox, progressbar, etc. that usually do not interact with the mouse; All invalid controls (enabled is false);

Basically, you can drag anywhere you think it should.

usage:

First look at public members:

//Dragger switch
bool enabled {get;set;}
//Exclude list.
You can add or remove control instances to it,Controls in the list do not accept dragging
list<control>excludecontrols {get;}
//Event:Occurs when ready to drag,Cancel dragging with e.cancel=true,e also carries other information
event eventhandler<formdraggingcanceleventargs>dragging;
//Event:After the status of the drag switch is changed
event eventhandler enabledchanged;

It's easy to use,Anytime, anywhereformdragger.enabled=true/falseYou can turn dragging on and off.For example, you can open it in the main function,After all the self-built forms in the program can be dragged happily,However, forms provided by the system, such as message boxes, message boxes, and various dialog boxes (such as open file dialog boxes), cannot be dragged.Drag these need hooks,Can't make it (in fact, a dialogdragger.cs has been implemented in the program, which is used to drag the system dialog box,However, there is a problem with the colordialog of the color selection dialog box.So it is not integrated for the time being,I feel it is necessary and resolved later to update,It is recommended to watch). About message boxes,You can also choose this,Because it is self-made,So you can drag.

For controls that apply drag rules,Left mouse click on a message (such as mousedown) cannot reach it,Because it was blocked,So registering this kind of event will not trigger,To prevent a draggable control from being dragged,Such as a picture frame,You want it to have the function of "hyper-chain". When you click it, the registered mousedown event processing method is executed.Then there are two ways to implement exceptions:

1. Add the control to the exception list:formdragger.excludecontrols.add (picturebox1);//Note that the control instance is accepted here,Instead of control type;

2. Register the formdragger.dragging event. In the event processing method,The e passed in has a control property, which represents the point-to-point control.So you can determine if e.control is the control i want to exception,If so, let e.cancel=true;in addition e also carries other information,Such as mouse position and coordinate type for auxiliary judgment;

The above scenarios have examples in the source code,for your reference.

principle:

Use application.addmessagefilter to add a message filter to the program,Intercept and process the left mouse click message sent to the program form,If the logic is satisfied,Stopped the message,And send a message that clicks the title bar to the form where the control is located,When the control is clicked, the effect that the system thinks is to click on the title bar of the form.For details on processing,Please visit the address below.

Solution:

https://github.com/ahdung/formdraggerdemo[Priority]

c
  • Previous Mobile terminal uses localResizeIMG4 to compress images
  • Next C # Basics-Variables