Windows Develop Bookmark and Share   
 index > Windows Forms General > Movable controls at runtime, performance issue
 

Movable controls at runtime, performance issue

I have a panel with 6 buttons on it. I would like to make it so that the user can move the panel with the buttons on it.

I have the code here for the mouse events. The problem that I am having is that when I move the panel it does not redraw fast enough, I can see the blank spaces behind the buttons on the panel. I have enabled double buffering on the form that the panel is on.

I would like the user to be able to move the panel smoothly.



Code Snippet

private void panel_MouseDown(object sender, MouseEventArgs e)

{


int xOffset = e.X;
int yOffset = e.Y;

if (e.Button == MouseButtons.Left)
{
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}

private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Refresh();
Control active = (Control)sender;
active.Left += e.X - mouseOffset.X;
active.Top += e.Y - mouseOffset.Y;

active.Refresh();

}
}

private void panel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}


}

taijin  Tuesday, July 01, 2008 7:40 PM
Maybe this will help you.

Lior.
Lior Salem  Wednesday, July 02, 2008 4:51 PM

Hi Taijin,

I have a good idea inimplementing this function and move the panel smoothly. Here is my code and step:

1. In the form you should using namespace: "using System.Runtime.InteropServices;"

2. Copy the code below to the form:

Code Snippet

publicconst int SC_CLOSE = 0xF060;

public const int WM_SYSCOMMAND = 0x0112;

public const int WM_NCLBUTTONDOWN = 0xA1;

public const int HTCAPTION = 0x2;

[DllImport("User32.dll")]

public static extern bool ReleaseCapture();

[DllImport("User32.dll")]

public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

Code Snippet

void panel1_MouseDown(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

ReleaseCapture();

SendMessage(this.panel1.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);

}

}

Wish this solution will really help you.

Myproduct  Friday, July 04, 2008 9:51 AM
Take a look at this thread and see if it helps to give you some ideas.

Rudedog
Rudedog2  Tuesday, July 01, 2008 8:02 PM
I took a look at the thread. I already have the code for moving the control at runtime. The problem I am having is with performance. Thanks though.
taijin  Tuesday, July 01, 2008 8:09 PM
Try turning off the Visible property on the controls populating the panel while it is being moved.
Rudedog2  Tuesday, July 01, 2008 8:12 PM
I would very much like to have the control visible while draging.

Though if I can not get this to work correctly I may have to resolve to making the control not visible and draw an out line of the control while it is moving. Thanks for the suggestion.
taijin  Tuesday, July 01, 2008 8:24 PM

Since you're moving the panel(position on parent control changes)you'll need to do a refresh on the parent. This will give correct visual effects but is even slower.

To boost things up, try the following:

* when you start moving

- Create a bitmap of the panelcontrol

- set visibility of the panel to false (no PerformLayouts on the parent)

- Create a bitmap of the parentcontrol (with the panel invisible)

- set a flag for special drawing mode

* In the OnPaint of the parent, build in a switch between normal painting and painting the movement (special drawing mode)

* When painting the movement (every mouseMovement):

- use the bitmaps to draw the canvas. This is a lot faster. You can even use clipping. Not the whole canvas will need a redraw.

* when the movement is about to finish:

- reset the flag

- set visibility of the panel back to true

Mark Verbraeken2  Wednesday, July 02, 2008 2:47 PM
Maybe this will help you.

Lior.
Lior Salem  Wednesday, July 02, 2008 4:51 PM
thanks for the suggestions. As a workaround I used a picturebox instead of a panel. I am getting better performance, though not perfect. the code project code looks promising though I need to move only certain controls. I am new to c# so I don't know about making bitmaps of the controls, that seems like a lot of work. I think using a picture box is good enough for my purposes.
taijin  Thursday, July 03, 2008 7:51 PM

Hi Taijin,

I have a good idea inimplementing this function and move the panel smoothly. Here is my code and step:

1. In the form you should using namespace: "using System.Runtime.InteropServices;"

2. Copy the code below to the form:

Code Snippet

publicconst int SC_CLOSE = 0xF060;

public const int WM_SYSCOMMAND = 0x0112;

public const int WM_NCLBUTTONDOWN = 0xA1;

public const int HTCAPTION = 0x2;

[DllImport("User32.dll")]

public static extern bool ReleaseCapture();

[DllImport("User32.dll")]

public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

Code Snippet

void panel1_MouseDown(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

ReleaseCapture();

SendMessage(this.panel1.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);

}

}

Wish this solution will really help you.

Myproduct  Friday, July 04, 2008 9:51 AM

You can use google to search for other answers

Custom Search

More Threads

• PropertyGrid and DateTime format.
• getting users againest LDAP group
• get selected index of a ComboBox column in a DataGridView
• Customizing WinForms ErrorProvider to display it’s icon inside control’s entry
• Inheriting the contextmenustrip problem
• TableLayoutPanel formatting question.
• Ellipsis at design time on a winform - possible ?
• Is there any practical material about Application Configuration Management?
• Form to Form
• Change printer settings in c#