这里写目录标题
窗体类
设置窗体圆角
public partial class Form1 : Form
public Form1()
{
InitializeComponent();
SetFormRoundRectRgn(this, 5); //设置圆角
}
#region Public extern methods
[DllImport("gdi32.dll")]
public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
[DllImport("user32.dll")]
public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
public static extern int DeleteObject(int hObject);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
#endregion
/// <summary>
/// 设置窗体的圆角矩形
/// </summary>
/// <param name="form">需要设置的窗体</param>
/// <param name="rgnRadius">圆角矩形的半径</param>
public static void SetFormRoundRectRgn(Form form, int rgnRadius)
{
int hRgn = 0;
hRgn = CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius);
SetWindowRgn(form.Handle, hRgn, true);
DeleteObject(hRgn);
}
要在窗体大小改变的事件中添加 SetFormRoundRectRgn,不然窗体大小改变的时候会无法绘制回原样
private void Form1_SizeChanged(object sender, EventArgs e)
{
SetFormRoundRectRgn(this, 5);
}
使用无边框窗体时
FormBorderStyle 属性设置为None
this.FormBorderStyle = FormBorderStyle.None;
显示电脑状态栏
在窗体初始化事件Form1_Load或者构造函数中添加
this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;//在窗体初始化后添加一句代码 最大化的时候才会显示电脑状态栏
重绘窗体标题栏
在无边框窗体中添加一个Panel控件用于显示标题和最小化、最大化、关闭按钮
并且实现拖拽窗体
//控件绘制事件
private void panel1_Paint(object sender, PaintEventArgs e)
{
Rectangle vRectangle = new Rectangle(0, 0, panel_Top.Width, panel_Top.Height);
Graphics vGraphics = e.Graphics;
//设置渐变矩形
//LinearGradientMode.Vertical设置渐变方向
vGraphics.FillRectangle(new LinearGradientBrush(vRectangle, Color.FromArgb(50, 95, 119, 244), Color.FromArgb(100, 99, 113, 255), LinearGradientMode.Vertical), vRectangle);
StringFormat vStringFormat = new StringFormat();
vStringFormat.Alignment = StringAlignment.Center;
vStringFormat.LineAlignment = StringAlignment.Center;
Font vFont = new Font("黑体", 16);
vGraphics.DrawString("我是窗体标题", vFont, Brushes.BlanchedAlmond, vRectangle, vStringFormat);
vGraphics.Dispose();
}
//鼠标按下事件
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
//拖动窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void Bn_CloseForm_Click(object sender, EventArgs e)
{
this.Close();
}
private void Bn_MaximizeForm_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Maximized;
}
}
private void Bn_Minimize_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
}
}
自定义控件、重绘控件类
Panel——倒角、圆角、带标题
倒角、圆角的Panel
public partial class PolyonPanel : Panel
{
public PolyonPanel()
{
InitializeComponent();
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, false);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.Paint += new PaintEventHandler(PanelEx_Paint);
}
public PolyonPanel(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private Color backColor = Color.FromArgb(1, 28, 58);
[Category("自定义属性"), Description("组件的背景颜色。"), Browsable(true)]
public override Color BackColor
{
get {
return backColor; }
set
{
backColor = value;
this.Invalidate();
}
}
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
public enum polyonEnum
{
倒角,
圆角
}
public polyonEnum polyon;
[Category("自定义属性"), Description("设置形状"), Browsable(true), DefaultValue("属性默认值")]
public polyonEnum Polyon
{
get {
return polyon; }
set {
polyon = value; this.Invalidate(); }
}
Color _borderColor = Color.Black;
[Category("自定义属性"), Description("边框颜色。"), Browsable(true)]
public Color BorderColor
{
get {
return _borderColor; }
set {
_borderColor = value; this.Invalidate(); }
}
int _borderWidth = 1;
[Category("自定义属性"), Description("边框宽度。"), Browsable(true)]
public int BorderWidth
{
get {
return _borderWidth; }
set {
_borderWidth = value; this.Invalidate(); }
}
[Category("自定义属性"), Description("生成该颜色的图片填充背景图片"), Browsable(true)]
public Color BackImgColor
{
get {
return _currBColor; }
set {
SetBackColorImg