效果:
上一篇我们讲到畸形自定义控件,在调研过程中发现窗体也可以设置成畸形,主要原理是GDI+绘制畸形图片,根据图片生成窗体句柄
畸形窗体主要函数:
GDI+绘制所需图片,图片生成窗体
/// <summary>
/// 功能描述:根据图片显示窗体
/// </summary>
/// <param name="bitmap">bitmap</param>
/// <exception cref="System.ApplicationException">The picture must be 32bit picture with alpha channel.</exception>
/// <exception cref="ApplicationException">The picture must be 32bit picture with alpha channel.</exception>
private void SetBits(Bitmap bitmap)
{
if (!haveHandle) return;
if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
try
{
Win32.Point topLoc = new Win32.Point(Left, Top);
Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
Win32.Point srcLoc = new Win32.Point(0, 0);
hBitmap = bitmap.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);
}
}
无焦点窗体处理
#region 无焦点窗体处理
/// <summary>
/// Sets the active window.
/// </summary>
/// <param name="handle">The handle.</param>
/// <returns>IntPtr.</returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
/// <summary>
/// The wm activate
/// </summary>
private const int WM_ACTIVATE = 0x006;
/// <summary>
/// The wm activateapp
/// </summary>
private const int WM_ACTIVATEAPP = 0x01C;
/// <summary>
/// The wm ncactivate
/// </summary>
private const int WM_NCACTIVATE = 0x086;
/// <summary>
/// The wa inactive
/// </summary>
private const int WA_INACTIVE = 0;
/// <summary>
/// The wm mouseactivate
/// </summary>
private const int WM_MOUSEACTIVATE = 0x21;
/// <summary>
/// The ma noactivate
/// </summary>
private const int MA_NOACTIVATE = 3;
/// <summary>
/// WNDs the proc.
/// </summary>
/// <param name="m">要处理的 Windows <see cref="T:System.Windows.Forms.Message" />。</param>
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = new IntPtr(MA_NOACTIVATE);
return;
}
else if (m.Msg == WM_NCACTIVATE)
{
if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
{
if (m.LParam != IntPtr.Zero)
{
SetActiveWindow(m.LParam);
}
else
{
SetActiveWindow(IntPtr.Zero);
}
}
}
base.WndProc(ref m);
}
#endregion
WIn32 句柄
class Win32
{
/// <summary>
/// Struct Size
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Size
{
/// <summary>
/// The cx
/// </summary>
public Int32 cx;
/// <summary>
/// The cy
/// </summary>
public Int32 cy;
/// <summary>
/// Initializes a new instance of the <see cref="Size" /> struct.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
public Size(Int32 x, Int32 y)
{
cx = x;
cy = y;
}
}
/// <summary>
/// Struct BLENDFUNCTION
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
/// <summary>
/// The blend op
/// </summary>
public byte BlendOp;
/// <summary>
/// The blend flags
/// </summary>
public byte BlendFlags;
/// <summary>
/// The source constant alpha
/// </summary>
public byte SourceConstantAlpha;
/// <summary>
/// The alpha format
/// </summary>
public byte AlphaFormat;
}
/// <summary>
/// Struct Point
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
/// <summary>
/// The x
/// </summary>
public Int32 x;
/// <summary>
/// The y
/// </summary>
public Int32 y;
/// <summary>
/// Initializes a new instance of the <see cref="Point" /> struct.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
public Point(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
}
}
/// <summary>
/// The ac source over
/// </summary>
public const byte AC_SRC_OVER = 0;
/// <summary>
/// The ulw alpha
/// </summary>
public const Int32 ULW_ALPHA = 2;
/// <summary>
/// The ac source alpha
/// </summary>
public const byte AC_SRC_ALPHA = 1;
/// <summary>
/// Creates the compatible dc.
/// </summary>
/// <param name="hDC">The h dc.</param>
/// <returns>IntPtr.</returns>
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
/// <summary>
/// Gets the dc.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <returns>IntPtr.</returns>
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
/// <summary>
/// Selects the object.
/// </summary>
/// <param name="hDC">The h dc.</param>
/// <param name="hObj">The h object.</param>
/// <returns>IntPtr.</returns>
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
/// <summary>
/// Releases the dc.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="hDC">The h dc.</param>
/// <returns>System.Int32.</returns>
[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// Deletes the dc.
/// </summary>
/// <param name="hDC">The h dc.</param>
/// <returns>System.Int32.</returns>
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteDC(IntPtr hDC);
/// <summary>
/// Deletes the object.
/// </summary>
/// <param name="hObj">The h object.</param>
/// <returns>System.Int32.</returns>
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteObject(IntPtr hObj);
/// <summary>
/// Updates the layered window.
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="hdcDst">The HDC DST.</param>
/// <param name="pptDst">The PPT DST.</param>
/// <param name="psize">The psize.</param>
/// <param name="hdcSrc">The HDC source.</param>
/// <param name="pptSrc">The PPT source.</param>
/// <param name="crKey">The cr key.</param>
/// <param name="pblend">The pblend.</param>
/// <param name="dwFlags">The dw flags.</param>
/// <returns>System.Int32.</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 pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
/// <summary>
/// Exts the create region.
/// </summary>
/// <param name="lpXform">The lp xform.</param>
/// <param name="nCount">The n count.</param>
/// <param name="rgnData">The RGN data.</param>
/// <returns>IntPtr.</returns>
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
}
资源区:下载