重绘目的

  • 1. 满足非默认主题下的标题栏样式
  • 2. 在保留停靠功能的同时进行重绘。

代码如下:

 public partial class FormEx: Form
{
public FormEx()
{
InitializeComponent();
TitleBar.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(TitleBar, true, null);
Icon = Properties.Resources.shark;
CloseButtonImage = Properties.Resources.close;
MaximumButtonImage = Properties.Resources.window_max;
MaximumNormalButtonImage = Properties.Resources.window;
MinimumButtonImage = Properties.Resources.window_min;
CaptionBackgroundColor = Color.FromArgb(, , );
CaptionHeight = ;
BackColor = Color.White;
ControlBackColor = Color.Transparent;
this.TransparencyKey = boderColor;
ControlActivedColor = DrawHelper.GetNearColor(Color.White, , -, -, -); TitleBar.SendToBack(); base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor|
ControlStyles.DoubleBuffer, true);
base.AutoScaleMode = AutoScaleMode.None;
} #region 公开变量
[Category("标题栏"), Description("关闭按钮图片")]
public Image CloseButtonImage { get; set; } [Category("标题栏"), Description("最大化按钮图片")]
public Image MaximumButtonImage { get; set; } [Category("标题栏"), Description("最大化默认按钮图片")]
public Image MaximumNormalButtonImage { get; set; } [Category("标题栏"), Description("最小化按钮图片")]
public Image MinimumButtonImage { get; set; } [Category("标题栏"), Description("标题栏按钮鼠标悬浮背景色"), DefaultValue(typeof(Color), "#000000")]
public Color ControlActivedColor { get; set; } [Category("标题栏"), Description("标题栏按钮默认状态背景色"), DefaultValue(typeof(Color))]
public Color ControlBackColor { get; set; } private int captionHeight;
[Category("标题栏"), Description("标题栏高度"), DefaultValue(typeof(int), "")]
public int CaptionHeight { get { return captionHeight; } set { captionHeight = value; TitleBar.Height = value; } } [Category("标题栏"), Description("标题位置")]
public ContentAlignment TitleAlign { set; get; } = ContentAlignment.MiddleLeft; private Color captionBackgroundColor;
[Category("标题栏"), Description("标题栏背景颜色"), DefaultValue(typeof(Color), "White")]
public Color CaptionBackgroundColor
{
get { return captionBackgroundColor; }
set
{
captionBackgroundColor = value;
TitleBar.BackColor = captionBackgroundColor;
}
}
#endregion #region 私有变量 private MouseState _mouseState = MouseState.Out;
private Rectangle closeRect = Rectangle.Empty; //关闭按钮范围
private Rectangle maxRect = Rectangle.Empty; //最大化按钮范围
private Rectangle minRect = Rectangle.Empty; //最小化按钮范围
private Rectangle captionRect; //标题范围 private int btnH = ; //按钮大小
private int boderWidth = ; //边框宽度
private Color boderColor = Color.FromArgb(, , );
#endregion #region 重绘事件
private void TitleBar_Paint(object sender, PaintEventArgs e)
{
captionRect = new Rectangle(, , TitleBar.Width, TitleBar.Height);
DrawTitle(e.Graphics);
DrawControlButton(e.Graphics);
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawBound(e.Graphics);
} /// <summary>
/// 绘制边框
/// </summary>
private void DrawBound(Graphics g)
{
Pen pen = new Pen(boderColor, boderWidth * );
Rectangle rectangle = new Rectangle(, , Width, Height);
g.DrawRectangle(pen, rectangle);
pen = new Pen(Color.Black, );
rectangle = new Rectangle(boderWidth - , boderWidth - , Width - boderWidth * + , Height - boderWidth * + );
g.DrawRectangle(pen, rectangle);
Padding = new Padding(boderWidth);
} /// <summary>
/// 绘制控制按钮
/// </summary>
private void DrawControlButton(Graphics g)
{
int x = ClientSize.Width - btnH - - boderWidth;
if (CloseButtonImage != null)
{
DrawButtonImage(ref closeRect, ref x, _mouseState == MouseState.CloseHover, g, CloseButtonImage);
}
if (MaximizeBox)
{
if (WindowState == FormWindowState.Maximized && MaximumNormalButtonImage != null)
DrawButtonImage(ref maxRect, ref x, _mouseState == MouseState.MaxHover, g, MaximumNormalButtonImage);
else if (MaximizeBox && WindowState != FormWindowState.Maximized && MaximumButtonImage != null)
DrawButtonImage(ref maxRect, ref x, _mouseState == MouseState.MaxHover, g, MaximumButtonImage);
}
if (MinimizeBox && MinimumButtonImage != null)
{
DrawButtonImage(ref minRect, ref x, _mouseState == MouseState.MinHover, g, MinimumButtonImage);
}
} private void DrawButtonImage(ref Rectangle rect, ref int x, bool isHover, Graphics g, Image image)
{
//rect = new Rectangle(x, (captionHeight - btnH) / 2, btnH, btnH);
rect = new Rectangle(x, , btnH, btnH);
Brush brush = new SolidBrush(isHover ? ControlActivedColor : ControlBackColor);
g.FillRectangle(brush, rect);
g.DrawImage(image, rect);
if (image == CloseButtonImage)
rect = new Rectangle(x, , btnH + boderWidth, btnH);
x -= btnH;
} /// <summary>
/// 绘制标题
/// </summary>
private void DrawTitle(Graphics g)
{
int x = ;
if (ShowIcon && Icon != null)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
ImageAttributes image = new ImageAttributes();
image.SetWrapMode(WrapMode.TileFlipXY);
using (Bitmap bitmap = Icon.ToBitmap())
{
Rectangle rec = new Rectangle(x, (captionHeight - btnH) / , CaptionHeight - , CaptionHeight - );
g.DrawImage(bitmap, rec, , , bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, image);
}
x += ;
}
if (!string.IsNullOrEmpty(Text))
{
int fontHeight = Size.Ceiling(g.MeasureString("Text", TitleBar.Font)).Height;
int fontWidth = Size.Ceiling(g.MeasureString(Text, TitleBar.Font)).Width;
Brush brush = new SolidBrush(ForeColor);
if (TitleAlign == ContentAlignment.MiddleLeft)
g.DrawString(Text, TitleBar.Font, brush, x, (CaptionHeight - fontHeight) / );
else if (TitleAlign == ContentAlignment.TopCenter)
g.DrawString(Text, TitleBar.Font, brush, (Width - fontWidth)/, boderWidth); }
}
#endregion #region 其他事件
private void TitleBar_MouseClick(object sender, MouseEventArgs e)
{
if (e.Clicks != || e.Button != MouseButtons.Left)
return;
switch (_mouseState)
{
case MouseState.CloseHover:
Close();
break;
case MouseState.MaxHover:
WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
return;
case MouseState.MinHover:
WindowState = FormWindowState.Minimized;
return;
}
_mouseState = MouseState.Normal;
} private void TitleBar_MouseDown(object sender, MouseEventArgs e)
{
if (_mouseState != MouseState.CaptionHover)
return; if (e.Clicks == )
{
Win32API.ReleaseCapture();
Win32API.SendMessage(Handle, Win32API.WM_SYSCOMMAND, Win32API.SC_MOVE + Win32API.HTCAPTION, );
}
else if (e.Clicks == && e.Button == MouseButtons.Left)
{
WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
}
} private void TitleBar_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
if (closeRect != Rectangle.Empty && closeRect.Contains(p))
_mouseState = MouseState.CloseHover;
else if (minRect != Rectangle.Empty && minRect.Contains(p))
_mouseState = MouseState.MinHover;
else if (maxRect != Rectangle.Empty && maxRect.Contains(p))
_mouseState = MouseState.MaxHover;
else if (captionRect != Rectangle.Empty && captionRect.Contains(p))
_mouseState = MouseState.CaptionHover;
else
_mouseState = MouseState.Normal; Invalidate(captionRect, true);
} private void TitleBar_MouseLeave(object sender, EventArgs e)
{
_mouseState = MouseState.Out;
Invalidate(captionRect, true);
} private void FormEx_SizeChanged(object sender, EventArgs e)
{
Invalidate(captionRect, true);
}
#endregion #region 调整窗口大小 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
if (width == Width + )
return;
base.SetBoundsCore(x, y, width, height, specified);
} protected override void WndProc(ref Message m)
{
if (_mouseState == MouseState.CloseHover || _mouseState == MouseState.MinHover)
{
base.WndProc(ref m);
return;
}
switch (m.Msg)
{
case 0x0084:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= )
if (vPoint.Y <= )
m.Result = (IntPtr)Win32API.Guying_HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)Win32API.Guying_HTBOTTOMLEFT;
else m.Result = (IntPtr)Win32API.Guying_HTLEFT;
else if (vPoint.X >= ClientSize.Width - )
if (vPoint.Y <= )
m.Result = (IntPtr)Win32API.Guying_HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)Win32API.Guying_HTBOTTOMRIGHT;
else m.Result = (IntPtr)Win32API.Guying_HTRIGHT;
else if (vPoint.Y <= )
m.Result = (IntPtr)Win32API.Guying_HTTOP;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)Win32API.Guying_HTBOTTOM;
break;
case 0x0201: //鼠标左键按下的消息
m.Msg = 0x00A1; //更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero; //默认值
m.WParam = new IntPtr(); //鼠标放在标题栏内
base.WndProc(ref m);
break;
case 0x0083:
if (m.WParam != IntPtr.Zero)
{
NCCALCSIZE_PARAMS rcsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS));
Marshal.StructureToPtr(rcsize, m.LParam, false);
}
m.Result = new IntPtr();
break;
default:
base.WndProc(ref m);
break;
}
}
#endregion
} public enum MouseState
{
Normal,
MaxHover,
MinHover,
CloseHover,
CaptionHover,
Out,
}

因为主要目的是为了保留窗体是停靠功能,所以不能使用无边框然后直接重绘标题栏。拦截了window绘制有效区域标题栏的消息,仍然存在很多不完善的地方,如拦截边框消息后没有重新绘制原边框位置的图形,导致了原区域的透明等等。

最新文章

  1. SQL Server附加数据库失败错误号:5120的解决办法
  2. [nRF51822] 1、一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO
  3. Maven打包遇到的一些问题(zhuan)
  4. bundle install rake-10.4.2
  5. ios -- cell的图片下载
  6. HDU - 5187 - zhx&amp;#39;s contest (高速幂+高速乘)
  7. iOS_15_通过代码自己定义cell_微博UI
  8. How to check for and disable Java in OS X
  9. QPointer很大程度上避免了野指针(使用if语句判断即可,类似于dynamic_cast),而且使用非常方便 good
  10. C#单例模式的三种写法 以及 继承面试题
  11. android 多线程Thread,Runnable,Handler,AsyncTask
  12. jwt token Example - Python
  13. J2EE--常见面试题总结 -- (二)
  14. 支持异步写入的日志类,支持Framework2.0
  15. 2018-11-13 中文代码示例之Programming in Scala学习笔记第二三章
  16. SqlServer三种常用窗口函数
  17. python django day 3 页面,自动 跳转,参数传递
  18. SAP HANA HDBSQL命令
  19. 实现输入框不可输入、解决Enable,Disable等不能更新值问题
  20. [C#]泛型约束在窗体定义上的使用

热门文章

  1. [校内训练20_01_19]ABC
  2. [CF1037F]Maximum Reduction
  3. DOCKER 学习笔记5 Springboot+nginx+mysql 容器编排
  4. ROS机器人之话题(一)
  5. 使用nginx构建一个具备缓存功能的反向代理服务器
  6. 视觉slam十四讲课后习题ch3-7
  7. python 中的反斜杠匹配的问题
  8. 《Python学习手册 第五版》 -第9章 元组、文件与其他核心类型
  9. 快速应用开发(RAD)平台 - 20 年的演进
  10. css中flex布局