Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi ...
i use this code to move Form with Event Mouse_Down :

C#
DllImport("user32.dll")]
static extern int ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int wMsg, int wParam, object lParam);
private const int WM_NCLBUTTONDOWN = 161;
.
.
.

private void button7_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle.ToInt32(), WM_NCLBUTTONDOWN, 2, 0);
}


But after Mouse_Up ... Error :

http://s5.picofile.com/file/8141136026/Untitl34reed.png
Posted
Comments
BillWoodruff 17-Sep-14 3:49am    
Why are you using an API call to move a Windows Form in the first place, rather than using the Form in its standard movable condition ? Are you using a Form you've configured to have no TitleBar ?

First of all, there is no such event. The event is MouseDown.

Now, your big mistake is using P/Invoke. Using it without the real need only breaks the platform compatibility of your application. Whenever possible, you need to use only the pure .NET FCL. In your case, this is not only possible, but quite trivial. You need to use three events:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseup(v=vs.110).aspx[^].

Alternatively, you can override three matching virtual methods:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousedown(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousemove(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmouseup(v=vs.110).aspx[^].

Isn't it obvious? You need to handle MouseMove to do actual motion; MouseDown and MouseUp are used to turn on/off your "moving mode".

—SA
 
Share this answer
 
Comments
george4986 17-Sep-14 23:17pm    
nice sharing my +5v ;-)
Sergey Alexandrovich Kryukov 17-Sep-14 23:30pm    
Thank you; it feels like 5kV :-)
—SA
I have already found a solution with explanation posted by SA.
below is a working sample for reference

declare these variables globally
C#
int _X, _Y;
bool _Drag = false;


declare these events on OnLoad
C#
this.MouseUp += new MouseEventHandler(Form12_MouseUp);
this.MouseDown += new MouseEventHandler(Form12_MouseDown);
this.MouseMove += new MouseEventHandler(Form12_MouseMove);



code for events

C#
void Form12_MouseMove(object sender, MouseEventArgs e)
      {
          if (_Drag == false)
              return;
          Point pCurent = Location;
          Location = new Point(Location.X + e.X - _X, Location.Y + e.Y - _Y);
      }

      void Form12_MouseDown(object sender, MouseEventArgs e)
      {
          _Drag = true;
          _X = e.X;
          _Y = e.Y;
      }

      void Form12_MouseUp(object sender, MouseEventArgs e)
      {
          _Drag = false;
      }


good luck ;-)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900