麻烦让System.Drawing.Graphics在Form中工作

时间:2013-06-19 02:32:12

标签: c# .net winforms

我正在尝试在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而言,它只是一个空白表单,当我点击"新Windows表单应用程序"在Visual Studio 2012中。我还没有向Form1添加任何按钮,文本框或其他控件。

2 个答案:

答案 0 :(得分:1)

 private void Form1_Paint(object sender, EventArgs e)
    {
        if (shouldPaint)
        {
            using (Graphics graphics = CreateGraphics())
            {
                graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
            }
        }
    }
  

我正在尝试在Visual Studio中创建一个C#Windows窗体,以便我可以在窗体上绘制(就像一个   Microsoft Paint的基本版本)。我正在研究一个例子   一本C#2012书。

Alex Fr在DrawTools article中提供了一套优秀的绘图工具,这些工具是Draw Tool Redux的基础。

这是我最近写的一个工具,它添加到Draw Tool Redux,它为Mathematica创建了Epilogs:

enter image description here

我从http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control获得的EyeDropper拾色器

我获得的透明文本框:http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo

答案 1 :(得分:1)

尝试绘画的绘画事件。 this会帮助你。 链接只是一个例子。你需要按照要求实施