基本信息
源码名称:WinForm窗口美化 实例源码
源码大小:0.33M
文件格式:.rar
开发语言:C#
更新时间:2017-01-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
漂亮的窗口美化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
namespace GDIPlusDemo
{
public class FormEx : FormBase
{
#region Field
//窗体圆角矩形半径
private int _radius = 5;
//是否允许窗体改变大小
private bool _canResize = true;
private Image _fringe = Image.FromFile(@".\Res\fringe_bkg.png");
private Image _formBkg = Image.FromFile(@".\Res\FormBkg\bkg_flower.jpg");
//系统按钮管理器
private SystemButtonManager _systemButtonManager;
#endregion
#region Constructor
public FormEx()
{
InitializeComponent();
FormExIni();
_systemButtonManager = new SystemButtonManager(this);
}
#endregion
#region Properties
[DefaultValue(typeof(byte), "5")]
public int Radius
{
get
{
return _radius;
}
set
{
if (_radius != value)
{
_radius = value;
this.Invalidate();
}
}
}
public bool CanResize
{
get
{
return _canResize;
}
set
{
if (_canResize != value)
{
_canResize = value;
}
}
}
public override Image BackgroundImage
{
get
{
return _formBkg;
}
set
{
if (_formBkg != value)
{
_formBkg = value;
Invalidate();
}
}
}
internal Rectangle IconRect
{
get
{
if (base.ShowIcon && base.Icon != null)
{
return new Rectangle(8, 6, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Width);
}
return Rectangle.Empty;
}
}
internal Rectangle TextRect
{
get
{
if (base.Text.Length != 0)
{
return new Rectangle(IconRect.Right 2, 4, Width - (8 IconRect.Width 2), Font.Height);
}
return Rectangle.Empty;
}
}
internal SystemButtonManager SystemButtonManager
{
get
{
if (_systemButtonManager == null)
{
_systemButtonManager = new SystemButtonManager(this);
}
return _systemButtonManager;
}
}
#endregion
#region Overrides
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
if (MaximizeBox) { cp.Style |= (int)WindowStyle.WS_MAXIMIZEBOX; }
if (MinimizeBox) { cp.Style |= (int)WindowStyle.WS_MINIMIZEBOX; }
//cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN; //防止因窗体控件太多出现闪烁
cp.ClassStyle |= (int)ClassStyle.CS_DropSHADOW; //实现窗体边框阴影效果
}
return cp;
}
}
protected override void OnCreateControl()
{
base.OnCreateControl();
RenderHelper.SetFormRoundRectRgn(this, Radius);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
RenderHelper.SetFormRoundRectRgn(this, Radius);
UpdateSystemButtonRect();
UpdateMaxButton();
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Win32.WM_ERASEBKGND:
m.Result = IntPtr.Zero;
break;
case Win32.WM_NCHITTEST:
WmNcHitTest(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Move);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Down);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Up);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
SystemButtonManager.ProcessMouseOperate(Point.Empty, MouseOperate.Leave);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw BackgroundImage
e.Graphics.DrawImage(_formBkg, ClientRectangle, new Rectangle(0, 0, _formBkg.Width, _formBkg.Height), GraphicsUnit.Pixel);
//draw form main part
RenderHelper.DrawFromAlphaMainPart(this, e.Graphics);
//draw system buttons
SystemButtonManager.DrawSystemButtons(e.Graphics);
//draw fringe
RenderHelper.DrawFormFringe(this, e.Graphics, _fringe, Radius);
//draw icon
if (Icon != null && ShowIcon)
{
e.Graphics.DrawIcon(Icon, IconRect);
}
//draw text
if (Text.Length != 0)
{
TextRenderer.DrawText(
e.Graphics,
Text, Font,
TextRect,
Color.White,
TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_systemButtonManager != null)
{
_systemButtonManager.Dispose();
_systemButtonManager = null;
_formBkg.Dispose();
_formBkg = null;
_fringe.Dispose();
_fringe = null;
}
}
}
#endregion
#region Private Methods
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormEx));
this.SuspendLayout();
//
// FormEx
//
this.ClientSize = new System.Drawing.Size(913, 510);
this.Font = new System.Drawing.Font("微软雅黑", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FormEx";
this.Text = "360安全卫士";
this.ResumeLayout(false);
}
private void FormExIni()
{
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
SetStyles();
}
private void SetStyles()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
}
private void WmNcHitTest(ref Message m) //调整窗体大小
{
int wparam = m.LParam.ToInt32();
Point mouseLocation = new Point(RenderHelper.LOWORD(wparam), RenderHelper.HIWORD(wparam));
mouseLocation = PointToClient(mouseLocation);
if (WindowState != FormWindowState.Maximized)
{
if (CanResize == true)
{
if (mouseLocation.X < 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPRIGHT);
return;
}
if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
return;
}
if (mouseLocation.Y < 3)
{
m.Result = new IntPtr(Win32.HTTOP);
return;
}
if (mouseLocation.Y > Height - 3)
{
m.Result = new IntPtr(Win32.HTBOTTOM);
return;
}
if (mouseLocation.X < 3)
{
m.Result = new IntPtr(Win32.HTLEFT);
return;
}
if (mouseLocation.X > Width - 3)
{
m.Result = new IntPtr(Win32.HTRIGHT);
return;
}
}
}
m.Result = new IntPtr(Win32.HTCLIENT);
}
private void UpdateMaxButton() //根据窗体的状态更换最大(还原)系统按钮
{
bool isMax = WindowState == FormWindowState.Maximized;
if (isMax)
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@".\Res\SystemButton\restore_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@".\Res\SystemButton\restore_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@".\Res\SystemButton\restore_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "还原";
}
else
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@".\Res\SystemButton\max_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@".\Res\SystemButton\max_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@".\Res\SystemButton\max_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "最大化";
}
}
protected void UpdateSystemButtonRect()
{
bool isShowMaxButton = MaximizeBox;
bool isShowMinButton = MinimizeBox;
Rectangle closeRect = new Rectangle(
Width - SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
SystemButtonManager.SystemButtonArray[0].NormalImg.Height);
//update close button location rect.
SystemButtonManager.SystemButtonArray[0].LocationRect = closeRect;
//Max
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[1].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
SystemButtonManager.SystemButtonArray[1].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[1].LocationRect = Rectangle.Empty;
}
//Min
if (!isShowMinButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = Rectangle.Empty;
return;
}
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
SystemButtonManager.SystemButtonArray[1].LocationRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
}
#endregion
}
}