Click here to Skip to main content
15,885,910 members
Articles / Programming Languages / C#

Moveable Resizable Objects

Rate me:
Please Sign up or sign in to vote.
4.98/5 (59 votes)
9 Oct 2009CPOL198 min read 125.2K   8.7K   178  
Here is a description of an extremely powerful mechanism that makes screen objects moveable and resizable.
using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;

using MoveGraphLibrary;

namespace Test_MoveGraphLibrary
{
    public partial class Form_DefineNewBarChart : Form
    {
        int nSet, nSegment;
        Color clrStart, clrEnd;
        Rectangle rc;
        BarChart chart;
        Random rand;
        int nSeed;
        double [,] fVals;   // = new double [nSet, nSegment];
        string [] texts;

        bool bAfterInit = false;

        // -------------------------------------------------
        public Form_DefineNewBarChart ()
        {
            InitializeComponent ();
            //Font = new Font ("Times New Roman", 14);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            DateTime dt = DateTime .Now;
            nSeed = dt .Hour * 3600 + dt .Minute * 60 + dt .Second;
            while (nSeed < 10000)
            {
                nSeed += 1000;
            }
            rand = new Random (nSeed);
            nSet = 3;
            nSegment = 5;
            numericSets .Value = nSet;
            numericSegments .Value = nSegment;
            comboTexts .SelectedIndex = 1;

            int iStart = rand .Next (nSeed) % 7;
            int iEnd = rand .Next (nSeed) % 7;
            if (iStart == iEnd) iEnd++;
            clrStart = Auxi_Colours .ColorPredefined (iStart);
            clrEnd = Auxi_Colours .ColorPredefined (iEnd);
            int cyT = numericSegments .Bottom + 12;
            rc = new Rectangle (btnChange .Left, cyT, buttonOK .Right - btnChange .Left, buttonOK .Top - 12 - cyT);

            SetBarChart ();

            toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
            toolTip1 .SetToolTip (btnEndClr, Auxi_Convert .ClrToStr (clrEnd, ColorStr .IN_ROWS));
            bAfterInit = true;
        }
        // =================================================        SetBarChart
        private void SetBarChart ()
        {
            fVals = new double [nSet, nSegment];
            for (int i = 0; i < nSet; i++)
            {
                for (int j = 0; j < nSegment; j++)
                {
                    fVals [i, j] = rand .Next (nSeed) % 1000;
                }
            }
            if (bAfterInit)
            {
                texts = PrepareTexts ();
            }
            else
            {
                texts = Auxi_Common .strDays;
            }
            chart = new BarChart (this, rc, fVals, Side .S, texts, TextsDrawingDirection .LTtoRB,    //Auxi_Common .strDays
                                                   Side .E, 1000.0, 0.0, GridOrigin .ByStep, 200.0);
            chart .Colors = Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd);
            chart .FillCoefficients (0, 0.85);
            chart .NumScale .Visible = false;
            chart .TextScale .Visible = false;
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            chart .Draw (e .Graphics);
        }
        // -------------------------------------------------        ValueChanged_numericSets
        private void ValueChanged_numericSets (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                nSet = Convert .ToInt32 (numericSets .Value);
                SetBarChart ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        ValueChanged_numericSegments
        private void ValueChanged_numericSegments (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                nSegment = Convert .ToInt32 (numericSegments .Value);
                SetBarChart ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnChange
        private void Click_btnChange (object sender, EventArgs e)
        {
            SetBarChart ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_btnStartClr
        private void Click_btnStartClr (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = clrStart;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                clrStart = dlg .Color;
                chart .Colors = Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd);
                toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnEndClr
        private void Click_btnEndClr (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = clrEnd;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                clrEnd = dlg .Color;
                chart .Colors = Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd);
                toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
                Invalidate ();
            }
        }
        // -------------------------------------------------        BarChart
        public BarChart BarChart
        {
            get { return (chart); }
        }
        // -------------------------------------------------        SelectedIndexChanged_comboTexts
        private void SelectedIndexChanged_comboTexts (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                chart .TextScale .Texts = PrepareTexts ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        PrepareTexts
        private string [] PrepareTexts ()
        {
            string [] strs = new string [nSegment];
            string [] strsSrc;
            switch (comboTexts .SelectedIndex)
            {
                case 0:
                default:
                    strsSrc = Auxi_Common .strAlphabet;
                    break;

                case 1:
                    strsSrc = Auxi_Common .strDays;
                    break;

                case 2:
                    strsSrc = Auxi_Common .strMonths;
                    break;

                case 3:
                    strsSrc = Auxi_Common .strNames;
                    break;
            }
            for (int i = 0; i < nSegment; i++)
            {
                strs [i] = Auxi_Common .StringOfText (strsSrc, i);
            }
            return (strs);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions