新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#透明窗体问题 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7447 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#透明窗体问题 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 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 类中有些用不上的函数,需要甄别一下


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/5/28 14:38:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/27 2:52:42

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    93.750ms