private void WmNcpaint(ref Message m) { base.WndProc(ref m); checked { if (this.RenderWithVisualStyles()) { int iPartId = 1; int iStateId; if (base.Enabled) { if (base.ReadOnly) { iStateId = 6; } else { iStateId = 1; } } else { iStateId = 4; } NativeMethods.RECT rECT; NativeMethods.GetWindowRect(base.Handle, out rECT); rECT.Right -= rECT.Left; rECT.Bottom -= rECT.Top; rECT.Top = (rECT.Left = 0); IntPtr windowDC = NativeMethods.GetWindowDC(base.Handle); NativeMethods.RECT rECT2 = rECT; rECT2.Left += this.borderRect.Left; rECT2.Top += this.borderRect.Top; rECT2.Right -= this.borderRect.Right; rECT2.Bottom -= this.borderRect.Bottom; NativeMethods.ExcludeClipRect(windowDC, rECT2.Left, rECT2.Top, rECT2.Right, rECT2.Bottom); IntPtr hTheme = NativeMethods.OpenThemeData(base.Handle, "EDIT"); if (NativeMethods.IsThemeBackgroundPartiallyTransparent(hTheme, 1, 1) != 0) { NativeMethods.DrawThemeParentBackground(base.Handle, windowDC, ref rECT); } NativeMethods.DrawThemeBackground(hTheme, windowDC, iPartId, iStateId, ref rECT, IntPtr.Zero); NativeMethods.CloseThemeData(hTheme); NativeMethods.ReleaseDC(base.Handle, windowDC); m.Result = IntPtr.Zero; } } }
public void SetBits() { if (BackgroundImage != null) { //绘制绘图层背景 var bitmap = new Bitmap(BackgroundImage, Width, Height); if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } var oldBits = IntPtr.Zero; var screenDC = NativeMethods.GetDC(IntPtr.Zero); var hBitmap = IntPtr.Zero; var memDc = NativeMethods.CreateCompatibleDC(screenDC); try { var topLoc = new Point(Left, Top); var bitMapSize = new Size(Width, Height); var blendFunc = new Win32.Struct.BLENDFUNCTION(); var srcLoc = new Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = NativeMethods.SelectObject(memDc, hBitmap); blendFunc.BlendOp = AC.AC_SRC_OVER; blendFunc.SourceConstantAlpha = byte.Parse(Main.SkinOpacity.ToString()); blendFunc.AlphaFormat = AC.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; NativeMethods.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, ULW.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { NativeMethods.SelectObject(memDc, oldBits); NativeMethods.DeleteObject(hBitmap); } NativeMethods.ReleaseDC(IntPtr.Zero, screenDC); NativeMethods.DeleteDC(memDc); } } }
public static void SetBits(Form form, Bitmap bitmap, byte opcity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. var screenDc = NativeMethods.GetDC(IntPtr.Zero); var memDc = NativeMethods.CreateCompatibleDC(screenDc); var hBitmap = IntPtr.Zero; var oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = NativeMethods.SelectObject(memDc, hBitmap); var size = new Size(bitmap.Width, bitmap.Height); var pointSource = new Point(0, 0); var topPos = new Point(form.Left, form.Top); var blend = new Win32.Struct.BLENDFUNCTION(); blend.BlendOp = AC.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opcity; blend.AlphaFormat = AC.AC_SRC_ALPHA; NativeMethods.UpdateLayeredWindow(form.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, UpdateLayerWindowParameter.ULW_ALPHA); } finally { NativeMethods.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { NativeMethods.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from NativeMethods.GDI and it's working fine without any resource leak. NativeMethods.DeleteObject(hBitmap); } NativeMethods.DeleteDC(memDc); } bitmap.Dispose(); }
public void SetBits() { var image = new Bitmap(Main.Width + (Main.ShadowWidth * 2), Main.Height + (Main.ShadowWidth * 2)); var g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; DrawShadow(g); if (Image.IsCanonicalPixelFormat(image.PixelFormat) && Image.IsAlphaPixelFormat(image.PixelFormat)) { var zero = IntPtr.Zero; var dC = NativeMethods.GetDC(IntPtr.Zero); var hgdiobj = IntPtr.Zero; var hdc = NativeMethods.CreateCompatibleDC(dC); try { var pptDst = new Win32.Struct.POINT(Left, Top); var psize = new Win32.Struct.SIZE(Width, Height); var pblend = new Win32.Struct.BLENDFUNCTION(); var pprSrc = new Win32.Struct.POINT(0, 0); hgdiobj = image.GetHbitmap(Color.FromArgb(0)); zero = NativeMethods.SelectObject(hdc, hgdiobj); pblend.BlendOp = 0; pblend.SourceConstantAlpha = byte.Parse("255"); pblend.AlphaFormat = 1; pblend.BlendFlags = 0; NativeMethods.UpdateLayeredWindow(Handle, dC, ref pptDst, ref psize, hdc, ref pprSrc, 0, ref pblend, 2); return; } finally { if (hgdiobj != IntPtr.Zero) { NativeMethods.SelectObject(hdc, zero); NativeMethods.DeleteObject(hgdiobj); } NativeMethods.ReleaseDC(IntPtr.Zero, dC); NativeMethods.DeleteDC(hdc); } } throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); }
public static void SetBits(Form form, Bitmap bitmap) { // if (!haveHandle) return; if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } var oldBits = IntPtr.Zero; var screenDC = NativeMethods.GetDC(IntPtr.Zero); var hBitmap = IntPtr.Zero; var memDc = NativeMethods.CreateCompatibleDC(screenDC); try { var topLoc = new Point(form.Left, form.Top); var bitMapSize = new Size(bitmap.Width, bitmap.Height); var blendFunc = new Win32.Struct.BLENDFUNCTION(); var srcLoc = new Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = NativeMethods.SelectObject(memDc, hBitmap); blendFunc.BlendOp = AC.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = AC.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; NativeMethods.UpdateLayeredWindow(form.Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, UpdateLayerWindowParameter.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { NativeMethods.SelectObject(memDc, oldBits); NativeMethods.DeleteObject(hBitmap); } NativeMethods.ReleaseDC(IntPtr.Zero, screenDC); NativeMethods.DeleteDC(memDc); } }
public virtual void UpdateWindow(Bitmap bitmap, double opacity) { if (bitmap == null) { return; } if (Image.IsCanonicalPixelFormat(bitmap.PixelFormat) && Image.IsAlphaPixelFormat(bitmap.PixelFormat)) { IntPtr zero = IntPtr.Zero; IntPtr dC = NativeMethods.GetDC(IntPtr.Zero); IntPtr hObj = IntPtr.Zero; IntPtr hDC = NativeMethods.CreateCompatibleDC(dC); try { Point pptDst = new Point(base.Left, base.Top); Size psize = new Size(bitmap.Width, bitmap.Height); Struct.BLENDFUNCTION pblend = new Struct.BLENDFUNCTION(); Point pptSrc = new Point(0, 0); hObj = bitmap.GetHbitmap(Color.FromArgb(0)); zero = NativeMethods.SelectObject(hDC, hObj); pblend.BlendOp = 0; pblend.SourceConstantAlpha = (byte)((int)(255.0 * opacity)); pblend.AlphaFormat = 1; pblend.BlendFlags = 0; NativeMethods.UpdateWindow(base.Handle, dC, ref pptDst, ref psize, hDC, ref pptSrc, 0, ref pblend, 2); return; } finally { if (hObj != IntPtr.Zero) { NativeMethods.SelectObject(hDC, zero); NativeMethods.DeleteObject(hObj); } NativeMethods.ReleaseDC(IntPtr.Zero, dC); NativeMethods.DeleteDC(hDC); } } throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); }
private void WmNccalcsize(ref Message m) { base.WndProc(ref m); if (this.RenderWithVisualStyles()) { NativeMethods.NCCALCSIZE_PARAMS nCCALCSIZE_PARAMS = default(NativeMethods.NCCALCSIZE_PARAMS); NativeMethods.RECT rECT; if (m.WParam == IntPtr.Zero) { rECT = (NativeMethods.RECT)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.RECT)); } else { nCCALCSIZE_PARAMS = (NativeMethods.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.NCCALCSIZE_PARAMS)); rECT = nCCALCSIZE_PARAMS.rgrc0; } IntPtr windowDC = NativeMethods.GetWindowDC(base.Handle); IntPtr hTheme = NativeMethods.OpenThemeData(base.Handle, "EDIT"); NativeMethods.RECT rECT2; if (NativeMethods.GetThemeBackgroundContentRect(hTheme, windowDC, 1, 1, ref rECT, out rECT2) == 0) { rECT2.Inflate(-1, -1); this.borderRect = checked (new NativeMethods.RECT(rECT2.Left - rECT.Left, rECT2.Top - rECT.Top, rECT.Right - rECT2.Right, rECT.Bottom - rECT2.Bottom)); if (m.WParam == IntPtr.Zero) { Marshal.StructureToPtr(rECT2, m.LParam, false); } else { nCCALCSIZE_PARAMS.rgrc0 = rECT2; Marshal.StructureToPtr(nCCALCSIZE_PARAMS, m.LParam, false); } m.Result = new IntPtr(768); } NativeMethods.CloseThemeData(hTheme); NativeMethods.ReleaseDC(base.Handle, windowDC); } }