lz又开了一帖啊。呵呵丢失了事件,这么着吧。代码编辑器中找到 windows窗体设计器生成的代码,照我下面代码修改一下吧:#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(205, 238); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1";//主要是下面两句。呵呵 this .MouseMove += new System.Windows.Forms.MouseEventHandler (this.Form1_MouseMove ); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.ResumeLayout(false); } #endregion
上面那个Button1的就不用了。呵呵private void InitializeComponent() { this.SuspendLayout(); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); }
wzuomin怎么给你加分阿,太感谢了!!能否再告诉我原因就十分感谢了!!
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace gdiplustest0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Point p = new Point(e.X, e.Y); if (new Rectangle(0, 0, 100, 100).Contains(p)) this.Cursor = Cursors.Cross; else if (new Rectangle(100, 0, 100, 100).Contains(p)) this.Cursor = Cursors.Hand; else if (new Rectangle(0, 100, 100, 100).Contains(p)) this.Cursor = Cursors.VSplit; else if (new Rectangle(100, 100, 100, 100).Contains(p)) this.Cursor = Cursors.UpArrow; else this.Cursor = Cursors.Default; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.FillRectangle(Brushes.Blue, 0, 0, 100, 100); g.FillRectangle(Brushes.Red, 100, 0, 100, 100); g.FillRectangle(Brushes.Yellow, 0, 100, 100, 100); g.FillRectangle(Brushes.Green, 100, 100, 100, 100); } } }
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(205, 238);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";//主要是下面两句。呵呵
this .MouseMove += new System.Windows.Forms.MouseEventHandler (this.Form1_MouseMove );
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false); } #endregion
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace gdiplustest0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
if (new Rectangle(0, 0, 100, 100).Contains(p))
this.Cursor = Cursors.Cross;
else if (new Rectangle(100, 0, 100, 100).Contains(p))
this.Cursor = Cursors.Hand;
else if (new Rectangle(0, 100, 100, 100).Contains(p))
this.Cursor = Cursors.VSplit;
else if (new Rectangle(100, 100, 100, 100).Contains(p))
this.Cursor = Cursors.UpArrow;
else
this.Cursor = Cursors.Default;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Blue, 0, 0, 100, 100);
g.FillRectangle(Brushes.Red, 100, 0, 100, 100);
g.FillRectangle(Brushes.Yellow, 0, 100, 100, 100);
g.FillRectangle(Brushes.Green, 100, 100, 100, 100);
}
}
}
因为在生成窗体时,需要重新画窗体。你的程序需要安装一个Paint事件来重画你的窗体
或者重载一下OnPaint()方法就可以了。