我想自己做一个控件,实现最基本的功能:
在窗体上单击后,在该位置创建一个控件;可我就是实现不了?求助!
下面是控件的简单代码:using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace draw
{
/// <summary>
/// line 的摘要说明。
/// </summary>
public class new1 : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; System.Drawing.Region r;//设置窗口区域 public new1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化
this.Refresh(); } protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
draw(e.Graphics);
} public void draw(Graphics graphics)
{ //绘制边框
System.Drawing.Drawing2D.GraphicsPath gp=new
System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
Rectangle rectBorder=new Rectangle();
Pen borderPen=new Pen(System.Drawing.Brushes.Red,2); rectBorder.X = 7;
rectBorder.Y = 7;
rectBorder.Height = 15;
rectBorder.Width = 15; graphics.DrawRectangle(borderPen, rectBorder);
System.Drawing.SolidBrush myb=new SolidBrush(Color.YellowGreen);
graphics.FillRectangle(myb,rectBorder);
gp.AddRectangle(rectBorder);
r=new Region(gp);
this.Region=r; graphics.Dispose(); } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
在窗体上单击后,在该位置创建一个控件;可我就是实现不了?求助!
下面是控件的简单代码:using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace draw
{
/// <summary>
/// line 的摘要说明。
/// </summary>
public class new1 : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; System.Drawing.Region r;//设置窗口区域 public new1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化
this.Refresh(); } protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
draw(e.Graphics);
} public void draw(Graphics graphics)
{ //绘制边框
System.Drawing.Drawing2D.GraphicsPath gp=new
System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
Rectangle rectBorder=new Rectangle();
Pen borderPen=new Pen(System.Drawing.Brushes.Red,2); rectBorder.X = 7;
rectBorder.Y = 7;
rectBorder.Height = 15;
rectBorder.Width = 15; graphics.DrawRectangle(borderPen, rectBorder);
System.Drawing.SolidBrush myb=new SolidBrush(Color.YellowGreen);
graphics.FillRectangle(myb,rectBorder);
gp.AddRectangle(rectBorder);
r=new Region(gp);
this.Region=r; graphics.Dispose(); } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
graphics.Dispose();
删除试试。
试问,你窗体上的click事件中,是否在加载该控件后是否让其显示,并给其大小及位置,最重要的是其是否已添加到form.controls当中。
如果你没用form1.controls.add(new new1()),那么,你的控件当然不会在窗体上显示
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
n=new new1();
//n.Name="new"+i;
n.Width=100;
n.Height=100;
n.Top=e.Y;
n.Left=e.X;
n.Visible=true;
//i++;
this.Controls.Add(n);
}
//private int i=1;
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
n=new new1();
//n.Name="new"+i;
n.Width=100;
n.Height=100;
n.Top=e.Y;
n.Left=e.X;
n.Visible=true;
//i++;
this.Controls.Add(n);
}
删除试过了,不行。
窗体代码应该没错,我有用usercontrol试过了,可以创建:
如下
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
new1 n1=new new1();//new1为控件的类名
n1.Top=e.X;
n1.Left=e.Y;
this.Controls.Add(n1);
}