Hello, for a long time using several methods, I will give the code below. Perhaps there is a more humane solution to this problem, share the code.

[DllImport("user32", CharSet = CharSet.Auto)] internal extern static bool PostMessage(IntPtr hWnd, uint Msg, uint WParam, uint LParam); [DllImport("user32", CharSet = CharSet.Auto)] internal extern static bool ReleaseCapture(); const uint WM_SYSCOMMAND = 0x0112; const uint DOMOVE = 0xF012; const uint DOSIZE = 0xF008; 

And we insert into the MouseDown event of the control. code.

 ReleaseCapture(); PostMessage(this.Handle, WM_SYSCOMMAND, DOMOVE, 0); 

This option works fine, without lags, and flicker, maybe there is a better solution?

  • one
    and what you do not like this decision if it works? WinForms - a shell over WinAPI, you use WinAPI import to manage WinAPI, what is the inhumanity? There is an option, of course, based on the descending event routing (described already somewhere here ), only the code in this solution is much larger and the error probability is higher, do you need it? - rdorn
  • You have a plus from me, the lack of my solution is that there are often overloaded interfaces, in which case you have to create a MouseDown event for 5-10 elements. Naturally I create a method and insert it, I don’t like exactly the mountain of the same type of events. (I may have senile insanity) ... and maybe there are young advanced ones with better ideas. - Digital Core
  • one
    On WinForms, you can think of nothing better, at least in this aspect, for sure. WPF out-of-the-box event routing is able, but it is far from WinAPI and runs on top of DirectX. And here, for a hundred years, nothing has changed, third-party frameworks like DevExpress, too, do not change anything, they just add their cockroaches along with ryushechkami and beautiful. There, everything depends on the implementation of WinAPI, if you can do something, but it was not done - they finish it in third-party frameworks, if it is not possible - it means it cannot. - rdorn

1 answer 1

Dragging - The desired item is subscribed to the mouse button event MouseLeftButtonDown and in the handler we call DragMove();

 public void DragWindow(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) DragMove(); } 

The answer is taken from: Implementing standard buttons and behavior

  • In the WinForms issue, you gave a link to WPF. - Alexander Petrov
  • @AlexanderPetrov so all the commands written in that answer can be used in the Winforms code, no? - Andrew
  • @Andrew - not. WinForms, at least, does not have the DragMove method. - Alexander Petrov