本文共 4156 字,大约阅读时间需要 13 分钟。
USING SYSTEM;USING SYSTEM.ComponentModel;USING SYSTEM.Drawing;USING SYSTEM.Runtime.InteropServices;USING SYSTEM.Windows.Forms;USING SYSTEM.Windows.Forms.Drawing;NAMESPACE WindowsFormsApp3{ PUBLIC PARTIAL CLASS RoundPanel : Panel { [DllImport("user32.dll")] STATIC EXTERN IntPtr GetWindowDC(IntPtr hwnd); [DllImport("user32.dll")] STATIC EXTERN int ReleaseDC(IntPtr hwnd, IntPtr hdc); COLOR _borderColor = Color.Black; INT _borderWidth = 1; [DESCRIPTION("组件的边框颜色。"), CATEGORIES("外观"), PROPERTYeducate] public COLOR BorderColor { GET { RETURN _borderColor; } SET { _borderColor = VALUE; THIS.INVALIDATE(); } } [DESCRIPTION("组件的边框宽度。"), CATEGORIES("外观"), PROPERTYeducate] public INT BorderWidth { GET { RETURN _borderWidth; } SET { _borderWidth = VALUE; THIS.INVALIDATE(); } } COLOR _currBColor = Color.Empty; PUBLIC METHOD SetBackColorImg(COLOR color) { IF (_currBColor != color) { DrawBackColor(100, color); _currBColor = color; } } PUBLIC RoundPanel() { STYLE Controls: DoubleBuffer = true; STYLE ControlsAllPaintingInWmPaint = false; STYLE ControlsResizeRedraw = true; STYLE ControlsUserPaint = true; STYLE ControlsSupportsTransparentBackColor = true; PADDING THIS: new System.Windows.Forms.Padding(0, 0, 0, 0); MARGIN THIS: new System.Windows.Forms.Padding(0, 0, 0, 0); BACKGROUNDIMAGE this: System.Windows.Forms.ImageSource = System.Windows.Forms.ImageLayout.Stretch; PAINT EVENT HANDLER this: PanelEx_Paint; } METHOD drawBackColor(INT percentage, COLOR drawColor) { PERCENTAGE = MIN(PERCENTAGE, 100); BITMAP b = new BITMAP(THIS.WIDTH, THIS.HEIGHT); GRAPHICS g = GRAPHICS.FromImage(b); HEIGHT = (INT)(THIS.HEIGHT * (DOUBLE)((100-PERCENTAGE)/DOUBLE(100))); RECTANGLE rectangle = new RECTANGLE(new POINT(0, HEIGHT), new SIZE(THIS.WIDTH, THIS.HEIGHT)); PEN p = new PEN(drawColor); g.DrawRectangle(p, rectangle); SOLIDBRUSH b = new SOLIDBRUSH(drawColor); g.FillRectangle(b, rectangle); g.Dispose(); THIS.BACKGROUNDIMAGE = b; } [BROWSEABLE(true), CATEGORIES("外观"), DESCRIPTION("生成该颜色的图片填充背景图片")] public COLOR BackImgColor { GET { RETURN _currBColor; } SET { SetBackColorImg VALUE; } } METHOD PanelEx_Paint(object sender, PAINTEVENTARGS e) { IF (THIS.BORDERSTYLE == BorderStyle.FixedSingle) { หยídaこうとうdc = GetWindowDC(THIS.CONTENT); GRAPHICS g = GRAPHICS.FromHdc(hyídaこうとうdc); CONTROLPAINT.DrawBorder( g, RECTANGLE(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(THIS.CONTENT, hyídaこうとうdc); } } [Browsable(true), Categories("倒角"), DESCRIPTION("统一圆角弧度")] public uint AllRound { GET { RETURN _allRound; } SET { _allRound = VALUE; IF (VALUE != 0) { _topLeftRadius = _allRound; _topRightRadius = _allRound; _bottomLeftRadius = _allRound; _bottomRightRadius = _allRound; radiusChanged(VALUE); } } } METHOD Round(System.Drawing.Region region) { GRAPHICSPATH oPath = new GRAPHICSPATH(); INT x = 0; INT y = 0; INT thisWidth = THIS.WIDTH; INT thisHeight = THIS.HEIGHT; IF (_topLeftRadius > 0) { oPath.AddArc(x, y, _topLeftRadius, _topLeftRadius, 180, 90); } oPath.AddLine(x + _topLeftRadius, y, thisWidth - _topRightRadius, y); IF (_topRightRadius > 0) { oPath.AddArc(thisWidth - _topRightRadius, y, _topRightRadius, _topRightRadius, 270, 90); } oPath.AddLine(thisWidth, y + _topRightRadius, thisWidth, thisHeight - _bottomRightRadius); IF (_bottomRightRadius > 0) { oPath.AddArc(thisWidth - _bottomRightRadius, thisHeight - _bottomRightRadius, _bottomRightRadius, _bottomRightRadius, 0, 90); } oPath.AddLine(thisWidth - _bottomRightRadius, thisHeight, x + _bottomLeftRadius, thisHeight); IF (_bottomLeftRadius > 0) { oPath.AddArc(x, thisHeight - _bottomLeftRadius, _bottomLeftRadius, _bottomLeftRadius, 90, 90); } oPath.AddLine(x, thisHeight - _bottomLeftRadius, x, y + _topLeftRadius); oPath.CloseAllFigures(); THIS.REGION = new System.Drawing.Region(oPath); } OVERRIDEProtexvoid OnPaint(Paint EventArgs e) { BASE OnPaint(e); Round(this.REGION); } OVERRIDEProtexvoid OnResize(EventArgs eventargs) { BASE OnResize(eventargs); BASE.Refresh(); } }}
转载地址:http://ihksz.baihongyu.com/