Avatar billede mmbn Nybegynder
13. oktober 2005 - 23:46 Der er 1 løsning

Threading hjælp eventuel override

Jeg har en kontrol (http://msdn.microsoft.com/msdnmag/issues/04/10/AdvancedBasics/)
som er en progressbar ligende kontrol . Den er placeret i en statusbar i "main vinduet". når jeg kører en metode fra main vinduet der kræver lidt tid og åbner sit resultat i et andet vindue, ville jeg gerne have ovenævnte kontrol vist og kørende. er det en mulighed? hvordan undgår jeg at hele "main vinduet" fryser når jeg starter denne process?
den del af metoden jeg taler om ser således ud:
this.Cursor = Cursors.AppStarting;
                al = rapiHandling.SearchDirectories("\\",fts.StrValue);
                this.progress.Visible = false;
                this.Cursor = Cursors.Default;

rapihandling er fra  OpenNETCF.org RAPI library der hjælper med kokunikationen mellem en pda og en desktop. Den funktion jeg kalder søger pda'en igennem efter billeder. mens den tænker vil jeg gerne have at ovennævnte kontrol bliver vist og kører.

Kotrollen ser således ud:
    public enum ElementStyle : byte
    {
        /// <summary>
        /// Firkantet
        /// </summary>
        Square = 0,
        /// <summary>
        /// Rund
        /// </summary>
        Circle = 1
    }

    public class Progress : System.Windows.Forms.Control
    {
       

        private System.Windows.Forms.Timer cycle;
        private int icurrentAvtiveItem = 0;
        private int icycleSpeed = 1000;

        private ElementStyle enumShapeToDraw = ElementStyle.Circle;
        private int iShapeSize = 5;
        private int iShapeSpacing = 5;
        private int iShapeCount;
        private System.Windows.Forms.Border3DStyle bBorderStyle= System.Windows.Forms.Border3DStyle.Flat;

        const int SIZE_INCR = 2;
        const int  MIN_INTERVAL = 100;

        private System.ComponentModel.Container components = null;


        public Progress(System.ComponentModel.IContainer container)
        {
            // Required for Windows.Forms Class Composition Designer support
            container.Add(this);
            InitializeComponent();

            this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
            this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);

            cycle = new System.Windows.Forms.Timer();
            cycle.Interval = this.icycleSpeed;
            cycle.Enabled = this.Enabled;
            cycle.Tick += new EventHandler(cycle_Tick);
        }

        public Progress()
        {
            // Required for Windows.Forms Class Composition Designer support
            InitializeComponent();

            this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
            this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);

            cycle = new System.Windows.Forms.Timer();
            cycle.Interval = this.icycleSpeed;
            cycle.Enabled = this.Enabled;
            cycle.Tick += new EventHandler(cycle_Tick);
        }


        [System.ComponentModel.Category("Appearance")]
        public ElementStyle ShapeToDraw
        {
            get{return enumShapeToDraw;}
            set
            {
                if(value != enumShapeToDraw)
                {
                    enumShapeToDraw = value;
                    icurrentAvtiveItem = 0;
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.Category("Appearance")]
        public int ShapeSize
        {
            get{return iShapeSize;}
            set
            {
                if((value != iShapeSize) && (value > 0))
                {
                    iShapeSize = value;
                    icurrentAvtiveItem = 0;
                    RecalcCountAndInterval();
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.Category("Appearance")]
        public int ShapeSpacing
        {
            get{return iShapeSpacing;}
            set
            {
                if((value != iShapeSpacing) && (value > 0))
                {
                    iShapeSpacing = value;
                    icurrentAvtiveItem = 0;
                    RecalcCountAndInterval();
                    this.Invalidate();
                }
            }
        }

        public int ShapeCount
        {
            get{return iShapeCount;}
        }
   
        [System.ComponentModel.Category("Appearance")]
        public int CycleSpeed
        {
            get{return icycleSpeed;}
            set
            {
                if(value != icycleSpeed)
                {
                    icycleSpeed = value;
                    iShapeCount = 0;
                    RecalcCountAndInterval();
                }
            }
        }
       
        [System.ComponentModel.Category("Appearance")]
        public System.Windows.Forms.Border3DStyle BorderStyle
        {
            get{return bBorderStyle;}
            set
            {
                if(value != bBorderStyle)
                {
                    bBorderStyle = value;
                    this.Invalidate();
                }
            }
        }
       
   
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            System.Windows.Forms.ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.BorderStyle);

            for(int i=1;i<iShapeCount;i++)
            {
                Point pos = CalculateItemPosition(i);
                int x = pos.X;
                int y = pos.Y;

                if(i == icurrentAvtiveItem)
                {
                    DrawShape(g, this.enumShapeToDraw, x-SIZE_INCR, y-SIZE_INCR, iShapeSize + (SIZE_INCR*2));
                }
                else
                {
                    DrawShape(g, this.enumShapeToDraw, x, y, iShapeSize);
                }

            }
        }

        private Point CalculateItemPosition(int index)
        {
            Point pos = new Point(0,0);
            pos.X = (iShapeSpacing*index) + (iShapeSize*(index-1));
            pos.Y = (this.Height/2)-(iShapeSize/2);
            return pos;
        }

        private void DrawShape(Graphics g, ElementStyle shape, int x, int y, int size)
        {
            Brush shapeBrush = new SolidBrush(this.ForeColor);

            switch(shape)
            {
                case ElementStyle.Circle:
                {
                    g.FillEllipse(shapeBrush, x, y, size, size);
                    break;
                }
                case ElementStyle.Square:
                {
                    g.FillRectangle(shapeBrush, x, y, size, size);
                    break;
                }
            }
        }

                protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }


        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion

        private void cycle_Tick(object sender, EventArgs e)
        {
            int iOldActiveItem = icurrentAvtiveItem;

            if(this.icurrentAvtiveItem >= iShapeCount)
            {
                this.icurrentAvtiveItem = 1;
            }
            else
            {
                this.icurrentAvtiveItem += 1;
            }

            this.Invalidate();
//                this.Invalidate(icurrentAvtiveItem);
        }

        private Rectangle CalcItemRectangle(int i)
        {
            Rectangle rect = new Rectangle(0,0,0,0);
            Point pos = new Point(0,0);

            pos = CalculateItemPosition(i);
            rect.X = pos.X-SIZE_INCR;
            rect.Y = pos.Y-SIZE_INCR;
            rect.Width = this.iShapeSize + (2*SIZE_INCR);
            rect.Height = rect.Width;

            return rect;
        }
       
        protected override void OnResize(EventArgs e)
        {
            RecalcCountAndInterval();
            base.OnResize (e);
        }

        private void RecalcCountAndInterval()
        {
            int w = this.Width;
            int iNewShapeCount;
            if(iShapeSize > 0 && iShapeSpacing > 0)
            {
                iNewShapeCount = (int) Math.Floor((w-iShapeSpacing) / (iShapeSize+iShapeSpacing));
            }
            else
            {
                iNewShapeCount = 1;
            }

            if(iNewShapeCount != iShapeCount && iNewShapeCount > 0)
            {
                int interval = this.icycleSpeed/iNewShapeCount;
                if(interval >= MIN_INTERVAL)
                {
                    cycle.Interval = interval;
                }
                else
                {
                    cycle.Interval = MIN_INTERVAL;
                }
                iShapeCount = iNewShapeCount;
            }
        }
       
        protected override void OnEnabledChanged(EventArgs e)
        {
            cycle.Enabled = this.Enabled;
        }


    }
Avatar billede mmbn Nybegynder
18. oktober 2005 - 17:26 #1
lukker - ingen svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester