I am having a problem calling DefWindowsProc on Windows 8 from a C# winform. I have this form that I need it to be dragabble from anywhwere inside the form.
Here is my code.
[DllImport("user32.dll")]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool ReleaseCapture(IntPtr hwnd);
const uint WM_SYSCOMMAND = 0x112;
const uint MOUSE_MOVE = 0xF012;
public void Drag()
{
DefWindowProc(this.Handle, WM_SYSCOMMAND, (UIntPtr)MOUSE_MOVE, IntPtr.Zero);
}
private void OnMainPanelMouseDown(object sender, MouseEventArgs e)
{
Control ctrl = sender as Control;
ReleaseCapture(ctrl.Handle);
this.Drag(); // put the form into drag mode.
}
DefWindowProc always return 0 yet I am unable to drag my window. This call works on XP, Vista and 7 but not on 8. I am guessing it has something to do with the decleration of DefWindowProc that is not working well on Windows 8.
Please note that on Windows 8 I am building my application with the .NET 4.0 framework yet on other platforms I am using the 2.0 version to build the software.
I achieved your functionality with a slightly different approach.
I’m running Windows 8 and Visual Studio 2012.
I tested my solution an all versions of the framework, versions 2, 3, 4, 4.5.
Of course your main form will need to trap the mouse_down() method.
First I defined a class called UnsafeNativeMethods:
Inside the form you wish to move with mouse from anywhere: