Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DefWindowProc调用在Windows 8上不起作用_C#_.net_Windows_Winforms_Windows 8 - Fatal编程技术网

C# DefWindowProc调用在Windows 8上不起作用

C# DefWindowProc调用在Windows 8上不起作用,c#,.net,windows,winforms,windows-8,C#,.net,Windows,Winforms,Windows 8,从C#winform在Windows 8上调用DefWindowsProc时遇到问题。我有一张表格,我需要它可以从表格里面的任何地方拖出 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void mouse_down(object sender, MouseEventArgs e) { Control

从C#winform在Windows 8上调用DefWindowsProc时遇到问题。我有一张表格,我需要它可以从表格里面的任何地方拖出

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void mouse_down(object sender, MouseEventArgs e)
    {
        Control ctrl = sender as Control;
        UnsafeNativeMethods2.ReleaseCapture(ctrl.Handle);
        this.Drag(); // put the form into drag mode.

    }
    public void Drag()
    {
        UnsafeNativeMethods2.DefWindowProc(this.Handle, UnsafeNativeMethods2.WM_SYSCOMMAND, (IntPtr) UnsafeNativeMethods2.MOUSE_MOVE, IntPtr.Zero);
    }
}
这是我的密码

[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始终返回0,但我无法拖动窗口。此调用适用于XP、Vista和7,但不适用于8。我猜这与DefWindowProc在Windows8上运行不好有关


请注意,在Windows 8上,我使用.NET 4.0框架构建应用程序,而在其他平台上,我使用2.0版本构建软件。

我使用稍微不同的方法实现了您的功能

我正在运行Windows 8和Visual Studio 2012

我测试了我的解决方案和所有版本的框架,版本2,3,4,4.5

当然,主窗体需要捕获mouse_down()方法

首先,我定义了一个名为unsafentiveMethods的类:

[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    public static extern int SendMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);

    [DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
    public static extern bool ReleaseCapture();

}
在表单中,您希望使用鼠标从任意位置移动:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void mouse_down(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            UnsafeNativeMethods.ReleaseCapture();
            UnsafeNativeMethods.SendMessage(Handle, UnsafeNativeMethods.WM_NCLBUTTONDOWN, UnsafeNativeMethods.HT_CAPTION, 0);
        }
    }


}

我用稍微不同的方法实现了您的功能

我正在运行Windows 8和Visual Studio 2012

我测试了我的解决方案和所有版本的框架,版本2,3,4,4.5

当然,主窗体需要捕获mouse_down()方法

首先,我定义了一个名为unsafentiveMethods的类:

[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    public static extern int SendMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);

    [DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
    public static extern bool ReleaseCapture();

}
在表单中,您希望使用鼠标从任意位置移动:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void mouse_down(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            UnsafeNativeMethods.ReleaseCapture();
            UnsafeNativeMethods.SendMessage(Handle, UnsafeNativeMethods.WM_NCLBUTTONDOWN, UnsafeNativeMethods.HT_CAPTION, 0);
        }
    }


}

我重新编写了这个示例,以查看DefWindowProc()的行为是否不同。事实并非如此。当拖到远处时,窗体仍将停靠到边缘

然而,代码如下:

[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods2
{
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref int lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32")]
    public static extern int ReleaseCapture(IntPtr hwnd);

    public const int WM_SYSCOMMAND = 0x112;
    public const int MOUSE_MOVE = 0xF012;
}
这是表单中的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void mouse_down(object sender, MouseEventArgs e)
    {
        Control ctrl = sender as Control;
        UnsafeNativeMethods2.ReleaseCapture(ctrl.Handle);
        this.Drag(); // put the form into drag mode.

    }
    public void Drag()
    {
        UnsafeNativeMethods2.DefWindowProc(this.Handle, UnsafeNativeMethods2.WM_SYSCOMMAND, (IntPtr) UnsafeNativeMethods2.MOUSE_MOVE, IntPtr.Zero);
    }
}
我的DefWindowProc()声明与您的稍有不同


总之,这两种方法都可以实现相同的结果—能够从窗体中的任何位置拖动窗口。

我重新编写了示例,以查看DefWindowProc()的行为是否不同。事实并非如此。当拖到远处时,窗体仍将停靠到边缘

然而,代码如下:

[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods2
{
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref int lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32")]
    public static extern int ReleaseCapture(IntPtr hwnd);

    public const int WM_SYSCOMMAND = 0x112;
    public const int MOUSE_MOVE = 0xF012;
}
这是表单中的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void mouse_down(object sender, MouseEventArgs e)
    {
        Control ctrl = sender as Control;
        UnsafeNativeMethods2.ReleaseCapture(ctrl.Handle);
        this.Drag(); // put the form into drag mode.

    }
    public void Drag()
    {
        UnsafeNativeMethods2.DefWindowProc(this.Handle, UnsafeNativeMethods2.WM_SYSCOMMAND, (IntPtr) UnsafeNativeMethods2.MOUSE_MOVE, IntPtr.Zero);
    }
}
我的DefWindowProc()声明与您的稍有不同


总之,这两种方法都可以实现相同的结果—能够从窗体中的任何位置拖动窗口。

这根本不能解决问题,窗口仍然无法移动到屏幕的上边缘之外。这根本不能解决问题,窗口仍然无法移动到屏幕的上边缘之外。