Click here to Skip to main content
15,891,657 members
Articles / Programming Languages / C#

Custom Windows Panel

Rate me:
Please Sign up or sign in to vote.
4.69/5 (20 votes)
15 Mar 2010CPOL 64.8K   1.9K   57   16
Every control in this panel can change the size and move on the panel

Introduction

I've downloaded some articles to use in my project for resizing and moving every control at runtime, but those aren't more efficient in my conditions. So I decided to make a custom panel to do that. It's useful for some projects such as report generator and more.

Using the Code

Canvas class is inherited from Panel control and in the OnControlAdded method of the panel, some events are used to handle moving controls.

Canvas has defined Tracker class to handle resizing the control on the panel.

Just drag and drop the custom panel (named Canvas) on the form and put some controls to test it.

C#
public class Canvas : Panel
{
...
protected override void OnControlAdded(System.Windows.Forms.ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is Tracker)
return;

e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
e.Control.MouseDown += new MouseEventHandler(Control_MouseDown);
e.Control.MouseMove += new MouseEventHandler(Control_MouseMove);
e.Control.MouseUp += new MouseEventHandler(Control_MouseUp);

e.Control.LocationChanged += new EventHandler(Control_LocationChanged);

e.Control.HandleDestroyed += new EventHandler(Control_HandleDestroyed);
}
...
}

History

  • 29th May, 2009: Initial post
  • 15th Mar, 2010: Updated source code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionDoesn't work when scrolled off the screen Pin
MattRhoades2-Nov-11 14:16
MattRhoades2-Nov-11 14:16 
GeneralRe: Doesn't work when scrolled off the screen Pin
chrisbray2-Nov-11 19:59
chrisbray2-Nov-11 19:59 
GeneralRe: Doesn't work when scrolled off the screen Pin
MattRhoades4-Nov-11 5:00
MattRhoades4-Nov-11 5:00 
GeneralRe: Doesn't work when scrolled off the screen Pin
chrisbray4-Nov-11 5:18
chrisbray4-Nov-11 5:18 
GeneralRe: Doesn't work when scrolled off the screen Pin
MattRhoades4-Nov-11 5:36
MattRhoades4-Nov-11 5:36 
GeneralXml integration for object position and size Pin
sleekyworm26-Nov-10 21:58
sleekyworm26-Nov-10 21:58 
GeneralHide Tracker Handles Pin
chrisbray24-Mar-10 13:44
chrisbray24-Mar-10 13:44 
Hi Alireza,

Nice code, thank you for sharing it. I can see a number of possible improvements, but the immediately obvious one is that if you click on the canvas itself any previously selected control appears to remain selected i.e. it continues displaying the tracker handles even when it is not in fact selected. This is simple to solve by overriding the OnMouseDown event of the canvas:

protected override void OnMouseDown(MouseEventArgs e)
{
    tracker.Visible = false;
    base.OnMouseDown(e);
}


I have only had a quick look at the code, and there may be a better way of handling this not least because there is still a problem if you click on a control that is not resizeable... it might be nice to still show a highlight but not implement the handles in that scenario. This could be done by removing the checking code from the Control_MouseDown handler in the Canvas class and just passing the control anyway:

private void Control_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        isMouseDown = true;
        hitPoint = e.Location;

        Control control = (Control)sender;
        tracker.Control = control;
    }
}


Then in the tracker class you can simply modify the OnPaint event handler so that it always draws the highlight but only draws the handles if the control can be resized:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    e.Graphics.Clear(BackColor);
    PropertyInfo info = control.GetType().GetProperty("AutoSize");
    if (info != null && ((bool)info.GetValue(control, null)) == false)
    {
        e.Graphics.FillRectangles(Brushes.White, trackers);
        e.Graphics.DrawRectangles(Pens.Black, trackers);
    }
}


In note that GetProperty is called more than once, so I would suggest a variable called 'resizable' be set when the control is assigned to the tracker which can then be interrogated so that reflection is used only once. There is also an issue with selecting Text Boxes for resizing, and it would be good to have alignment indicators as we do in VS - I will look at these issues when I get time and post a further suggestion if I come up with sensible solutions.

Chris Bray
GeneralRe: Hide Tracker Handles [modified] Pin
Alireza Soleimani26-Mar-10 19:05
Alireza Soleimani26-Mar-10 19:05 
GeneralRe: Hide Tracker Handles Pin
chrisbray26-Mar-10 23:43
chrisbray26-Mar-10 23:43 
GeneralRe: Hide Tracker Handles Pin
Alireza Soleimani28-Mar-10 11:03
Alireza Soleimani28-Mar-10 11:03 
GeneralRe: Hide Tracker Handles Pin
chrisbray28-Mar-10 22:03
chrisbray28-Mar-10 22:03 
GeneralRe: Hide Tracker Handles Pin
MattRhoades2-Nov-11 14:17
MattRhoades2-Nov-11 14:17 
GeneralRe: Hide Tracker Handles Pin
chrisbray2-Nov-11 19:52
chrisbray2-Nov-11 19:52 
QuestionCan't find Canvas in my toolbox???? Pin
sleezy27-Aug-09 13:44
sleezy27-Aug-09 13:44 
GeneralGreat! (VS2008 Required) Pin
jp2code26-Aug-09 8:57
professionaljp2code26-Aug-09 8:57 
GeneralMy vote of 1 Pin
snehalshahprit29-May-09 16:30
snehalshahprit29-May-09 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.