我有个窗口Form1,然后里面镶嵌了另一个窗口Form2
我想让Form2的背景透明,来显示Form1的背景。
怎么做呢?
我设置了Form1的透明过滤。
          this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
           this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
  然后把Form1的背景设为白色。
虽然Form2是透明了,但是也看不到Form1的背景图了。请问为什么?

解决方案 »

  1.   

    Form的属性中本来就有透明度这一属性的,把form2设为全透明不就行了
      

  2.   

    不行,没有用的,因为Form2已经属于子窗口了,这个opacity属性没有任何作用。
    它就能受Form1的opacity的影响,可是也会导致了From2的别的控件都透明了。
    这样不行
      

  3.   

    paint.net就是这样,下个源码看看去
      

  4.   

    public static void SetFormOpacity(Form form, double opacity)
            {
                if (opacity < 0.0 || opacity > 1.0)
                {
                    throw new ArgumentOutOfRangeException("opacity", "must be in the range [0, 1]");
                }            if (System.Environment.OSVersion.Version >= OS.WindowsXP)
                {
                    uint exStyle = SafeNativeMethods.GetWindowLongW(form.Handle, NativeConstants.GWL_EXSTYLE);                byte bOldAlpha = 255;                if ((exStyle & NativeConstants.GWL_EXSTYLE) != 0)
                    {
                        uint dwOldKey;
                        uint dwOldFlags;
                        bool result = SafeNativeMethods.GetLayeredWindowAttributes(form.Handle, out dwOldKey, out bOldAlpha, out dwOldFlags);
                    }                byte bNewAlpha = (byte)(opacity * 255.0);
                    uint newExStyle = exStyle;                if (bNewAlpha != 255)
                    {
                        newExStyle |= NativeConstants.WS_EX_LAYERED;
                    }                if (newExStyle != exStyle || (newExStyle & NativeConstants.WS_EX_LAYERED) != 0)
                    {
                        if (newExStyle != exStyle)
                        {
                            SafeNativeMethods.SetWindowLongW(form.Handle, NativeConstants.GWL_EXSTYLE, newExStyle);
                        }                    if ((newExStyle & NativeConstants.WS_EX_LAYERED) != 0)
                        {
                            SafeNativeMethods.SetLayeredWindowAttributes(form.Handle, 0, bNewAlpha, NativeConstants.LWA_ALPHA);
                        }
                    }                GC.KeepAlive(form);
                }
                else
                {
                    form.Opacity = opacity;
                }
            }这个是paint.net的代码SafeNativeMethods类,是一个用来做win32API的类,你可以参考一下,最好自己去下他的代码研究下。有很多图形方面的东西可以借鉴
      

  5.   

    好复杂啊,我想再问一个,我想清除PictureBox的一部分图,怎么做?
    我看到函数Clear,只能清除全部的:
      PictureBox box;
      Bitmap bm = (Bitmap)box.Image;
      Graphics g = Graphics.FromImage(bm);
      g.Clear(Color.Transparent);