以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  C#透明窗体问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=84963)


--  作者:卷积内核
--  发布时间:5/28/2010 2:38:00 PM

--  C#透明窗体问题
迅雷资讯的透明度似乎是统一的。^V^
UpdateLayeredWindow()可以实现你的需求,只不过调用该函数窗体无法显示其他控件。、
我的调用方法
private void SetAlphaBorder(Bitmap bit,Point location)
        {
            if (!haveHandle) return;

            if (!Bitmap.IsCanonicalPixelFormat(bit.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bit.PixelFormat))
                throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");

            IntPtr oldBits = IntPtr.Zero;
            IntPtr screenDC = Win32.GetWindowDC(IntPtr.Zero);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr memDc = Win32.CreateCompatibleDC(screenDC);

            try
            {
                Win32.Point topLoc = new Win32.Point(location.X, location.Y);
                Win32.Size bitMapSize = new Win32.Size(bit.Width, bit.Height);
                Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
                Win32.Point srcLoc = new Win32.Point(0, 0);

                hBitmap = bit.GetHbitmap(Color.FromArgb(0));
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = 255;
                blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags = 0;

                Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }

UpdateLayeredWindow()函数原型
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern int UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);


全部复制给你吧
internal class Win32
{
    /// <summary>
    /// 对窗口进行层次化处理
    /// </summary>
    /// <param name="hWnd">窗口句柄</param>
    /// <param name="hdcDst">目标窗口的屏幕上下文句柄</param>
    /// <param name="pptDst">目标位置</param>
    /// <param name="psize">目标尺寸</param>
    /// <param name="hdcSrc">内存句柄源</param>
    /// <param name="pprSrc">源位置</param>
    /// <param name="crKey">混合标志</param>
    /// <param name="pblend">合成方式</param>
    /// <param name="dwFlags">透明标志</param>
    /// <returns>层次化窗口是否成功</returns>
    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern int UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
    /// <summary>
    /// 控制窗口合成的方式
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BLENDFUNCTION
    {
        public byte BlendOp;
        public byte BlendFlags;
        public byte SourceConstantAlpha;
        public byte AlphaFormat;
    }
    /// <summary>
    /// 获取窗口的设备上下文句柄
    /// </summary>
    /// <param name="hWnd">窗口句柄</param>
    /// <returns>窗口的设备上下文句柄</returns>
    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);

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

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

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern int DeleteObject(IntPtr hObj);

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

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern int DeleteDC(IntPtr hDC);
    // 需要的标志
    public const Int32 WS_EX_LAYERED = 0x80000;
    public const Int32 HTCAPTION = 0x02;
    public const Int32 WM_NCHITTEST = 0x84;
    public const Int32 ULW_ALPHA = 0x02;
    public const byte AC_SRC_OVER = 0x00;
    public const byte AC_SRC_ALPHA = 0x01;

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

        public Size(Int32 x, Int32 y)
        {
            cx = x;
            cy = y;
        }
    }

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

        public Point(Int32 x, Int32 y)
        {
            this.x = x;
            this.y = y;
        }
    }
    /// <summary>
    /// 获取对驱动设备的引用
    /// </summary>
    /// <param name="lpszDriver">驱动名称</param>
    /// <param name="lpszDevice">设备名称</param>
    /// <param name="lpszOutput">无用,可以设定位"NULL"</param>
    /// <param name="lpInitData">任意的可打印数据</param>
    /// <returns></returns>
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);

    [DllImport("gdi32.dll")]
    public static extern bool BitBlt(
      IntPtr hdcDest,   //   目标设备的句柄     
      int nXDest,   //   目标对象的左上角的X坐标     
      int nYDest,   //   目标对象的左上角的X坐标     
      int nWidth,   //   目标对象的矩形的宽度     
      int nHeight,   //   目标对象的矩形的长度     
      IntPtr hdcSrc,   //   源设备的句柄     
      int nXSrc,   //   源对象的左上角的X坐标     
      int nYSrc,   //   源对象的左上角的X坐标     
      System.Int32 dwRop   //   光栅的操作值     
      );
    /// <summary>
    /// 设置运用钩子的函数和实例
    /// </summary>
    /// <param name="idHook">钩子类型</param>
    /// <param name="lpfn">钩子过程的指针,即钩子处理函数</param>
    /// <param name="pInstance">程序实例的句柄</param>
    /// <param name="threadId">制定被监视的线程</param>
    [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr pInstance, int threadId);
    /// <summary>
    /// 将 Hook 信息传递到链表中下一个 Hook 处理过程
    /// </summary>
    /// <param name="pHookHandle">钩子函数句柄</param>
    /// <param name="nCode">钩子类型</param>
    /// <param name="wParam">主内容</param>
    /// <param name="lParam">副内容</param>
    [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
        Int32 wParam, IntPtr lParam);
    /// <summary>
    /// 卸载钩子
    /// </summary>
    /// <param name="pHookHandle">要卸载钩子的指针</param>
    /// <returns>钩子卸载是否成功</returns>
    [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
    /// <summary>
    /// 鼠标钩子事件结构定义
    /// </summary>
    /// <remarks>详细说明请参考MSDN中关于 MSLLHOOKSTRUCT 的说明</remarks>
    [StructLayout(LayoutKind.Sequential)]
    public struct MouseHookStruct
    {
        /// <summary>
        /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.
        /// </summary>
        public POINT Point;

        public UInt32 MouseData;
        public UInt32 Flags;
        public UInt32 Time;
        public UInt32 ExtraInfo;
    }
    /// <summary>
    /// 坐标值
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;
    }
    /// <summary>
    /// 钩子处理函数委托
    /// </summary>
    /// <param name="nCode">消息类别</param>
    /// <param name="wParam">w消息内容</param>
    /// <param name="lParam">l消息类容</param>
    public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
}


Win32 类中有些用不上的函数,需要甄别一下


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms