简体   繁体   English

防止网格在鼠标向下滚动

[英]Prevent grid from scrolling on mouse down

How can I prevent Windows (7/8) from scrolling (touch events?) an Infragistics UltraGrid when the user's left mouse button is down and moving? 当用户的鼠标左键按下并移动时,如何防止Windows(7/8)滚动(触摸事件?) Infragistics UltraGrid This behavior is messing up my dragdrop events. 此行为使我的拖放事件变得混乱。

I'm using an UltraGrid to receive and initiate dragdrops. 我正在使用UltraGrid接收并启动拖放。 When the user clicks an item in the grid I set a variable leftButton to true so I prevent dropping the item on itself. 当用户单击网格中的项目时,我将变量leftButton设置为true,因此可以防止将该项目放到自身上。 I'm using the MouseLeaveElement to start DoDragDrop the DragDrop from within the grid. 我正在使用MouseLeaveElement从网格内部启动DoDragDrop DragDrop

The code is below. 代码如下。 It worked in Windows XP, but fails in Windows 7 and 8. mainGrid_MouseLeaveElement is now being called when the left mouse button is released and that's to late because that's when the drag should have finished. 它在Windows XP中工作,但在Windows 7和8中失败。现在,释放鼠标左键时将调用mainGrid_MouseLeaveElement ,这已经很晚了,因为拖动应该已经完成​​。 It looks like Windows OS is taking over when the left mouse is down and the mouse is being moved. 按下鼠标左键并移动鼠标时,Windows OS似乎正在接管。 It releases it back to the application when the mouse button is released. 释放鼠标按钮时,它将释放回应用程序。

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;
}

Solved this problem by using the SelectionDrag event on the maingrid instead of MouseLeaveElement. 通过在maingrid上使用SelectionDrag事件而不是MouseLeaveElement解决了此问题。

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;
    }
}

instead of 代替

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;
    }
}

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

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