Custom form drag, does not work... why?

T

ThunderMusic

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?

thanks

ThunderMusic
 
C

Chris Dunaway

ThunderMusic said:
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?

I don't know why your code is behaving the way it is, but try this code
in your form:

protected override void WndProc(ref Message m)
{
const int WmNcHitTest = 0x84;
const int HtCaption = 2;

if (m.Msg == WmNcHitTest)
m.Result = new IntPtr(HtCaption);
else
base.WndProc(ref m);
}

With the code above you can click anywhere of the form and move the
form.

If you have panels or other controls on the form and you want to move
the form by clicking on them you can use this code (uses a panel but
should work on other controls):

private void pnlAdmin_MouseDown(object sender, MouseEventArgs
e)
{
const uint WM_SYSCOMMAND = 0x0112;
const uint SC_MOVE = 0xF010;
const int HtCaption = 2;

pnlAdmin.Capture = false;

SendMessage(this.Handle, WM_SYSCOMMAND, new IntPtr(SC_MOVE
+ HtCaption),new IntPtr(0));
}

Hope this helps
 
L

Larry Lard

ThunderMusic said:
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.
 
T

ThunderMusic

Thanks a lot... it works perfectly!! I don't see such a big change, but it
works so that perfect!! Thanks again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top