c#WinForm:删除或自定义按钮的“对焦矩形”

前端之家收集整理的这篇文章主要介绍了c#WinForm:删除或自定义按钮的“对焦矩形”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法禁用或更好地绘制自己的焦点矩形为常规的按钮控件! (虚线似乎是 Windowss 95ish)

我注意到,控件属性(FOR BUTTONS)没有ownerdrawfixed设置(我不知道这甚至是用于解决方案的路径,尽管我已经看到它用于自定义其他控件).

解决方法

得到这个权利比听起来更棘手.毫无疑问,自定义按钮绘画不可覆盖的原因之一.这样工作如预期: @H_502_8@using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; class MyButton : Button { private VisualStyleRenderer renderer; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.Focused && Application.RenderWithVisualStyles && this.FlatStyle == FlatStyle.Standard) { if (renderer == null) { VisualStyleElement elem = VisualStyleElement.Button.PushButton.Normal; renderer = new VisualStyleRenderer(elem.ClassName,elem.Part,(int)PushButtonState.Normal); } Rectangle rc = renderer.GetBackgroundContentRectangle(e.Graphics,new Rectangle(0,this.Width,this.Height)); rc.Height--; rc.Width--; using (Pen p = new Pen(Brushes.DarkGray)) { e.Graphics.DrawRectangle(p,rc); } } } }

猜你在找的C#相关文章