自定义pictureBox 控件的问题

shijies 2021-04-28 10:34:44
从网上找了一段自定义pictureBox 控件的代码,这段代码的目的是将pictureBox 控件的形状改造成圆形,网上的代码是继承PictureBox类,重写了OnCreateControl方法和OnPaint,我的理解是在工程中加入MyPictureBox类,在主窗体中调用MyPictureBox类的OnPaint方法,参考网上的代码,我上机试试,我上机的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;



namespace WindowsFormsApp3
{
class MyPictureBox : PictureBox
{
protected override void OnCreateControl()
{
Rectangle rec = new Rectangle(0, 0, 72, 72);
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(rec);
// gp.AddEllipse(this.ClientRectangle);
Region region = new Region(gp);
this.Region = region;
gp.Dispose();
region.Dispose();
base.OnCreateControl();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Rectangle rec = new Rectangle(0, 0, 72, 72);
var g = pe.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawEllipse(Pens.White, rec);
}
}

}

编译后出现了一些异常:

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0246 未能找到类型或命名空间名“PictureBox”(是否缺少 using 指令或程序集引用?) WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\MyPictureBox.cs 13 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0115 “MyPictureBox.OnCreateControl()”: 没有找到适合的方法来重写 WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\MyPictureBox.cs 15 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0115 “MyPictureBox.OnPaint(PaintEventArgs)”: 没有找到适合的方法来重写 WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\MyPictureBox.cs 27 活动的


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0246 未能找到类型或命名空间名“PaintEventArgs”(是否缺少 using 指令或程序集引用?) WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\MyPictureBox.cs 27 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0201 只有 assignment、call、increment、decrement、await 和 new 对象表达式可用作语句 WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\Form1.cs 22 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0122 “MyPictureBox.OnPaint(PaintEventArgs)”不可访问,因为它具有一定的保护级别 WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\Form1.cs 22 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS1061 “MyPictureBox”未包含“Region”的定义,并且找不到可接受第一个“MyPictureBox”类型参数的可访问扩展方法“Region”(是否缺少 using 指令或程序集引用?) WindowsFormsApp3 E:\C#绘图\圆形按钮\WindowsFormsApp3\WindowsFormsApp3\MyPictureBox.cs 22 活动的

请问何故?
...全文
651 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
morliz子轩 2021-04-28
  • 打赏
  • 举报
回复
这个是呈现的效果:
morliz子轩 2021-04-28
  • 打赏
  • 举报
回复
demo:

public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
        {
            //四边圆角
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
            gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
            gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
            gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
            gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
            gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
            gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
            gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
            gp.CloseFigure();
            g.DrawPath(p, gp);
            gp.Dispose();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            DrawRoundRect(e.Graphics, Pens.Black, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1, 10);
        }
你上面看的源码是有问题的,我测试过,加载的图片不会呈现。只会呈现一个白色背景圆形
xuzuning 2021-04-28
  • 打赏
  • 举报
回复
System.Windows.Forms
紫魂一号 2021-04-28
  • 打赏
  • 举报
回复
应该是没引用或者版本不对
desperaso 2021-04-28
  • 打赏
  • 举报
回复
这种东应不着控件吧,paint一下就完了,什么panel、picbox、button都可以

public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
gp.CloseAllFigures();
return gp;
}

// panel的paint
private void panel1_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18,true, Color.FromArgb(90, 143, 0), Color.FromArgb(41, 67, 0));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString("其实我是个Panel", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
}

morliz子轩 2021-04-28
  • 打赏
  • 举报
回复
引用 7 楼 desperaso 的回复:
对照查看 https://www.cnblogs.com/qiaoke/p/6124693.html
这篇资料很有参考价值,感谢。
desperaso 2021-04-28
  • 打赏
  • 举报
回复
引用 4 楼 morliz子轩 的回复:
demo:

public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
{
//四边圆角
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
g.DrawPath(p, gp);
gp.Dispose();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawRoundRect(e.Graphics, Pens.Black, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1, 10);
}


你上面看的源码是有问题的,我测试过,加载的图片不会呈现。只会呈现一个白色背景圆形


对照查看
https://www.cnblogs.com/qiaoke/p/6124693.html
xuzuning 2021-04-28
  • 打赏
  • 举报
回复
空间都是在 System.Windows.Forms 中定义的,所以一定要 using System.Windows.Forms; 代码被你改错了 正确的是
 public class MyPictureBox : PictureBox
    {
        protected override void OnCreateControl()
        {
              GraphicsPath gp = new GraphicsPath();
              gp.AddEllipse(this.ClientRectangle);
            Region region = new Region(gp);
            this.Region = region;
            gp.Dispose();
            region.Dispose();
            base.OnCreateControl();
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            //Rectangle rec = new Rectangle(0, 0, 72, 72);
            var g = pe.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            //g.DrawEllipse(Pens.White, rec);
            g.DrawImage(Image, Point.Empty);/// 
        }
    }
长宽应在外部调整 wideh 和 height 属性,二不应在内部写死,否则就失去写成控件的意义了

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧