代码如下 如果要拖动他 需要加什么代码
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = e.Graphics;
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 250.0f;
float y = 200.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本 }
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = e.Graphics;
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 250.0f;
float y = 200.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本 }
然后重绘你的图,只是x,y都加上刚才求出的便宜量。请用双倍缓冲方式,否则picturebox的图像会有闪烁的现象。
MouseDown的时候,有当前鼠标坐标,去你记录的区域里找包含你鼠标当前坐标的对象。找到之后就知道该拖哪个了。其实,只是文字的话,用Label也可以,拖动的话,google一下如何拖动控件。那样更方便些
MouseMove事件里添加pictureBox1.Invalidate();
等我上IE
public partial class Form1 : Form
{
int x,y,a, b,c,d;
bool isPressDown;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
} private void Form1_Load(object sender, EventArgs e)
{ }
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = e.Graphics;
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//float x = 250.0f;
//float y = 200.0f;
if (x+a > 700)
{
x -= 700;
}
else if (x+a < 0)
{
x += 700;
}
if (y+b > 500)
{
y -= 500;
}
else if (y+b < 0)
{
y += 500;
}
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x+a, y+b);//绘制文本 } private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isPressDown)
{
a = e.X-c;
b = e.Y-d;
pictureBox1.Invalidate();
}
} private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isPressDown = true;
c = e.X;
d = e.Y;
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isPressDown = false;
x += a;
y += b;
pictureBox1.Invalidate();
a = b = c = d = 0;
}
}
{
if ((e.X > panel1.Location.X) && (e.Y > panel1.Location.Y) && (e.X < panel1.Location.X + panel1.Size.Width) && (e.Y < panel1.Location.Y + panel1.Height))
{
MessageBox.Show(" hit");
}
else
{
MessageBox.Show(" miss");
}
}永远不会hit,满足hit条件时Form1接收不到事件,因为有个视频控件,没有mousedown事件,我想判断其是否被点中,一直没解决,后来用不到了,没再寻找.
有没有办法让被盖住的也接收到事件?
嘿嘿,菜