I have a form that includes a panel with buttons that gets generated by code (so they don't exist on the form at start) and placed on the panel. typically about 10-20 buttons but could possibly get up to about 100.
Now I need the user be able to rotate, change shape and dimensions of the buttons individually, as well move them by mouse (got this part working using the buttons). The Button class don't seem to be the way to go so I started reading about shapes, paint events, pictureboxes and the like but I cant figure out a neat way of doing it.
EDIT Ok, so I have gotten somewhere using the paint event but when I move my test button it flickers a lot. this is my code so far:
public class CustomButton
{
Panel panel;
private MyData data;
private Point mouseClickLocation;
private bool selected = false;
private bool moving = false;
int dX = 0;
int dY = 0;
//constructor
public TableGraphics(MyData newData, Panel newPanel)
{
panel = newPanel;
data = newData;
panel.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaintHandler);
panel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDownHandler);
}
//paint eventhandler
public void OnPaintHandler(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(data.ButtonLocation, data.ButtonSize);
using (Pen pen = new Pen(Color.Red, 5))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
//mouse down eventhandler
public void OnMouseDownHandler(object sender, MouseEventArgs e)
{
Point buttonLocation = data.ButtonLocation;
Size buttonSize = data.ButtonSize;
//do we hit this object?
if ((e.X < buttonLocation.X + buttonSize.Width) && (e.X > buttonLocation.X) &&
(e.Y < buttonLocation.Y + buttonSize.Height) && (e.Y > buttonLocation.Y))
{
panel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUpHandler);
panel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveHandler);
selected = true;
mouseClickLocation = e.Location;
}
else
{
//deselect
selected = false;
}
}
//mouse up eventhandler
public void OnMouseUpHandler(object sender, MouseEventArgs e)
{
if (selected)
{
selected = false;
moving = false;
panel.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.OnMouseUpHandler);
panel.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveHandler);
}
}
//mouse drag eventhandler
public void OnMouseMoveHandler(object sender, MouseEventArgs e)
{
if(selected && moving)
{
data.ButtonLocation = new Point(e.X + dX, e.Y + dY);
panel.Invalidate();
}
else if (selected && !moving)
{
dX = e.X - mouseClickLocation.X;
dY = e.Y - mouseClickLocation.Y;
int distance = ((dX * dX) + (dY * dY));
if(distance > 15)
{
dX = data.ButtonLocation.X - e.X;
dY = data.ButtonLocation.Y - e.Y;
moving = true;
}
}
}
}
The CustomButton is created from a form and fed a data class that is stored elsewhere and a reference to the panel on the form, one time for each data object that exist (so far just 1 for testing purposes)