防止网格向下滚动鼠标

时间:2014-01-29 19:30:49

标签: c# .net visual-studio infragistics ultragrid

当用户的鼠标左键按下并移动时,如何阻止Windows(7/8)滚动(触摸事件?)Infragistics UltraGrid?这种行为搞砸了我的拖拽事件。

我正在使用UltraGrid来接收并发起拖拽。当用户单击网格中的项目时,我将变量leftButton设置为true,因此我会阻止将项目放在自身上。我正在使用MouseLeaveElement从网格中启动DoDragDrop DragDrop

代码如下。它在Windows XP中有效,但在Windows 7和8中失败。当鼠标左键被释放时,现在正在调用mainGrid_MouseLeaveElement,这是因为拖动应该已经完成​​的时候才会被调用。当鼠标左键移动并且鼠标移动时,看起来Windows操作系统正在接管。当释放鼠标按钮时,它会将其释放回应用程序。

private leftMouseDown = false;

public void Fill(ToolbarForm ownerForm, DocumentOwner owner, int? ownerIdentifier)
{
    ...
    this.mainGrid.DragDrop += new DragEventHandler(grid_DragDrop);
    this.mainGrid.DragEnter += new DragEventHandler(grid_DragEnter);
    this.mainGrid.MouseDown += new MouseEventHandler(mainGrid_MouseDown);
    this.mainGrid.MouseUp += new MouseEventHandler(mainGrid_MouseUp);
    this.mainGrid.MouseLeaveElement += new Infragistics.Win.UIElementEventHandler(mainGrid_MouseLeaveElement);
    ...
}

void mainGrid_MouseLeaveElement(object sender, Infragistics.Win.UIElementEventArgs e)
{
    if (leftMouseDown)
    {
        ...
        DataObject data = new DataObject();
        data.SetFileDropList(files);
        this.mainGrid.DoDragDrop(data, DragDropEffects.Copy);
        leftMouseDown = false;
    }
}

void mainGrid_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        leftMouseDown = false;
}

void mainGrid_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        leftMouseDown = true;
}

public void grid_DragDrop(object sender, DragEventArgs e)
{
    string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);

    foreach (string filename in filenames)
    {
        this.AddDocument(filename);
    }
}

private void grid_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop, false) && !leftMouseDown)
    {
        e.Effect = DragDropEffects.All;
    }

    else e.Effect = DragDropEffects.None;
}

1 个答案:

答案 0 :(得分:1)

通过在maingrid而不是MouseLeaveElement上使用SelectionDrag事件解决了这个问题。

void maingrid_SelectionDrag(object sender, Infragistics.Win.UIElementEventArgs e)
{
    if (leftMouseDown)
    {
        ...
        DataObject data = new DataObject();
        data.SetFileDropList(files);
        this.mainGrid.DoDragDrop(data, DragDropEffects.Copy);
        leftMouseDown = false;
    }
}

而不是

void mainGrid_MouseLeaveElement(object sender, Infragistics.Win.UIElementEventArgs e)
{
    if (leftMouseDown)
    {
        ...
        DataObject data = new DataObject();
        data.SetFileDropList(files);
        this.mainGrid.DoDragDrop(data, DragDropEffects.Copy);
        leftMouseDown = false;
    }
}