简体   繁体   中英

Catching mouse move

Situation :

I'm currently working a project where the aim is to develop a VS-like IDE, where users can drap/drop new controls to a design surface, and modify properties of these controls.

So i implemented IDesignerHost, IServiceContainer, IContainer, IComponentChangeService, and some others useful interface, made to design that.

Everything works fine, i've my toolbox, my design surface, and my propertyGrid working just fine.

Problem is :

Attached to the drag'n'droped controls is a label, which has to follow the control while the user move it with his mouse .

I tried to use the LocationChanged event of the controls, to move the label when the control move. But this event occurs only one time, after the control has moved so the label doesn't move while the control move .

I'm not able to find a way for make this work. Does anyone have any good ideas please ?

Thank you

Edit : I use a custom class, implementing IDesignerHost. Controls on this design surface doesn't fire events Mouse----- (eg : MouseDown, MouseMove).

I finally found how to do it :

I implemented ISelectionService and in the SetSelectedComponents function, I managed to select the label control associated with any selected control.

I overrided the designer of the label, so that no border/resize-rectangle would show when the label is selected.

This is a not very elegant solution, but it works well =).

Every Control has a ControlDesigner , provides additional methods to support extending and altering the behavior of an associated Control at design time.

In the ControlDesigner, you have a BehaviorService , which is responsible to control on DesignSurface behavior of the control.

BehaviorService has multiple Glyph s and Adorner s which are like UI decorators for the control. The control re-size rubber-band like rectangle is a Glyph, called System.Windows.Forms.Design.Behavior.SelectionBorderGlyph a private class to .Net 2.0.

This links might be of help:

http://msdn.microsoft.com/en-us/library/ms171820.aspx

http://msdn.microsoft.com/en-us/library/bb514670%28VS.90%29.aspx

You should be able to add your custom Glyph which has a Label attached with the Control.

HTH

Form2 contains a panel1, label1

panel1.MouseMove += panel1_MouseMove
panel1.MouseDown += panel1_MouseDown

when MouseDown+Left Button clicked -> save initial mouse position when MouseMove+Left Button clicked -> move (panel1+label1) by the difference between current mouse position and the saved initial position.

it's done.

public partial class Form2 : Form
{
    private int _x, _y;

    public Form2()
    {
        InitializeComponent();
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            panel1.Location = new Point(panel1.Location.X + (e.X - _x), panel1.Location.Y + (e.Y - _y));
            label1.Location = new Point(label1.Location.X + (e.X - _x), label1.Location.Y + (e.Y - _y));
        }
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _x = e.X;
            _y = e.Y;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM