我最近开始学习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
不存在。在 winform 中,解决方案将是一个非常小的变化:
温形
如何调用:
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>