我希望能够将图形绘制到窗体窗口而不是图片框。但是,表单窗口似乎没有捕获mousemove事件。
namespace CollisionTest
{
public partial class Form1 : Form
{
private Graphics paper;
private Pen pen;
public Form1()
{
InitializeComponent();
//paper = pictureBox1.CreateGraphics();
paper = this.CreateGraphics();
pen = new Pen(Color.Blue);
pen.Width = 5;
this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form1_MouseMove);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
paper.Clear(Form1.ActiveForm.BackColor);
paper.DrawRectangle(pen, e.X + 10, this.Height - 20, 50, 10);
}
}
}
答案 0 :(得分:0)
paper.Clear方法清除整个绘图表面并使用指定的背景颜色填充它。
因此,当您移动鼠标时,首先清除图形对象并绘制一些东西,这样您就看不到任何东西。 测试删除:
paper.Clear(Form1.ActiveForm.BackColor);
来自您的代码
答案 1 :(得分:0)
看起来你想要一个" pong paddle"穿过表格的底部?
只需将this.Height
更改为this.ClientRectangle.Height
:
public partial class Form1 : Form
{
private Pen pen;
private Graphics paper;
public Form1()
{
InitializeComponent();
pen = new Pen(Color.Blue, 5);
paper = this.CreateGraphics();
this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form1_MouseMove);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
paper.Clear(this.BackColor);
paper.DrawRectangle(pen, e.X + 10, this.ClientRectangle.Height - 20, 50, 10);
}
}