我通过这个方法画出了字符串:
Graphics graphics = this.CreateGraphics();
Font drawFont = new Font("Arial",16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF drawPoint = new PointF(150.0F,150.0F);
graphics.DrawString("字符串", drawFont, drawBrush, drawPoint);
但是我想要的效果是:让所画出的字符串跟着鼠标移动,且画出的字符串也是改变的。
我的思路是这样的:要先清除前面所画的字符,然后再重新画一次。
问题:无法用clear来清除,因为有背景图片。有哪位仁兄知道解决方法的,告知下,谢谢,有不知gdi32.dll有画字符串的异或接口没?
Graphics graphics = this.CreateGraphics();
Font drawFont = new Font("Arial",16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF drawPoint = new PointF(150.0F,150.0F);
graphics.DrawString("字符串", drawFont, drawBrush, drawPoint);
但是我想要的效果是:让所画出的字符串跟着鼠标移动,且画出的字符串也是改变的。
我的思路是这样的:要先清除前面所画的字符,然后再重新画一次。
问题:无法用clear来清除,因为有背景图片。有哪位仁兄知道解决方法的,告知下,谢谢,有不知gdi32.dll有画字符串的异或接口没?
this.Invalidate
public class StringClass
{
public string str = "字符串";
public Point p = new Point();
public StringClass() { }
}
StringClass sc = new StringClass();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
sc.p = e.Location;
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
graphics.DrawString(sc.str, drawFont, drawBrush, sc.p);
}
public class M_DRAW
{
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll", EntryPoint = "SetROP2")]
private static extern IntPtr SetROP2(IntPtr hdc, IntPtr fnDrawMode); [DllImport("gdi32.dll", EntryPoint = "CreatePen")]
public static extern IntPtr CreatePen(int penStype, int penWidth, int penColor); [DllImport("gdi32.dll", EntryPoint = "GetStockObject")]
private static extern IntPtr GetStockObject(int fnObject); [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll", EntryPoint = "Ellipse")]
public static extern bool Ellipse(IntPtr hdc, int left, int top, int right, int bottom); [DllImport("gdi32.dll", EntryPoint = "MoveToEx")]
public static extern IntPtr MoveToEx(IntPtr hdc, int x, int y, int p); [DllImport("gdi32.dll", EntryPoint = "LineTo")]
public static extern IntPtr LineTo(IntPtr hdc, int x, int y); [DllImport("gdi32.dll", EntryPoint = "LineTo")]
public static extern IntPtr DrawString(IntPtr hdc, int x, int y); #region C#异或画圆
/// <summary>
/// C#异或画圆
/// </summary>
/// <param name="control">System.Windows.Forms.Control</param>
/// <param name="centerPoint">圆心或起点</param>
/// <param name="endPoint">终点</param>
/// <param name="penColor">画笔颜色</param>
public static void DrawEBL(System.Windows.Forms.Control control, M_POINT centerPoint, M_POINT endPoint, int penColor)
{
IntPtr hdc = GetDC(control.Handle);//获取句柄
float deltx = endPoint.x - centerPoint.x;
float delty = endPoint.y - centerPoint.y;
float radius = (float)Math.Sqrt(Math.Pow(deltx, 2) + Math.Pow(delty, 2));//圆的半径 IntPtr R2_XORPEN = (IntPtr)7;
SetROP2(hdc, R2_XORPEN);//设置当前为异或画法
IntPtr pen = (IntPtr)CreatePen(0, 1, penColor);//创建画笔
IntPtr oldPen = SelectObject(hdc, pen);//添加画笔
int NULL_BRUSH = 5;
IntPtr hdcBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH)); Ellipse(hdc, (int)(centerPoint.x - radius), (int)(centerPoint.y - radius), (int)(centerPoint.x + radius), (int)(centerPoint.y + radius));//画圆 SetROP2(hdc, (IntPtr)12);//还原默认画法
SelectObject(hdc, oldPen);
SelectObject(hdc, hdcBrush);
DeleteObject(pen); ReleaseDC(control.Handle, hdc);
}
#endregion
}
鼠标移动时记录,x y 坐标即可
用一个 timer 控件
循环的画(15帧/秒,30帧/帧 就可以了60帧也可),就是 1/15 秒一次 timer 事件里执行下面的东西 new 一个图片对象(大小自己根据情况),图片生成的 Graphics
1、画底图
2、画字然后在把合成的图片整个画到 form 上 ok 了