
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
{
    ///      /// 对窗口进行层次化处理     /// 
/// 窗口句柄
/// 目标窗口的屏幕上下文句柄
/// 目标位置
/// 目标尺寸
/// 内存句柄源
/// 源位置
/// 混合标志
/// 合成方式
/// 透明标志
    /// 
[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);
    ///      /// 控制窗口合成的方式     /// 
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
    ///      /// 获取窗口的设备上下文句柄     /// 
/// 窗口句柄
    /// 
[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;
}
}
    ///      /// 获取对驱动设备的引用     /// 
/// 驱动名称
/// 设备名称
/// 无用,可以设定位"NULL"
/// 任意的可打印数据
    /// 
[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 // 光栅的操作值
);
    ///      /// 设置运用钩子的函数和实例     /// 
/// 钩子类型
/// 钩子过程的指针,即钩子处理函数
/// 程序实例的句柄
/// 制定被监视的线程
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr pInstance, int threadId);
    ///      /// 将 Hook 信息传递到链表中下一个 Hook 处理过程     /// 
/// 钩子函数句柄
/// 钩子类型
/// 主内容
/// 副内容
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam);
    ///      /// 卸载钩子     /// 
/// 要卸载钩子的指针
    /// 
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
    ///      /// 鼠标钩子事件结构定义     /// 
    /// 
[StructLayout(LayoutKind.Sequential)]
public struct MouseHookStruct
{
        ///          /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.         /// 
public POINT Point;
public UInt32 MouseData;
public UInt32 Flags;
public UInt32 Time;
public UInt32 ExtraInfo;
}
    ///      /// 坐标值     /// 
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
    ///      /// 钩子处理函数委托     /// 
/// 消息类别
/// w消息内容
/// l消息类容
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
}
Win32 类中有些用不上的函数,需要甄别一下
