博客
关于我
C#winform可设置四个角倒角的panel控件
阅读量:544 次
发布时间:2019-03-09

本文共 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/

你可能感兴趣的文章
poj 3468 A Simple Problem with Integers 降维线段树
查看>>
poj 3468 A Simple Problem with Integers(线段树 插线问线)
查看>>
poj 3485 区间选点
查看>>
poj 3518 Prime Gap
查看>>
poj 3539 Elevator——同余类bfs
查看>>
Qt笔记——官方文档全局定义(三)Macros宏
查看>>
poj 3628 Bookshelf 2
查看>>
Qt笔记——官方文档全局定义(一)Types数据类型
查看>>
POJ 3670 DP LIS?
查看>>
POJ 3683 Priest John's Busiest Day (算竞进阶习题)
查看>>
POJ 3988 Selecting courses
查看>>
POJ 4020 NEERC John's inversion 贪心+归并求逆序对
查看>>
poj 4044 Score Sequence(暴力)
查看>>
POJ 基础数据结构
查看>>
POJ 题目3020 Antenna Placement(二分图)
查看>>
Poj(1797) Dijkstra对松弛条件的变形
查看>>
SpringBoot为什么不需要xml配置文件?
查看>>
POJ--2391--Ombrophobic Bovines【分割点+Floyd+Dinic优化+二分法答案】最大网络流量
查看>>
Qt笔记——SQLite初探QSqlDatabase QSqlQuery
查看>>
POJ-1163-The Triangle
查看>>