Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ParentMdi Code:
C#
private void btnBrowse_Click(object sender, EventArgs e)
        {
            //Open folderDialogue at root level
            fbdBrowse = new FolderBrowserDialog();

            //Display the dialogue box
            fbdBrowse.ShowDialog();

            //Get the selected path and display it
            txtDirectory.Text = fbdBrowse.SelectedPath;

            if(Directory.Exists(txtDirectory.Text))
            {
                //Enable the Generate Constellation button
                btnGenConst.Enabled = true;
            }


        }

        private void btnGenConst_Click(object sender, EventArgs e)
        {
            //Get all directories @ the selected path
            string[] sGetDirectory  = Directory.GetFiles(@txtDirectory.Text, "*.cst", SearchOption.AllDirectories);

            //Loop through the directory and create DisplayMdi for file in the directory
            foreach (string sGetpaths in sGetDirectory)
            {
                if (File.Exists(sGetpaths))
                {
                    //Get the name the file
                    string sfilename = sGetpaths.Substring(sGetpaths.LastIndexOf(@"\") + 1);
                    //Call the DisplayMdi.
                    DisplayMdi Display = new DisplayMdi(txtDirectory.Text, sfilename);
                    Display.MdiParent = this;
                    //Display.Text = sfilename.Substring(0,sfilename.LastIndexOf("."));
                    Display.Show();
                    this.LayoutMdi(MdiLayout.Cascade);
                }
            }
            //Disable Generate Constellation until the next browse
            btnGenConst.Enabled = false;
        }

ChildMdi Code:
public partial class DisplayMdi : Form
    {
        Graphics myDrawFillEllipse;
        Graphics myDrawLine;
        Pen myPen;
        SolidBrush myBrush;
        StreamReader strReader;
        Point pntPrevCoodinates;
        Point pntCoodinates;
        private string sDriectory;
        private string sFileName;
        Timer Time = new Timer();
        int iKeepTime = 0;
        int iKeepCount = 0;


        public DisplayMdi(string myDirectory, string myFilename)
        {
            InitializeComponent();
            Time.Interval = 100;
            Time.Tick += new EventHandler(Timer_Tick);
            Time.Start();

            Directory = myDirectory;
            FileName = myFilename;
            Text = myFilename.Substring(0, myFilename.LastIndexOf("."));
            FileStream fsPath = new FileStream(Directory + "\\" + FileName, FileMode.Open, FileAccess.Read);
            strReader = new StreamReader(fsPath);
        }

        public string Directory
        {
            get { return sDriectory; }
            set { sDriectory = value; }
        }
        public string FileName
        {
            get { return sFileName; }
            set { sFileName = value; }
        }
        void Timer_Tick(object sender, EventArgs e)
        {
         
                string input = strReader.ReadLine();
                string[] Coords;
                if (input == null)
                {
                    Time.Stop();
                }
                else
                {
                    myPen = new Pen(Color.Yellow, 2);
                    myBrush = new SolidBrush(Color.Yellow);
                    myDrawFillEllipse = this.CreateGraphics();
                    myDrawLine = this.CreateGraphics();
                    Coords = input.Split(';');

                    pntCoodinates = new Point(Convert.ToInt32(Coords[0]), Convert.ToInt32(Coords[1]));

                    if (iKeepCount == 0)
                    {
                        //Plot the coodinates of the first mouse click
                        myDrawFillEllipse.DrawEllipse(myPen, pntCoodinates.X, pntCoodinates.Y, 4, 4);
                        myDrawFillEllipse.FillEllipse(myBrush, pntCoodinates.X, pntCoodinates.Y, 3, 3);

                    }
                    else
                    {
                        //Plot the coodinates from the first mouse click and draw a line
                        myDrawLine.DrawLine(myPen, pntPrevCoodinates, pntCoodinates);
                        myDrawFillEllipse.DrawEllipse(myPen, pntCoodinates.X, pntCoodinates.Y, 4, 4);
                        myDrawFillEllipse.FillEllipse(myBrush, pntCoodinates.X, pntCoodinates.Y, 3, 3);
                    }

                    pntPrevCoodinates = pntCoodinates;
                    iKeepCount++;
                }
        }

        private void DisplayMdi_Load(object sender, EventArgs e)
        {

        }
Posted

1 solution

You are doing wrong thing. The graphics rendered this way is not preserved anywhere.

You need to render graphics every time the Windows message WM_PAINT is sent. This is done in the handler of the event Paint or, better yet, in the overridden virtual method OnPaint of any Control you want to render your graphics in, including Form. You should not create the instance of the class Graphic, instead, you should get one passed to you through the event arguments parameter.

When some part of your control is invalidated, your rendering method will be called again. It will happen if a part of the view if covered by some other window and later gets exposed again. You should keep all the data needed for rendering. The dynamic behavior of the graphics is achieved through change in this data followed by invalidation of the control or a part of it using one of the Invalidate methods.

Please see also my past answers to some related questions:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900