Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ThunderMusic - 14 Jul 2006 15:14 GMT
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
ThunderMusic - 14 Jul 2006 15:35 GMT
if it can be of any use, I'm on VS2005, so framework 2.0

> Hi,
> I have a borderless form and I try to drag it.  When I do it, the form
[quoted text clipped - 35 lines]
>
> ThunderMusic
Chris Dunaway - 14 Jul 2006 15:41 GMT
> 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
Larry Lard - 14 Jul 2006 16:01 GMT
> Hi,
> I have a borderless form and I try to drag it.  When I do it, the form start
[quoted text clipped - 30 lines]
>
> 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.

Signature

Larry Lard
Replies to group please

ThunderMusic - 14 Jul 2006 16:13 GMT
Thanks a lot... it works perfectly!! I don't see such a big change, but it
works so that perfect!!  Thanks again...

>> Hi,
>> I have a borderless form and I try to drag it.  When I do it, the form
[quoted text clipped - 88 lines]
> by overriding the WndProc and converting mousedowns to mousedowns in
> the caption area. I don't know which way is better, or why.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.