我在一个textBox中画一条直线,输入文字之后直线就消失了,如果先写文字再画下的话,鼠标在别处点一下直线也会消失,我想要那种又能画图又能写文本的效果,怎么做
代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool paint=false;
Point point1;
Pen redpen = new Pen(Color.Red, 3);
int y;
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
if (paint == true)
{
point1 = new Point(e.X, e.Y);
y = e.Y;
}
} private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint == true)
{
Point point2 = new Point(e.X, y);
Graphics g = this.CreateGraphics();
textBox1.CreateGraphics().DrawLine(redpen, point1, point2);
}
} private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
}
}
代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool paint=false;
Point point1;
Pen redpen = new Pen(Color.Red, 3);
int y;
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
if (paint == true)
{
point1 = new Point(e.X, e.Y);
y = e.Y;
}
} private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint == true)
{
Point point2 = new Point(e.X, y);
Graphics g = this.CreateGraphics();
textBox1.CreateGraphics().DrawLine(redpen, point1, point2);
}
} private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
}
}
这里有个自定义控件的c#例子,可以参考下/
protected override void OnPaint(PaintEventArgs e)
函数