我最近开始用c#学习编程。首先,我画了一个简单的圆圈,但是我对#34; 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);
}
}
}
}
答案 0 :(得分:3)
首先,您已经为winforms
制作了一种方法(如果您需要在.Forms
中导入wpf
,您应该知道它的错误)。 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>