Csharp/C#教程:C#通过重写Panel改变边框颜色与宽度的方法分享

本文实例讲述了C#通过重写Panel改变边框颜色与宽度的方法。分享给大家供大家参考。具体实现方法如下:

usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Runtime.InteropServices; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Drawing; namespaceImageStudio { publicclassPanelEx:System.Windows.Forms.Panel { [DllImport("user32.dll")] privatestaticexternIntPtrGetWindowDC(IntPtrhwnd); [DllImport("user32.dll")] privatestaticexternintReleaseDC(IntPtrhwnd,IntPtrhdc); privateColor_borderColor=Color.Black; privateint_borderWidth=1; // //摘要: //获取或设置控件的边框颜色。 // //返回结果: //控件的边框颜色System.Drawing.Color。默认为System.Drawing.Color.Black //属性的值。 [Description("组件的边框颜色。"),Category("Appearance")] publicColorBorderColor { get { return_borderColor; } set { _borderColor=value; this.Invalidate(); } } // //摘要: //获取或设置控件的边框宽度。 // //返回结果: //控件的边框宽度int。默认为1 //属性的值。 [Description("组件的边框宽度。"),Category("Appearance")] publicintBorderWidth { get { return_borderWidth; } set { _borderWidth=value; this.Invalidate(); } } publicPanelEx() { SetStyle(ControlStyles.DoubleBuffer,true); SetStyle(ControlStyles.AllPaintingInWmPaint,false); SetStyle(ControlStyles.ResizeRedraw,true); SetStyle(ControlStyles.UserPaint,true); SetStyle(ControlStyles.SupportsTransparentBackColor,true); this.Paint+=newPaintEventHandler(PanelEx_Paint); } privatevoidPanelEx_Paint(objectsender,PaintEventArgse) { if(this.BorderStyle==BorderStyle.FixedSingle) { IntPtrhDC=GetWindowDC(this.Handle); Graphicsg=Graphics.FromHdc(hDC); ControlPaint.DrawBorder( g, newRectangle(0,0,this.Width,this.Height), _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid); g.Dispose(); ReleaseDC(Handle,hDC); } } } }

希望本文所述对大家的C#程序设计有所帮助。

上述就是C#学习教程:C#通过重写Panel改变边框颜色与宽度的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)! 您可能感兴趣的文章:详解C#用new和override来实现抽象类的重写区别C#重写ComboBox实现下拉任意组件的方法C#重写DataGridView在C#的类或结构中重写ToString方法的用法简介c#方法重写和隐藏的学习示例c#重写TabControl控件实现关闭按钮的方法C#中重载与重写区别分析C#运算符重载用法实例分析C#中重载相等(==)运算符示例C#中多态、重载、重写区别分析C#重载运算符详解基于C#方法重载的

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906746.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐