//窗体关闭时 protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); //在Form_FormClosing中添加代码实现窗体的淡出 FormWin32.AnimateWindow(this.Handle, 800, FormWin32.AW_BLEND | FormWin32.AW_HIDE); }
public void SetBits() { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Properties.Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = FormWin32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = FormWin32.CreateCompatibleDC(screenDC); try { FormWin32.Point topLoc = new FormWin32.Point(Left, Top); FormWin32.Size bitMapSize = new FormWin32.Size(Width, Height); FormWin32.BLENDFUNCTION blendFunc = new FormWin32.BLENDFUNCTION(); FormWin32.Point srcLoc = new FormWin32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = FormWin32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = FormWin32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = FormWin32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; FormWin32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, FormWin32.ULW_ALPHA); } catch (Exception ex) { } finally { if (hBitmap != IntPtr.Zero) { FormWin32.SelectObject(memDc, oldBits); FormWin32.DeleteObject(hBitmap); } FormWin32.ReleaseDC(IntPtr.Zero, screenDC); FormWin32.DeleteDC(memDc); } }
//Show或Hide被调用时 protected override void OnVisibleChanged(EventArgs e) { if (Visible) { //启用窗口淡入淡出 if (!DesignMode) { //淡入特效 FormWin32.AnimateWindow(this.Handle, 800, FormWin32.AW_BLEND | FormWin32.AW_ACTIVATE); } //判断不是在设计器中 base.OnVisibleChanged(e); } else { base.OnVisibleChanged(e); FormWin32.AnimateWindow(this.Handle, 800, FormWin32.AW_BLEND | FormWin32.AW_HIDE); } }
/// <summary> /// 使窗口有鼠标穿透功能 /// </summary> private void CanPenetrate() { int intExTemp = FormWin32.GetWindowLong(this.Handle, FormWin32.GWL_EXSTYLE); int oldGWLEx = FormWin32.SetWindowLong(this.Handle, FormWin32.GWL_EXSTYLE, FormWin32.WS_EX_TRANSPARENT | FormWin32.WS_EX_LAYERED); }