一个非常容易回答的问题 [英] A very easy question to answer
本文介绍了一个非常容易回答的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是有史以来最愚蠢的问题之一。好的,我有一个表格:
This is probably one of the most stupid questions ever asked. Okay, so I have a form:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Whatever
{
public class Form1 : Form
{
public Form1
{
InitializeComponent();
Graphics g = CreateGraphics();
g.Clear(Color.Black);
g.FillRectangle(Brushes.Tan, 0, 0, 20, 50);
}
}
}
无论我做什么都不会在屏幕上画出
正如我所说,这可能是一个愚蠢的问题,但是它会给你一些额外的分数。
No matter what I do nothing will be drawn on the screen
As I said this is probably a stupid question but hey it will give you some extra points.
推荐答案
覆盖OnPaint方法(受保护覆盖OnPaint(...)
然后将绘图代码放在那里,OnPaint方法有一个绘制事件参数,其中包含对图形对象的引用,所以你不需要单独创建它。
Override the OnPaint method (protected override OnPaint(... )
Then put your drawing code in there, the OnPaint method has a paint event args that includes a reference to the graphics object, so you don't need to create it separately.
public class Form1 : Form
{
public Form1
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.Black);
g.FillRectangle(Brushes.Red, 0, 0, 20, 50);
}
}
Form.OnPaint MSDN [ ^ ]
BTW,你用黑色清除图形然后画一个黑色矩形,所以你永远不会看到它。我将颜色更改为红色,您将看到背景现在为黑色,上面有一个红色矩形。
Form.OnPaint MSDN[^]
BTW, you are clearing the graphics with "Black" then painting a black rectangle, so you would never see it. I changed the color to red and you will see the background now black with a red rectangle on it.
这篇关于一个非常容易回答的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文