【发布时间】:2013-06-19 02:32:12
【问题描述】:
我正在尝试在 Visual Studio 中制作 C# Windows 表单,以便可以在表单上绘图(如 Microsoft Paint 的基本版本)。我正在研究 C# 2012 书中的一个示例。我已经逐字编写了代码,但是当我构建并运行程序时,我实际上无法在表单上绘制任何内容。代码编译成功,没有任何错误。任何人都可以看到可以改进代码的地方,以便我可以成功地在表单上绘制吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace paint3
{
public partial class Form1 : Form
{
bool shouldPaint = false;
public Form1() // constructor
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
shouldPaint = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
shouldPaint = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)
{
using (Graphics graphics = CreateGraphics())
{
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
}
}
}
}
}
就 Form1 而言,它只是我在 Visual Studio 2012 中单击“新建 Windows 窗体应用程序”时创建的一个空白表单。我没有在 Form1 中添加任何按钮、文本框或其他控件。
【问题讨论】:
-
你需要把它放在Paint事件中
-
与论坛网站不同,我们不使用“谢谢”、“感谢任何帮助”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.