123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- // ReSharper disable UnusedMember.Local
- // ReSharper disable IdentifierTypo
- // ReSharper disable MemberCanBePrivate.Global
- namespace BsWidget
- {
- public abstract partial class BaseForm : Form
- {
- private const int WM_NCLBUTTONDOWN = 0xA1;
- private const int HT_CAPTION = 0x2;
- private const int WS_EX_LAYERED = 0x00080000;
- public const byte AcSrcOver = 0x00;
- public const byte AcSrcAlpha = 0x01;
- public const int UlwAlpha = 0x00000002;
- [StructLayout(LayoutKind.Sequential)]
- public struct POINT
- {
- public int x;
- public int y;
- public POINT(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct SIZE
- {
- public int cx;
- public int cy;
- public SIZE(int cx, int cy)
- {
- this.cx = cx;
- this.cy = cy;
- }
- }
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- public struct Blendfunction
- {
- public byte BlendOp;
- public byte BlendFlags;
- public byte SourceConstantAlpha;
- public byte AlphaFormat;
- }
- [DllImport("user32", ExactSpelling = true, SetLastError = true)]
- public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize,
- IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref Blendfunction pblend, int dwFlags);
- [DllImport("user32", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr GetDC(IntPtr hWnd);
- [DllImport("user32", ExactSpelling = true)]
- public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
- [DllImport("gdi32", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr CreateCompatibleDC(IntPtr hDc);
- [DllImport("gdi32", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteDC(IntPtr hdc);
- [DllImport("gdi32", ExactSpelling = true)]
- public static extern IntPtr SelectObject(IntPtr hDc, IntPtr hObject);
- [DllImport("gdi32", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteObject(IntPtr hObject);
- private Size _viewSize;
- private Bitmap _buffedBitmap;
- public BaseForm() => InitializeComponent();
- protected override CreateParams CreateParams
- {
- get
- {
- var cp = base.CreateParams;
- cp.ExStyle |= WS_EX_LAYERED;
- return cp;
- }
- }
- protected override void OnSizeChanged(EventArgs e)
- {
- _viewSize = Size;
- base.OnSizeChanged(e);
- var old = _buffedBitmap;
- var sz = ViewSize;
- _buffedBitmap = new Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppArgb);
- UpdateGraphic();
- old?.Dispose();
- }
- public Size ViewSize
- {
- get { return _viewSize; }
- protected set
- {
- if (value.Width > 0 && value.Height > 0)
- {
- MinimumSize = value;
- MaximumSize = value;
- _viewSize = value;
- }
- }
- }
- protected void UpdateGraphic()
- {
- if (_buffedBitmap == null) return;
- using (var g = Graphics.FromImage(_buffedBitmap))
- {
- RenderGraphic(g);
- }
- var screenDc = GetDC(IntPtr.Zero);
- var memDc = CreateCompatibleDC(screenDc);
- var hBitmap = IntPtr.Zero;
- var oldBitmap = IntPtr.Zero;
- try
- {
- hBitmap = _buffedBitmap.GetHbitmap(Color.FromArgb(0)); // 创建GDI位图句柄,效率较低
- oldBitmap = SelectObject(memDc, hBitmap);
- var topPos = new POINT(Left, Top);
- var pointSource = new POINT(0, 0);
- var size = new SIZE(_buffedBitmap.Width, _buffedBitmap.Height);
- var blend = new Blendfunction
- {
- BlendOp = AcSrcOver,
- BlendFlags = 0,
- SourceConstantAlpha = 255,
- AlphaFormat = AcSrcAlpha
- };
- UpdateLayeredWindow(
- Handle, screenDc
- , ref topPos, ref size
- , memDc, ref pointSource
- , 0, ref blend,
- UlwAlpha);
- }
- catch (Exception ex)
- {
- Debug.Print(ex.ToString());
- }
- finally
- {
- SelectObject(memDc, oldBitmap);
- DeleteObject(hBitmap);
- DeleteObject(oldBitmap);
- ReleaseDC(IntPtr.Zero, screenDc);
- DeleteDC(screenDc);
- DeleteDC(memDc);
- }
- }
- protected abstract void RenderGraphic(Graphics g);
- }
- }
|