111,130
社区成员
发帖
与我相关
我的任务
分享Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int hdc = (int)g.GetHdc();
Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 0);
drawer.DrawLine(0, 0, 100, 100);
g.ReleaseHdc();
this.pictureBox1.Image = bmp as Image;Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 6);参数改成了6,画出来的不是异或线,是一条白色的线。 /// <summary>
/// 传入创建DC的句柄和绘图模式
/// </summary>
/// <param name="hWnd"></param>
/// <param name="drawMode"></param>
public Win32XORPenDrawer(int hWnd, int drawMode)
{
//GetDC by hwnd
this.hWnd = hWnd;
IntPtr res = GetDC(new IntPtr(hWnd));
hDc = res.ToInt32();
SetROP2(res, new IntPtr(drawMode));
}
public Win32XORPenDrawer(int hWnd, int drawMode)
{
this.hDc = hWnd;
SetROP2((IntPtr)hDc, new IntPtr(drawMode));
}Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
int hdc = (int)g.GetHdc();
Win32XORPenDrawer drawer = new Win32XORPenDrawer(hdc, 0);
drawer.DrawLine(0, 0, 100, 100);
g.ReleaseHdc();
this.pictureBox1.Image = bmp as Image;using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Win32APIDraw
{
/// <summary>
/// 封装交互线效果的类
/// </summary>
public class Win32XORPenDrawer : IDisposable
{
#region 声明Win32 api
struct POINTAPI
{
public int x;
public int y;
}
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern bool MoveToEx(IntPtr hDC,
IntPtr x,
IntPtr y,
ref POINTAPI lpPoint);
[DllImport("gdi32.dll")]
private static extern IntPtr LineTo(IntPtr hdc, IntPtr x, IntPtr y);
[DllImport("gdi32.dll")]
private static extern IntPtr SetROP2(IntPtr hdc, IntPtr fnDrawMode);
#endregion
#region 构造函数
/// <summary>
/// 传入创建DC的句柄和绘图模式
/// </summary>
/// <param name="hWnd"></param>
/// <param name="drawMode"></param>
public Win32XORPenDrawer(int hWnd, int drawMode)
{
//GetDC by hwnd
this.hWnd = hWnd;
IntPtr res = GetDC(new IntPtr(hWnd));
hDc = res.ToInt32();
SetROP2(res, new IntPtr(drawMode));
}
~Win32XORPenDrawer()
{
Dispose(false);
}
#endregion
#region 私有成员
private int hDc;
private int hWnd;
private bool disposed = false;
private void Dispose(bool disposing)
{
if (!this.disposed)
{
//ReleaseDC
ReleaseDC(new IntPtr(hWnd), new IntPtr(hDc));
}
disposed = true;
}
#endregion
#region 公共接口
#region IDisposable 成员
public void Dispose()
{
Dispose(true);
}
#endregion
/// <summary>
/// 从x1, y1向x2, y2画线
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
public void DrawLine(int x1, int y1, int x2, int y2)
{
POINTAPI lpOld = new POINTAPI();
MoveToEx(new IntPtr(hDc), new IntPtr(x1), new IntPtr(y1), ref lpOld);
LineTo(new IntPtr(hDc), new IntPtr(x2), new IntPtr(y2));
}
public void MoveTo(int x, int y)
{
POINTAPI lpOld = new POINTAPI();
MoveToEx(new IntPtr(hDc), new IntPtr(x), new IntPtr(y), ref lpOld);
}
public void LineTo(int x, int y)
{
LineTo(new IntPtr(hDc), new IntPtr(x), new IntPtr(y));
}
#endregion
}
}