避免表格重叠时的图形清晰(层叠的表格) [英] Avoid Graphics being clear when forms overlap (cascaded forms)

查看:137
本文介绍了避免表格重叠时的图形清晰(层叠的表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ParentMdi代码:

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)
        {

        }

推荐答案

您做错了事.用这种方式渲染的图形不会保留在任何地方.

每次发送Windows消息WM_PAINT时,您都需要渲染图形.这是在事件Paint的处理程序中完成的,或者更好的是,在要呈现图形的任何Control的重写的虚拟方法OnPaint中进行,包括Form.您不应该创建类Graphic的实例,而是应该通过事件arguments参数将其传递给您.

当控件的某些部分无效时,将再次调用您的呈现方法.如果视图的一部分被其他窗口覆盖,并且稍后再次暴露,则会发生这种情况.您应该保留渲染所需的所有数据.通过更改此数据,然后使用Invalidate方法之一使控件或控件的一部分无效,可以实现图形的动态行为.

另请参阅我过去对一些相关问题的回答:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子表单之间画线 [在面板上捕获图形 [
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


这篇关于避免表格重叠时的图形清晰(层叠的表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆