[英]Drawing circle in canvas
我最近開始學習C#編程。 首先,我畫了一個簡單的圓圈,但是我對“ char” -e.Graphics有疑問。 我有必要的名稱空間,例如System.Drawing和System.windows.Form程序,是WPF應用程序所關心的。 我希望能夠輸入尺寸並按下按鈕來繪制圓圈。
namespace drawcircle
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void circle_Click(object sender, RoutedEventArgs e)
{
int iks = int.Parse(beginx.Text);
int igrek = int.Parse(beginy.Text);
int width = int.Parse(wid.Text);
int height = int.Parse(hei.Text);
draw.circle(iks, igrek, width, height);
}
class draw
{
public static void circle(int x, int y, int width, int height)
{
Pen color = new Pen(Color.Red);
System.Drawing.SolidBrush fillblack = new System.Drawing.SolidBrush(Color.Black);
Rectangle circle = new Rectangle(x, y, width, height);
Graphics g = e.Graphics;
g.DrawEllipse(color, circle);
}
}
}
}
首先,您已經為winforms
創建了一個方法(如果您需要在wpf
導入.Forms
,則應該知道它是錯誤的)。 諸如SolidBrush
和Color.Red
之類的東西都不存在wpf
。 在winforms中,解決方案將是很小的更改:
通話方式:
draw.circle(10, 20, 40, 40, this.CreateGraphics());
類:
class draw
{
public static void circle(int x, int y, int width, int height, Graphics g)
{
Pen color = new Pen(Color.Red);
System.Drawing.SolidBrush fillblack = new System.Drawing.SolidBrush(Color.Black);
Rectangle circle = new Rectangle(x, y, width, height);
g.DrawEllipse(color, circle);
}
}
對於wpf,我將嘗試執行以下操作:
通話方式:
draw.circle(10, 10, 100, 100, MainCanvas);
類:
class draw
{
public static void circle(int x, int y, int width, int height, Canvas cv)
{
Ellipse circle = new Ellipse()
{
Width = width,
Height = height,
Stroke = Brushes.Red,
StrokeThickness = 6
};
cv.Children.Add(circle);
circle.SetValue(Canvas.LeftProperty, (double)x);
circle.SetValue(Canvas.TopProperty, (double)y);
}
}
XAML:
將網格更改為畫布,並命名如下:
<Canvas Name="MainCanvas">
</Canvas>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.