從網上找了一段自定義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 活動的
請問何故?
uj5u.com熱心網友回復:
這種東應不著控制元件吧,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));
}
uj5u.com熱心網友回復:
應該是沒參考或者版本不對uj5u.com熱心網友回復:
System.Windows.Formsuj5u.com熱心網友回復:
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);
}
你上面看的原始碼是有問題的,我測驗過,加載的圖片不會呈現。只會呈現一個白色背景圓形
uj5u.com熱心網友回復:
這個是呈現的效果:
uj5u.com熱心網友回復:
空間都是在 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 屬性,二不應在內部寫死,否則就失去寫成控制元件的意義了
uj5u.com熱心網友回復:
對照查看
https://www.cnblogs.com/qiaoke/p/6124693.html
uj5u.com熱心網友回復:
這篇資料很有參考價值,感謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/281918.html
標籤:C#
上一篇:知乎首頁界面 推薦關注熱榜切換
