无法使System.Drawing.Graphics在窗体中工作 [英] Trouble Getting System.Drawing.Graphics to work in Form

查看:77
本文介绍了无法使System.Drawing.Graphics在窗体中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Visual Studio中制作一个C#Windows窗体,以便可以在该窗体上绘制(就像Microsoft Paint的基本版本一样)。我正在研究C#2012本书中的示例。我已经按原样编写了代码,但是当我构建并运行该程序时,实际上无法在表单上绘制任何内容。代码成功编译,没有任何错误。谁能看到代码可以改进的地方,以便我可以成功地使用表格?

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;

名称空间paint3
{
公共局部类Form1:表单
{
bool shouldPaint = false;

public Form1()//构造函数
{
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(对象发送者,MouseEventArgs e)
{
if(shouldPaint)
{
using(Graphics graphics = CreateGraphics())
{
graphics.FillEllipse(new SolidBrush(Color.BlueViolet),eX,eY,4,4);
}
}
}
}
}



就Form1而言,它只是在Visual Studio 2012中单击新建Windows窗体应用程序时创建的空白窗体。我没有向Form1添加任何按钮,文本框或其他控件。

解决方案

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




我正在尝试在Visual Studio中制作一个C#Windows表单,以便可以在该表单上绘制(例如Microsoft Paint的
基本版本)。我正在通过
a C#2012书中的示例进行研究。


Alex Fr在他的< href = http://www.codeproject.com/Articles/8494/DrawTools rel = nofollow noreferrer> DrawTools文章,这些工具可作为



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


I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book. I have written the code verbatim, but when I build and run the program, I cannot actually draw anything on the form. The code compiles successfully without any errors. Can anyone see where the code can be improved so that I can successfully draw on the form?

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);
            }
        }
    }
}
}

As far as Form1 is concerned, it's simply a blank form that is created when I click "New Windows Forms Application" in Visual Studio 2012. I haven't added any buttons, text boxes, or other controls to Form1.

解决方案

 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);
            }
        }
    }

I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book.

Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.

Here's a tool I recently wrote by adding to Draw Tool Redux, it creates Epilogs for Mathematica:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control

The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo

这篇关于无法使System.Drawing.Graphics在窗体中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆