WinForm-扁平化设计之 Combobox样式重绘去掉边框
#region DropDownList样式重绘 /// <summary> /// 主要为DropDownList样式重绘(特定性较强) /// </summary> public partial class ComboboxEx : ComboBox { public ComboboxEx() { // DropDownStyle = ComboBoxStyle.DropDownList; } public Color BoardColor { get; set; } = Color.White; protected override void WndProc(ref Message m) { base.WndProc(ref m); //WM_PAINT = 0xf; 要求一个窗口重画自己,即Paint事件时 //WM_CTLCOLOREDIT = 0x133;当一个编辑型控件将要被绘制时发送此消息给它的父窗口; //通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色 //windows消息值表,可参考: if (m.Msg == 0xf || m.Msg == 0x133) { IntPtr hDC = GetWindowDC(m.HWnd); if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回 { return; } //建立Graphics对像 Graphics g = Graphics.FromHdc(hDC); g.FillRectangle(new SolidBrush(BackColor), new Rectangle(0, 0, Width, Height)); //画边框的 //ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), BoardColor, ButtonBorderStyle.Solid); //画坚线 //ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid); Point pA = new Point(Width - 20, Height / 2 - 3); Point pB = pA + new Size(6, 6); Point pC = pA + new Size(12, 0); g.DrawLine(new Pen(Color.White, 2), pA, pB); g.DrawLine(new Pen(Color.White, 2), pB, pC); if (this.SelectedIndex > -1) { string text = SelectedItem.ToString(); Size strSize = Size.Ceiling(g.MeasureString(text, this.Font)); g.DrawString(text, Font, new SolidBrush(ForeColor), 5, (Height - strSize.Height) / 2); } //g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height)); //释放DC ReleaseDC(m.HWnd, hDC); } } [DllImport("User32.dll")] public static extern IntPtr GetWindowDC(IntPtr hwnd); [DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); } #endregion
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/15132635.html
分类:
.Net-WinForm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下