在运行时拖动时控制闪烁

时间:2017-05-18 14:33:44

标签: c# button tabs drag flicker

Stack Overflow用户。在C#VS 2010 Windows窗体项目中,我在运行时在标签页上拖动用户创建的控件时遇到控制闪烁的问题。我使用了以下代码:

private void control_MouseMove(object sender, MouseEventArgs e)         
{              
    if (isDragged)             
    {                 
        Point newPoint = ((Control)sender).PointToScreen(new Point(e.X, 
        e.Y));                 
        newPoint.Offset(ptOffset);                 
        ((Control)sender).Location = newPoint;                 
        ((Control)sender).Refresh();             
    }         
}          
private void control_MouseDown(object sender, MouseEventArgs e)         
{                           
    if (e.Button == MouseButtons.Left)             
    {                 
        isDragged = true;                 
        Point ptStartPosition = ((Control)sender).PointToScreen(new 
        Point(e.X, e.Y));                  
        ptOffset = new Point();                 
        ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;                 
        ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;             
    }             
    else             
    {                 
        isDragged = false;             
    }          
}          
private void control_MouseUp(object sender, MouseEventArgs e)         
{             
    ((Control)sender).Refresh();             
    isDragged = false;         
}
private void createButton_PB_Click(object sender, EventArgs e)
{
    int ctrlExists = 0;
    string btnName = btnName_TB.Text;

    foreach (Button button in tabControl1.SelectedTab.Controls)
    {
        if (button.Text == btnName)
        {
            ctrlExists = 1;
        }
    }
    if (btnName_TB.Text != "" && ctrlExists == 0)
    {               
        Button newButton = new Button();
        newButton.Name = btnName.Replace(" ", String.Empty);
        newButton.Name += "u";
        newButton.Text = btnName;
        tabControl1.SelectedTab.Controls.Add(newButton);
        newButton.Left = 10;
        newButton.Top = 420;  
        lastBtnClicked = newButton;
    }
    SetupClickEvents(tabControl1.SelectedTab);
}

所以,问题是我可以添加一个按钮并在运行时拖动它。但是,当我添加另一个按钮并拖动它...在我完成之后,然后返回尝试拖动第一个按钮时,该按钮会闪烁并且就像它试图在整个地方移动一样。有时它会消失。我觉得这与控件位于标签页内的事实有关。也许我没有正确计算“newPoint”变量。任何想法的家伙?

1 个答案:

答案 0 :(得分:0)

好的,所以我发现了一些与按钮创建和创建时添加的事件相关的基本缺陷。我做了一些相当大的改变,这个问题似乎已经消失了。以下是更新的代码。

    private void control_MouseMove(object sender, MouseEventArgs e)
    { 
        if (isDragged)
        {
            Point newPoint = ((Control)sender).PointToScreen(new Point(e.X, 
            e.Y));
            newPoint.Offset(ptOffset);
            ((Control)sender).Location = newPoint;
            ((Control)sender).Refresh();
        }
    }

    private void control_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && checkBox1.Checked)
        {
            isDragged = true;

            ((Control)sender).MouseMove += new 
            MouseEventHandler(control_MouseMove);

            Point ptStartPosition = ((Control)sender).PointToScreen(new 
            Point(e.X, e.Y));

            ptOffset = new Point();
            ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
            ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
        }
        else
        {
            isDragged = false;
        }

    }

    private void control_MouseUp(object sender, MouseEventArgs e)
    {
        ((Control)sender).MouseMove -= control_MouseMove;
        ((Control)sender).Refresh();
        isDragged = false;
    }

    private void SetupClickEvents(Control control)
    {            
        control.Click += new EventHandler(StoreLastClick);
        control.MouseDown += new MouseEventHandler(control_MouseDown);
        //control.MouseMove += new MouseEventHandler(control_MouseMove);
        control.MouseUp += new MouseEventHandler(control_MouseUp);            
    }



    private void createButton_PB_Click(object sender, EventArgs e)
    {
        ctrlExists = 0;
        string btnName = btnName_TB.Text;

        foreach (Button button in tabControl1.SelectedTab.Controls)
        {
            if (button.Name == btnName)
            {
                ctrlExists = 1;
            }
        }
        if (btnName_TB.Text != "" && ctrlExists == 0)
        {               
            Button newButton = new Button();
            newButton.Name = btnName.Replace(" ", String.Empty);
            newButton.Text = btnName;
            tabControl1.SelectedTab.Controls.Add(newButton);
            newButton.Left = 10;
            newButton.Top = 420;
            SetupClickEvents(newButton);
        }
    }

    private void deleteButton_PB_Click(object sender, EventArgs e)
    {
        ctrlExists = 0;

        if (lastCtrlClicked != null)
        {
            string btnName = lastCtrlClicked.Name;
            foreach (Button button in tabControl1.SelectedTab.Controls)
            {
                if (button.Name == btnName)
                {
                    ctrlExists = 1;
                }
            }
        }

        if (ctrlExists == 1 && lastCtrlClicked != null)
        {
            tabControl1.SelectedTab.Controls.Remove(lastCtrlClicked);
            lastCtrlClicked.Dispose();
            ctrlExists = 0;
        }
        lastCtrlClicked = null;
    }