wince 绘图中使用橡皮擦。
这里说的绘图使用橡皮擦,就是把蒙在表面的图片擦掉,显示里面的图片。
原理很简单,就是用被蒙着的图片来创建一个画刷,然后在mousemove里面绘图就可以了
代码如下:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace phone_r1
{
public partial class Form1 : Form
{
[DllImport("coredll.dll",EntryPoint="CreatePen")]
public static extern IntPtr CreatePen(int nPenStyle, int nWidth, int color);
[DllImport("coredll.dll", EntryPoint = "CreatePatternBrush")]
public static extern IntPtr CreatePatternBrush(int hBitmap);
[DllImport("coredll.dll", EntryPoint = "SelectObject")]
private extern static IntPtr SelectObject(IntPtr hdc, IntPtr obj);
[DllImport("coredll.dll", EntryPoint = "DeleteObject")]
private extern static bool DeleteObject(IntPtr obj);
[DllImport("coredll.dll", EntryPoint = "Ellipse")]
private extern static bool Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2 );
private const int NULL_PEN = 5;
//string[] strArr;
//屏幕绘图对象
private Graphics ScreenGraphics;
//内存绘图对象
private Graphics BufferGraphics;
//内存图
private Bitmap imgOffscreen;
bool IsDown = false;
Bitmap bmp;
#region ......
//画刷句柄
private IntPtr g_hBrush;
//画笔句柄
private IntPtr g_hPen;
IntPtr g_hdc;
IntPtr g_oldp;
IntPtr g_oldb;
#endregion
public Form1()
{
InitializeComponent();
bmp = new Bitmap(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\PIC\\a20.bmp");
//图片句柄
int ibmp = bmp.GetHbitmap().ToInt32();
//画刷句柄
g_hBrush = CreatePatternBrush(ibmp);
ScreenGraphics = this.CreateGraphics();
imgOffscreen = new Bitmap(240, 320);
BufferGraphics = Graphics.FromImage(imgOffscreen);
BufferGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 240, 320));
g_hdc = BufferGraphics.GetHdc();
g_hPen = CreatePen(NULL_PEN, 1, 0);
IntPtr oldp = SelectObject(g_hdc, g_hPen);
IntPtr oldb = SelectObject(g_hdc, g_hBrush);
}
//private void Form1_Paint(object sender, PaintEventArgs e)
//{
//}
protected override void OnPaint(PaintEventArgs e)
{
ScreenGraphics.DrawImage(imgOffscreen, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
IsDown = true;
}
/// <summary>
/// 颜色值转换
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
private static int ColorToCOLORREF(Color color)
{
return ((color.R | (color.G << 8)) | (color.B << 16));
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Ellipse(g_hdc, e.X - 10, e.Y - 10, e.X + 10, e.Y + 10);
this.Invalidate();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
IsDown = false;
}
~Form1()
{
SelectObject(g_hdc, g_oldb);
SelectObject(g_hdc, g_oldp);
DeleteObject(g_hBrush);
DeleteObject(g_hPen);
BufferGraphics.ReleaseHdc(g_hdc);
}
}
}