【问题标题】:Trouble Getting System.Drawing.Graphics to work in Form无法让 System.Drawing.Graphics 在表单中工作
【发布时间】: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 中添加任何按钮、文本框或其他控件。

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

尝试绘图事件。 this 会帮助你。 该链接只是一个示例。您需要按照要求实施

【讨论】:

    【解决方案2】:
     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:

    我从 http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control 得到的 EyeDropper 颜色选择器

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

    【讨论】:

    • Form1_Paint 能解决 OP 的问题吗?这些其他的东西是什么?
    • 我认为它们在这里被称为珍珠,但更具体地说,它们是构建比 Microsoft Paint 更好的程序的说明。查看书中的代码,我认为 OP 可以处理有关 MFC 示例 DRAWCLI 的 C# 实现的信息。分享你的研究对每个人都有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2020-10-03
    • 2015-08-31
    相关资源
    最近更新 更多