简体   繁体   中英

C# .Net 4.0 panel_MouseMove Event flickering rectangles

My Problem is, even when i want to move my drawed rectangles on panel_MouseMove(), all these rectangles flicker. And i don't no why. Here is my Code of the panel_MouseMove Method:

private void panel_MouseMove(object sender, MouseEventArgs e)
    {
        bool flag = false;
        if (_guiParserMgr != null)
        {
            if (e.Button == MouseButtons.Left)
            {
                foreach (GuiModulModel gmm in _guiParserMgr.Moduls)
                {
                    log.Debug("Modul: XPos:" + gmm.getXPos() + " YPos:" + gmm.getYPos() + " WidthPos:" + ((int)gmm.getXPos() + (int)gmm.getWidth()) + " HeightPos:" + ((int)gmm.getYPos() + (int)gmm.getHeight()));

                    if (flag == false)
                    {
                        foreach (GuiEntityModel gem in gmm.Entitys)
                        {
                            log.Debug("Entity: XPos:" + gem.getXPos() + " YPos:" + gem.getYPos() + " WidthPos:" + ((int)gem.getXPos() + (int)gem.getWidth()) + " HeightPos:" + ((int)gem.getYPos() + (int)gem.getHeight()));
                            if (gem.getXPos() < e.X && gem.getXPos() + gem.getWidth() > e.X && gem.getYPos() < e.X && gem.getYPos() + gem.getHeight() > e.Y)
                            {

                                // Increment rectangle-location by mouse-location delta.
                                gem.setXPos(e.X /*+ _firstPoint.X*/);
                                gem.setYPos(e.Y /*+ _firstPoint.Y*/);

                                // Re-calibrate on each move operation.
                                _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                                //panel.Invalidate();

                                foreach (GuiFieldModel gfm in gem.Fields)
                                {
                                    // Increment rectangle-location by mouse-location delta.
                                    gfm.setXPos(e.X/*+ _firstPoint.X*/);
                                    gfm.setYPos(e.Y + 15/*+ _firstPoint.Y*/);

                                    // Re-calibrate on each move operation.
                                    _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                                    //panel.Invalidate();                                    
                                }
                                flag = true;
                            }
                        }
                    }
                    if (flag == false)
                    {
                        foreach (GuiEnumTypeModel getm in gmm.EnumTypes)
                        {
                            log.Debug("EnumType: XPos:" + getm.getXPos() + " YPos:" + getm.getYPos() + " WidthPos:" + ((int)getm.getXPos() + (int)getm.getWidth()) + " HeightPos:" + ((int)getm.getYPos() + (int)getm.getHeight()));
                            if (getm.getXPos() < e.X && getm.getXPos() + getm.getWidth() > e.X && getm.getYPos() < e.X && getm.getYPos() + getm.getHeight() > e.Y)
                            {
                                // Increment rectangle-location by mouse-location delta.
                                getm.setXPos(e.X /*+ _firstPoint.X*/);
                                getm.setYPos(e.Y /*+ _firstPoint.Y*/);
                                // Re-calibrate on each move operation.
                                _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                                //panel.Invalidate();
                                flag = true;
                            }

                        }
                    }
                    if (flag == false)
                    {

                        foreach (GuiServiceModel gsm in gmm.Services)
                        {
                            log.Debug("Service: XPos:" + gsm.getXPos() + " YPos:" + gsm.getYPos() + " WidthPos:" + ((int)gsm.getXPos() + (int)gsm.getWidth()) + " HeightPos:" + ((int)gsm.getYPos() + (int)gsm.getHeight()));
                            if (gsm.getXPos() < e.X && gsm.getXPos() + gsm.getWidth() > e.X && gsm.getYPos() < e.X && gsm.getYPos() + gsm.getHeight() > e.Y)
                            {

                                // Increment rectangle-location by mouse-location delta.
                                gsm.setXPos(e.X /*+ _firstPoint.X*/);
                                gsm.setYPos(e.Y /*+ _firstPoint.Y*/);

                                // Re-calibrate on each move operation.
                                _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                                //panel.Invalidate();
                                flag = true;
                            }

                        }
                    }
                    if (flag == false)
                    {
                        foreach (GuiWebMvcModel gwm in gmm.WebMvcs)
                        {
                            log.Debug("WebMVC: XPos:" + gwm.getXPos() + " YPos:" + gwm.getYPos() + " WidthPos:" + ((int)gwm.getXPos() + (int)gwm.getWidth()) + " HeightPos:" + ((int)gwm.getYPos() + (int)gwm.getHeight()));

                            if (gwm.getXPos() < e.X && gwm.getXPos() + gwm.getWidth() > e.X && gwm.getYPos() < e.X && gwm.getYPos() + gwm.getHeight() > e.Y)
                            {
                                // Increment rectangle-location by mouse-location delta.
                                gwm.setXPos(e.X /*+ _firstPoint.X*/);
                                gwm.setYPos(e.Y /*+ _firstPoint.Y*/);

                                // Re-calibrate on each move operation.
                                _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                                //panel.Invalidate();
                                flag = true;
                            }

                        }
                    }
                    if (flag == false)
                    {
                        if (gmm.getXPos() < e.X && gmm.getXPos() + gmm.getWidth() > e.X && gmm.getYPos() < e.X && gmm.getYPos() + gmm.getHeight() > e.Y)
                        {
                            // Increment rectangle-location by mouse-location delta.
                            gmm.setXPos(e.X /*+ _firstPoint.X*/);
                            gmm.setYPos(e.Y /*+ _firstPoint.Y*/);

                            // Re-calibrate on each move operation.
                            _firstPoint = new MovePoint { X = e.X, Y = e.Y };

                            //panel.Invalidate();
                            flag = true;

                        }
                    }
                    panel.Invalidate();




                }
            }
        }
    }

Please Help Me! :D Thank you :)

My example code is for a PictureBox, but also works for other controls and shapes. It works flicker free.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace BigBrother
{
    public class SizeablePictureBox : PictureBox
    {

        private const int grab = 16;
        public Point PBstartLocation;
        public Point PBcornerStart;

        public SizeablePictureBox()
        {
            this.ResizeRedraw = true;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
            ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x84)
            {

                var pos = this.PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
                if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
                    m.Result = new IntPtr(17);
            }
        }


        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                PBstartLocation = e.Location;
                PBcornerStart = this.Location;
                this.BringToFront();
            }

        }


        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Left = e.X + this.Left - PBstartLocation.X;
                this.Top = e.Y + this.Top - PBstartLocation.Y;
            }
        }
    }

Better example, made from link in comment: https://mega.co.nz/#!cghECSLZ!KVzvbzkSPmuonuBVTsRok1Milz1cbbGW9bJEmTQQnsY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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