views:

401

answers:

3

Hello,

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----- (e.g. : MouseDown, MouseMove).

A: 

Hello,

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;
        }
    }
}
najmeddine
Sorry but doesn't work on my project. I use a custom class, implementing IDesignerHost. Controls on this design surface doesn't fire events Mouse----- (e.g. : MouseDown, MouseMove).
Clement Herreman
+1  A: 

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 =).

Clement Herreman
+1  A: 

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 Glyphs and Adorners 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

nils_gate
Thank you for the suggestion, even though I'm not working on this project anymore. I'll take a look at it =)
Clement Herreman