cyclss 2018-03-22 14:44 采纳率: 0%
浏览 527
已结题

半透明异形窗体与主窗体“捆绑”问题

按下button,弹出半透明异形窗体与红框,运行效果如图图片说明
但是问题来了,主窗体form1与AlphaForm中间可以插入外部软件的窗体,效果如图图片说明

大神有没有办法,将两窗体“捆绑”,中间无法插入其他窗体
我的代码如下,

 namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        AlphaForm putForm;//放外面来
        bool sign = true;


        public Form1()
        {
            InitializeComponent();
            pictureBox1.Load  (@"C:\6.png");
        }

        private void Form1_Move(object sender, EventArgs e)
        {
            putForm.Location = new Point(this.Location.X +7 , this.Location.Y + 29);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (sign == true)
            {
                Bitmap bmp = new Bitmap(401,301);
                bmp = potholing(50, 50, 170, 170, bmp, 120, 0);
                Graphics g = Graphics.FromImage(bmp);
                Pen p = new Pen(Color.Red, 2);
                g.DrawRectangle(p, 50, 50, 120, 120);

                putForm = new AlphaForm(); //alpha通道的显示图像窗体
                putForm.Location = new Point(this.Location.X + 7, this.Location.Y + 29);//框显示在Form1  100x100的位置
                putForm.SetBitmap(bmp, 255); //设置图像 
                putForm.Show(); //显示窗体
                sign = false;
            }
        }
        public Bitmap potholing(int _x, int _y, int _width, int _height, Bitmap src, int num, int trans)
        {
            try
            {
                int w = src.Width;
                int h = src.Height;
                Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                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;
                            b = pIn[0];
                            g = pIn[1];
                            r = pIn[2];
                            pOut[1] = (byte)g;
                            pOut[2] = (byte)r;

                            if ((y > _y) && (y < _height) && (x > _x) && (x < _height))
                            {
                                pOut[3] = (byte)trans;
                            }
                            else pOut[3] = (byte)num;

                            pOut[0] = (byte)b;
                            pIn += 4;
                            pOut += 4;
                        }
                        pIn += srcData.Stride - w * 4;
                        pOut += srcData.Stride - w * 4;
                    }
                    src.UnlockBits(srcData);
                    dstBitmap.UnlockBits(dstData);
                    return dstBitmap;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
                return null;
            }
        }

    }
 namespace WindowsFormsApplication4
{
    public partial class AlphaForm : Form
    {
        class Alpha_Win32
        {
            public enum Bool
            {
                False = 0,
                True
            };

            [StructLayout(LayoutKind.Sequential)]
            public struct Point
            {
                public Int32 x;
                public Int32 y;

                public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct Size
            {
                public Int32 cx;
                public Int32 cy;

                public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
            }

            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            struct ARGB
            {
                public byte Blue;
                public byte Green;
                public byte Red;
                public byte Alpha;
            }

            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct BLENDFUNCTION
            {
                public byte BlendOp;
                public byte BlendFlags;
                public byte SourceConstantAlpha;
                public byte AlphaFormat;
            }

            public const Int32 ULW_COLORKEY = 0x00000001;
            public const Int32 ULW_ALPHA = 0x00000002;
            public const Int32 ULW_OPAQUE = 0x00000004;

            public const byte AC_SRC_OVER = 0x00;
            public const byte AC_SRC_ALPHA = 0x01;


            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern IntPtr GetDC(IntPtr hWnd);

            [DllImport("user32.dll", ExactSpelling = true)]
            public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern Bool DeleteDC(IntPtr hdc);

            [DllImport("gdi32.dll", ExactSpelling = true)]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            public static extern Bool DeleteObject(IntPtr hObject);
        }
        public AlphaForm()
        {
            FormBorderStyle = FormBorderStyle.None;
            DoubleBuffered = true;
            TopMost = true;
            ShowInTaskbar = false;
        }

         public void SetBitmap(Bitmap bitmap)
    {
        SetBitmap(bitmap, 255);
    }

    public void SetBitmap(Bitmap bitmap, byte opacity)
    {
        /*
        if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."+ bitmap.PixelFormat);
        */
        IntPtr screenDc = Alpha_Win32.GetDC(IntPtr.Zero);
        IntPtr memDc = Alpha_Win32.CreateCompatibleDC(screenDc);
        IntPtr hBitmap = IntPtr.Zero;
        IntPtr oldBitmap = IntPtr.Zero;

        try
        {
            hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
            oldBitmap = Alpha_Win32.SelectObject(memDc, hBitmap);

            Alpha_Win32.Size size = new Alpha_Win32.Size(bitmap.Width, bitmap.Height);
            Alpha_Win32.Point pointSource = new Alpha_Win32.Point(0, 0);
            Alpha_Win32.Point topPos = new Alpha_Win32.Point(Left, Top);
            Alpha_Win32.BLENDFUNCTION blend = new Alpha_Win32.BLENDFUNCTION();
            blend.BlendOp = Alpha_Win32.AC_SRC_OVER;
            blend.BlendFlags = 0;
            blend.SourceConstantAlpha = opacity;
            blend.AlphaFormat = Alpha_Win32.AC_SRC_ALPHA;

            Alpha_Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Alpha_Win32.ULW_ALPHA);
            GC.Collect();
        }
        finally
        {
            Alpha_Win32.ReleaseDC(IntPtr.Zero, screenDc);
            if (hBitmap != IntPtr.Zero)
            {
                Alpha_Win32.SelectObject(memDc, oldBitmap);
                Alpha_Win32.DeleteObject(hBitmap);
            }
            Alpha_Win32.DeleteDC(memDc);
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00080000; 
            return cp;
        }
    }
    }
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-03-22 15:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题