简体   繁体   English

C#拖动n放置,在mouse_down中开始拖动操作? 与点击冲突

[英]C# drag n drop, start drag operation in mouse_down ? conflicts with click

I have a C# WinForms application with a GridView on a Form wich shows records from the database containing blobs (files PDF/JPG/etc.) stored in a database. 我有一个C#WinForms应用程序,该应用程序在窗体上具有GridView,可显示数据库中包含存储在数据库中的Blob(PDF / JPG /等文件)的记录。

I can doubleclick on a row in the grid, to write the blob to disk and open the file. 我可以双击网格中的一行,将blob写入磁盘并打开文件。 I can single-click on rows to select one or more rows (using ctrl+shift) I can drag files onto the grid to add the files as rows to the grid (and database) 我可以单击行以选择一个或多个行(使用ctrl + shift),也可以将文件拖到网格上以将文件作为行添加到网格(和数据库)中

Now I want the user to be able to drag one or more rows from the grid to, for instance, the desktop or a mailclient but cannot figure out in what event to start the 'drag' operation. 现在,我希望用户能够将网格中的一个或多个行拖动到例如桌面或邮件客户端,但无法确定在什么情况下才能开始“拖动”操作。

When a user selects one or more file he/she does that using the left-mousebutton, dragging uses the same left-mousebutton, both events trigger the mouse-down event. 当用户选择一个或多个文件时,他/她使用鼠标左键进行操作,拖动使用相同的鼠标左键,这两个事件都会触发鼠标按下事件。 How does one determine what the user is about to do? 如何确定用户将要做什么?

I have tried starting the drag operation in the mouse-down event, but that doesn't work if I want to select multiple rows, every time I click on a row a drag operation starts... 我曾尝试在mouse-down事件中启动拖动操作,但是如果我要选择多行,那将不起作用,每当我单击一行时,拖动操作就会开始...

how is that handled in the windows explorer for instance? 例如,在Windows资源管理器中如何处理? How does one detect what the user is trying to do? 如何检测用户正在尝试做什么?

I have got it working now. 我现在开始工作了。 However I did not use a timer as suggested. 但是,我没有使用建议的计时器。

In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation. 在Mouse-Down中,我设置一个标志并存储X,Y点,然后在Mouse-Up中,我重置该标志;在Mouse-move中,我基于所存储的X,Y点数计算运动,当X或Y中的运动超过10像素时方向我开始拖动操作。

Here's the code. 这是代码。

' '

    private bool DraggingFromGrid = false;
    private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point(  );

    void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            DraggingFromGrid = true;
            DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);
        }
    }

    void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)
    {
        if (DraggingFromGrid)
        {
            DraggingFromGrid = false;
        }
    }

    void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)
    {
        if (DraggingFromGrid)
        {
            if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10 ||
                System.Math.Abs(e.Y - DraggingStartPoint.Y) > 10)
            {
                StartDragging();
            }
        }
    }

    private void StartDragging()
    {
        DraggingFromGrid = false;

        // create files
        var _criteria = this.GetSelectionFromGrid();
        var _files = new List<string>();

        ... retrieve filenames and store in _files List ...

        var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

        DoDragDrop(_data, DragDropEffects.Copy);
    }

' '

there is more then one way to do that. 还有更多的方法可以做到这一点。 I strongly suggest you try some of them until you decide what's best for you. 我强烈建议您尝试其中的一些,直到您决定最适合自己的为止。 for example, you can use the mouse_down event to start the drag and drop by using a timer. 例如,您可以使用mouse_down事件通过计时器来开始拖放。 if the user is clicking the mouse more then 0.5 seconds, u start the drag. 如果用户单击鼠标超过0.5秒,则开始拖动。 the mouse_up event kill the drag/dropping. mouse_up事件会终止拖放。 another way is making the drag and drop only with some key pressed while clicking the mouse. 另一种方法是仅在单击鼠标时按下某些键进行拖放。

I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. 我有一个类似的问题,就是在表单中添加拖动会干扰现有的双击功能。 I found a very simple solution: 我找到了一个非常简单的解决方案:

void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Clicks != 2)
    {
        StartDragging();
    }
}

This cancels dragging if there is a double click so that double clicking continues to work. 如果有双击,则取消拖动,以便双击继续起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM