当前位置: 首页 > news >正文

玩转控件:封装个带图片的Label控件

[ToolboxBitmap(typeof(Label))] publicpartialclassImageLabel : Label { private Image _image; privateint _imageWidth = 32; privateint _imageHeight = 32; privateint _imageTextSpacing = 5; private ContentAlignment _imageAlign = ContentAlignment.MiddleLeft; // 默认图片左中对齐 publicImageLabel() { this.DoubleClick += ImageLabel_DoubleClick; // 确保控件支持透明背景(如果需要) this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw, true); } #region Public Properties [Category("Appearance")] [Description("The image displayed.")] public Image Image { get { return _image; } set { _image = value; Invalidate(); OnImageChanged(EventArgs.Empty); } } [Category("Appearance")] [DefaultValue(32)] publicint ImageWidth { get { return _imageWidth; } set { _imageWidth = value; Invalidate(); } } [Category("Appearance")] [DefaultValue(32)] publicint ImageHeight { get { return _imageHeight; } set { _imageHeight = value; Invalidate(); } } [Category("Appearance")] [DefaultValue(5)] publicint ImageTextSpacing { get { return _imageTextSpacing; } set { _imageTextSpacing = value; Invalidate(); } } [Category("Appearance")] [Description("Determines the alignment of the image within the control.")] [DefaultValue(ContentAlignment.MiddleLeft)] public ContentAlignment ImageAlign { get { return _imageAlign; } set { _imageAlign = value; Invalidate(); } } #endregion #region Events [Category("Property Changed")] [Description("Occurs when the Image property value changes.")] publicevent EventHandler ImageChanged; protectedvirtualvoidOnImageChanged(EventArgs e) { ImageChanged?.Invoke(this, e); } #endregion #region Overridden Methods protectedoverridevoidOnPaint(PaintEventArgs e) { // 计算图片矩形(根据 ImageAlign 对齐) Rectangle imageRect = GetImageRectangle(); // 计算文字矩形(自动避开图片区域) Rectangle textRect = GetTextRectangle(imageRect); // 绘制图片 if (Image != null) { e.Graphics.DrawImage(Image, imageRect); } // 绘制文字 TextFormatFlags flags = TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter; // 根据文字对齐方式调整 flags(可选,这里简单使用左对齐) flags |= TextFormatFlags.Left; TextRenderer.DrawText(e.Graphics, this.Text, this.Font, textRect, this.ForeColor, flags); } #endregion #region Private Layout Methods ///<summary> /// 根据 ImageAlign 计算图片的绘制位置 ///</summary> private Rectangle GetImageRectangle() { int x = 0, y = 0; int clientWidth = this.ClientSize.Width; int clientHeight = this.ClientSize.Height; // 水平对齐 switch (ImageAlign) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: x = Padding.Left; break; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: x = (clientWidth - ImageWidth) / 2; break; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: x = clientWidth - Padding.Right - ImageWidth; break; } // 垂直对齐 switch (ImageAlign) { case ContentAlignment.TopLeft: case ContentAlignment.TopCenter: case ContentAlignment.TopRight: y = Padding.Top; break; case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleRight: y = (clientHeight - ImageHeight) / 2; break; case ContentAlignment.BottomLeft: case ContentAlignment.BottomCenter: case ContentAlignment.BottomRight: y = clientHeight - Padding.Bottom - ImageHeight; break; } // 边界检查,确保不超出控件范围 x = Math.Max(Padding.Left, Math.Min(x, clientWidth - Padding.Right - ImageWidth)); y = Math.Max(Padding.Top, Math.Min(y, clientHeight - Padding.Bottom - ImageHeight)); returnnew Rectangle(x, y, ImageWidth, ImageHeight); } ///<summary> /// 计算文字区域,自动避开图片区域 ///</summary> private Rectangle GetTextRectangle(Rectangle imageRect) { Rectangle textRect = new Rectangle(Padding.Left, Padding.Top, this.ClientSize.Width - Padding.Left - Padding.Right, this.ClientSize.Height - Padding.Top - Padding.Bottom); // 如果图片为空,直接返回全区域 if (Image == null) return textRect; // 根据图片对齐方式,判断文字应该放在图片的哪一侧 // 简单策略:如果图片在左边,文字放在右边;图片在右边,文字放在左边;图片在顶部/底部则文字放在剩余区域 // 更完善的方案可以自由组合,这里演示最常见的几种 bool imageLeft = (ImageAlign == ContentAlignment.MiddleLeft || ImageAlign == ContentAlignment.TopLeft || ImageAlign == ContentAlignment.BottomLeft); bool imageRight = (ImageAlign == ContentAlignment.MiddleRight || ImageAlign == ContentAlignment.TopRight || ImageAlign == ContentAlignment.BottomRight); bool imageTop = (ImageAlign == ContentAlignment.TopCenter || ImageAlign == ContentAlignment.TopLeft || ImageAlign == ContentAlignment.TopRight); bool imageBottom = (ImageAlign == ContentAlignment.BottomCenter || ImageAlign == ContentAlignment.BottomLeft || ImageAlign == ContentAlignment.BottomRight); if (imageLeft) { int textLeft = imageRect.Right + ImageTextSpacing; textRect = new Rectangle(textLeft, textRect.Y, this.ClientSize.Width - textLeft - Padding.Right, textRect.Height); } elseif (imageRight) { int textRight = imageRect.Left - ImageTextSpacing; textRect = new Rectangle(Padding.Left, textRect.Y, Math.Max(0, textRight - Padding.Left), textRect.Height); } elseif (imageTop) { int textTop = imageRect.Bottom + ImageTextSpacing; textRect = new Rectangle(textRect.X, textTop, textRect.Width, this.ClientSize.Height - textTop - Padding.Bottom); } elseif (imageBottom) { int textBottom = imageRect.Top - ImageTextSpacing; textRect = new Rectangle(textRect.X, Padding.Top, textRect.Width, Math.Max(0, textBottom - Padding.Top)); } else// 居中情况,文字覆盖图片区域(或简单放在下方) { // 如果图片居中,文字默认放在图片下方 int textTop = imageRect.Bottom + ImageTextSpacing; textRect = new Rectangle(textRect.X, textTop, textRect.Width, this.ClientSize.Height - textTop - Padding.Bottom); } // 确保文字矩形有效 if (textRect.Width < 0) textRect.Width = 0; if (textRect.Height < 0) textRect.Height = 0; return textRect; } #endregion #region Double-click to upload image privatevoidImageLabel_DoubleClick(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Title = "请选择一张图片"; openFileDialog.Filter = "图像文件|*.jpg;*.jpeg;*.png;*.gif;*.bmp"; if (openFileDialog.ShowDialog() == DialogResult.OK) { this.Image = Image.FromFile(openFileDialog.FileName); } } } #endregion
http://www.jsqmd.com/news/1139293/

相关文章:

  • ARP(地址解析协议)与NDP(邻居发现协议)
  • Unity手游逆向:Assembly-CSharp.dll加密识别与内存转储实战指南
  • LLM嵌入运动规划内核:语义约束自动转数学表达
  • 计算机毕业设计之jsp汽车养护网站的设计与实现
  • 2026企业级Agent产品推荐,三大选型标准与5款客服Agent横评
  • 从花屏到丢帧:视频设备晶振选型 6 大致命误区,90% 研发都踩过
  • 【从零构建分布式在线评测系统】二、Judge0服务器上的心跳程序
  • OPA191 非反相放大器 PCB 布局:3 项关键优化将噪声降低 20dB(附对比图)
  • Windows Server 2008 R2 IIS 低权限 WebShell 提权实战
  • ArcGIS Pro 3.6.3学习版安装全解析:环境、许可与排错
  • Vue导航守卫学习
  • 煤炭井下通信链路隐患排查,GN-W10A 测试设备适配矿山复杂网络环境
  • 为什么说Notepad--是你跨平台文本编辑的最佳选择?
  • 如何创建openEuler新语言翻译团队?完整申请与审批流程指南
  • CryptoJS v4.0.0 模块化升级与实战:JavaScript加密库核心用法解析
  • 【Springboot毕设全套源码+文档】基于springboot蔬菜超市系统的设计与实现(丰富项目+远程调试+讲解+定制)
  • 秋冬服装营销缺创意?先知大模型重塑内容生产力
  • 2026最新微信小程序怎么做自己的店铺全流程
  • AI合规工具:AI合规审计工具的选型与使用指南
  • TLS证书撤销机制深度解析:从CRL、OCSP到Nginx实战配置
  • AI-Shoujo HF Patch完整安装与配置指南:从模组整合到创作平台搭建
  • AutoGLM安卓端侧部署实战:从ADB到本地大模型运行
  • 城市污水管网监测的主要内容
  • 在后台服务中使用 Scoped 服务,为什么总是报错?
  • 从闭源平台的数据困局,到搭建真正属于自己的 AI 知识库
  • CTF Web入门:从HTTP响应包中寻找Flag的侦察思维与实战方法
  • 影刀RPA新手教程:界面布局完全指南——指令区、画布区、属性区深度解析与使用技巧
  • MD5加密Java代码?别找了,就这30行直接抄
  • Pytest+Requests+Allure:构建现代化接口自动化测试框架的实践指南
  • 【2026深度解析】Web渗透测试全流程实战指南:从信息收集到AI攻防进阶