星期零

技术改变生活,分享让我们快乐!
随笔 - 159, 文章 - 0, 评论 - 234, 阅读 - 44万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现。

一、设置按钮本身属性

1
2
3
buttonBubufx.FlatStyle = FlatStyle.Flat;
buttonBubufx.BackColor = Color.SkyBlue;
buttonBubufx.FlatAppearance.BorderColor = buttonBubufx.BackColor;

  

 

二、重绘,设置按钮的Region

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static int WM_NCPAINT = 0x0085;
       private static int WM_ERASEBKGND = 0x0014;
       private static int WM_PAINT = 0x000F;
 
       [DllImport("user32.dll")]
       static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
 
       [DllImport("user32.dll")]
       static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
 
       protected override void WndProc(ref Message m)
       {
           base.WndProc(ref m);
           if (m.Msg == WM_NCPAINT || m.Msg == WM_ERASEBKGND || m.Msg == WM_PAINT)
           {
               IntPtr hdc = GetDCEx(m.HWnd, (IntPtr)1, 1 | 0x0020);
 
               if (hdc != IntPtr.Zero)
               {
                   Graphics graphics = Graphics.FromHdc(hdc);
                   Color borderColor = Color.HotPink;
 
                   Rectangle rectangle = new Rectangle(textBox1.Location.X, textBox1.Location.Y + (25), textBox1.Width, textBox1.Height);
                   ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
                   m.Result = (IntPtr)1;
                   ReleaseDC(m.HWnd, hdc);
 
               }
           }
       }

  

原帖地址:winform设置button的边框颜色,或取消边框颜色,不显示边框。

bubuko.com提供,禁止转载。

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示