ThunderMusic wrote:
Hi,
I have a borderless form and I try to drag it. When I do it, the form start
to jump from position to position even when I don't move the mouse... My
formula seems right, but the result is not... can somebody help me please?
here is the code:
private void managementConsoleBarSmall1_MouseDown(object sender,
MouseEventArgs e)
{
if (!m_Dragging)
{
m_InitialDragPoint = new Point(e.X, e.Y);
}
m_Dragging = true;
}
private void managementConsoleBarSmall1_MouseUp(object sender,
MouseEventArgs e)
{
m_Dragging = false;
}
private void managementConsoleBarSmall1_MouseMove(object sender,
MouseEventArgs e)
{
if (m_Dragging)
{
this.SetBounds(this.Left + (e.X - m_InitialDragPoint.X), this.Top +
(e.Y - m_InitialDragPoint.Y), this.Width, this.Height);
this.m_InitialDragPoint = new Point(e.X, e.Y);
System.Threading.Thread.Sleep(10);
}
}
Someone sees something wrong here?
Quite an entertaining effect, but clearly not what you want. I think
your problems stem from confusion between the two different kinds of
coordinates - *client* coordinates (relative to a control, so (0,0) is
the top left corner) and *screen* coordinates.
This is one way to fix your code:
- Get rid of m_InitialDragPoint, and have a new Point variable called
m_DragPointOffset.
- Change the MouseDown routine to:
{
if (!m_Dragging)
{
// Remember the offset between the point of mousedown
// and the top left of the form
// Note that MouseEeventArgs.X and .Y are *client*
// coordinates, so they are already the numbers we want
m_DragPointOffset = new Point(e.X, e.Y);
m_Dragging = true;
}
}
- Change the MouseMove routine to:
{
if (m_Dragging)
{
// Move the form. We want to move the form to such
// a place that e.Location is the same as
m_DragPointOffset
// (that is, the mouse pointer is at the same relative
// location as when the mouse button was pressed).
// So we do this:
this.SetBounds(this.Left + e.X - m_DragPointOffset.X,
this.Top + e.Y - m_DragPointOffset.Y,
this.Width,
this.Height);
// Note that moving the form will trigger another
// MouseMove, but because the above calculation will
then
// result in the form not moving, we *don't* need
anything
// like this:
//System.Threading.Thread.Sleep(10);
}
}
Note that the way some people implement dragging of borderless forms is
by overriding the WndProc and converting mousedowns to mousedowns in
the caption area. I don't know which way is better, or why.
--
Larry Lard
Replies to group please