情况是这样,我现在想用一个MDI父窗体,这个窗体上有添加,修改,删除,搜索 这些按钮  在一个ToolStrip上
然后,我在父窗体中打开一个子窗体,如何用父窗体上的这三个按钮去操作当前打开的子窗体呢?

解决方案 »

  1.   

    父窗体上加一个Form的成员变量,用来保存创建的子窗体,这些按钮就可以操作子窗体,不过你要判断子窗体是否已经关闭,如果子窗体已经关闭Form.Created为false,此时要重新创建子窗体,你有多个子窗体的时候,这个Form变量可以记住当前子窗体,在子窗体的Activate事件中设置,
      

  2.   

    MDI中只有一个子窗体是激活的,每当他激活的时候,将子窗体事件挂接在主窗体按钮上,
    当子窗体失去激活的时候,解除挂接的事件。
      

  3.   

    我做了个demo,你可以看下,代码和图象都在,我是在主窗体中添加了两个按钮,一个打开图象,在子窗体中显示,一个对子窗体图象进行灰度化。主要看打开按钮中的子窗体激活程序,还有灰度化Gray按钮中的调用程序。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //(open按钮)
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                OpenImage();           
                Form2 f = new Form2();
                f.Show();
                f.pictureBox1.Image = (Image)src;
                this.ActivateMdiChild(f);//激活当前子窗体
                
            }
            //图像打开函数
            private string curFileName;
            //定义图象变量
            public Bitmap src;
           
            //灰度化,子窗体调用主窗体图象灰度化函数(gray按钮)
            private void grayToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Bitmap s = GetGray(src);
                if (this.ActiveMdiChild != null)
                {
                    Form2 f = new Form2();
                    f = (Form2)this.ActiveMdiChild;
                    f.pictureBox1.Image = (Image)s;
                    f.Show();
                }
            }
            //定义图象打开函数
            private void OpenImage()
            {
                try
                {
                    ofd.Filter = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp|jpeg files (*.jpg)|*.jpg|png files (*.png)|*.png";
                    ofd.Title = "打开";
                    ofd.ShowHelp = true;
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        curFileName = ofd.FileName;
                        src = new Bitmap(curFileName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //定义图像灰度化函数
            private Bitmap  GetGray(Bitmap src)
            {
                int w = src.Width;
                int h = src.Height;
                double[,] srcBytes = new double[w, h];
                //构建与原图像大小一样的模版图像
                Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                //将原图像存入内存
                System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* pIn = (byte*)srcData.Scan0.ToPointer();
                    byte* pOut = (byte*)dstData.Scan0.ToPointer();
                    byte* p;
                    int stride = srcData.Stride;
                    int r, g, b;
                    for (int y = 0; y < h; y++)
                    {
                        for (int x = 0; x < w; x++)
                        {
                            p = pIn;
                            r = p[2];
                            g = p[1];
                            b = p[0];
                            //调用图像灰度化公式
                            pOut[0] = pOut[1] = pOut[2] = (byte)(b * 0.114 + g * 0.587 + r * 0.299);
                            srcBytes[x, y] = pOut[0];
                            pIn += 3;
                            pOut += 3;
                        }
                        pIn += srcData.Stride - w * 3;
                        pOut += srcData.Stride - w * 3;
                    }
                    src.UnlockBits(srcData);
                    dstBitmap.UnlockBits(dstData);
                }
                return dstBitmap;        }
        }
    }