鼠标双击仅在右键单击时触发

时间:2014-10-04 22:17:20

标签: c# winforms

我想覆盖面板控件上的双击,所以我基本上就这样做了

HeaderPanel.MouseDoubleClick += (sender, args) =>
{
    MaximizeOnClick(sender, args);
};

然而(奇怪的是)它只会在我双击右键并且从不正常双击左键时触发。 ?问题是什么?

更新#1:我认为发生这种情况是因为我还覆盖了MouseDown事件,因为当我删除它时,左键单击是有效的,那么这两个事件之间是否存在某种交互?

更新#2:

const int WM_NCLBUTTONDOWN = 0xA1;
const int HTCAPTION = 0x2;
const UInt32 WM_NCHITTEST = 0x0084;
const UInt32 WM_MOUSEMOVE = 0x0200;
const UInt32 HTLEFT = 10;
const UInt32 HTRIGHT = 11;
const UInt32 HTBOTTOMRIGHT = 17;
const UInt32 HTBOTTOM = 15;
const UInt32 HTBOTTOMLEFT = 16;
const UInt32 HTTOP = 12;
const UInt32 HTTOPLEFT = 13;
const UInt32 HTTOPRIGHT = 14;
const int RESIZE_HANDLE_SIZE = 10;

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

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

protected override void WndProc(ref Message m)
{
    bool handled = false;
    if (m.Msg == WM_NCHITTEST || m.Msg == WM_MOUSEMOVE)
    {
        Size formSize = Size;
        Point screenPoint = new Point(m.LParam.ToInt32());
        Point clientPoint = PointToClient(screenPoint);

        Dictionary<UInt32, Rectangle> boxes = new Dictionary<UInt32, Rectangle>
        {
            {HTBOTTOMLEFT, new Rectangle(0, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTBOTTOM, new Rectangle(RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, formSize.Width - 2*RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTBOTTOMRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2*RESIZE_HANDLE_SIZE)},
            {HTTOPRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTTOP, new Rectangle(RESIZE_HANDLE_SIZE, 0, formSize.Width - 2*RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTTOPLEFT, new Rectangle(0, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)},
            {HTLEFT, new Rectangle(0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2*RESIZE_HANDLE_SIZE)}
        };

        foreach (KeyValuePair<UInt32, Rectangle> hitBox in boxes)
        {
            if (hitBox.Value.Contains(clientPoint))
            {
                m.Result = (IntPtr)hitBox.Key;
                handled = true;
                break;
            }
        }
    }

    if (!handled)
        base.WndProc(ref m);
}

private void HeaderPanelOnMouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}

private void MaximizeOnClick(object sender, EventArgs eventArgs)
{
    if (FormWindowState.Normal == WindowState)
    {
        MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
        WindowState = FormWindowState.Maximized;
    }
    else
    {
        MaximumSize = new Size(0, 0);
        WindowState = FormWindowState.Normal;
    }
    Panel.Select();
}

1 个答案:

答案 0 :(得分:3)

好的我已经通过在鼠标按下之前处理双击事件来修复该问题...基本上改变了这个

private void HeaderPanelOnMouseDown(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left) {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}

到这个

private void HeaderPanelOnMouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && e.Clicks >= 2) {
        MaximizeOnClick(sender, e);
        return;
    }

    if (e.Button != MouseButtons.Left) {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}