Introduction
As every programmer knows. the easiest and fastest method to draw self erasing lines is drawing line with XOR mode. But in .NET I could not see any method that makes self erasing lines, so it forced me to use GDI and GDI+ in mixed mode. I think, using GDI+ there is no way to draw self erasing lines. I've seen some methods invented to draw self erasing lines but about all of them are using BitBlt
ing or pushing the whole screen to buffer, and afterwards popping the buffer to erase them.
The Code
namespace GDIDraw
{
public enum PenStyles
{
PS_SOLID=0
,PS_DASH=1
,PS_DOT=2
,PS_DASHDOT=3
,PS_DASHDOTDOT=4
,PS_NULL=5
,PS_INSIDEFRAME=6
,PS_USERSTYLE=7
,PS_ALTERNATE=8
,PS_STYLE_MASK=0x0000000F
,PS_ENDCAP_ROUND= 0x00000000
,PS_ENDCAP_SQUARE= 0x00000100
,PS_ENDCAP_FLAT= 0x00000200
,PS_ENDCAP_MASK = 0x00000F00
,PS_JOIN_ROUND= 0x00000000
,PS_JOIN_BEVEL= 0x00001000
,PS_JOIN_MITER= 0x00002000
,PS_JOIN_MASK= 0x0000F000
,PS_COSMETIC= 0x00000000
,PS_GEOMETRIC= 0x00010000
,PS_TYPE_MASK= 0x000F0000
}
public enum drawingMode
{
R2_BLACK= 1
,R2_NOTMERGEPEN= 2
,R2_MASKNOTPEN= 3
,R2_NOTCOPYPEN= 4
,R2_MASKPENNOT= 5
,R2_NOT= 6
,R2_XORPEN= 7
,R2_NOTMASKPEN= 8
,R2_MASKPEN= 9
,R2_NOTXORPEN= 10
,R2_NOP= 11
,R2_MERGENOTPEN= 12
,R2_COPYPEN= 13
,R2_MERGEPENNOT= 14
,R2_MERGEPEN= 15
,R2_WHITE= 16
,R2_LAST= 16
}
public class GDI
{
private IntPtr hdc;
private System.Drawing.Graphics grp;
public void BeginGDI(System.Drawing.Graphics g)
{
grp=g;
hdc=grp.GetHdc();
}
public void EndGDI()
{
grp.ReleaseHdc(hdc);
}
public IntPtr CreatePEN(
PenStyles fnPenStyle,
int nWidth,
int crColor
)
{
return CreatePen(fnPenStyle,nWidth,crColor);
}
public bool DeleteOBJECT(IntPtr hObject)
{
return DeleteObject(hObject);
}
public IntPtr SelectObject(
IntPtr hgdiobj
)
{
return SelectObject(hdc,hgdiobj);
}
public void MoveTo(int X,int Y)
{
MoveToEx(hdc,X,Y,0);
}
public void LineTo(int X,int Y)
{
LineTo(hdc,X,Y);
}
public int SetROP2(drawingMode fnDrawMode)
{
return SetROP2(hdc,fnDrawMode);
}
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern int SetROP2(
IntPtr hdc,
drawingMode fnDrawMode
);
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern bool MoveToEx(
IntPtr hdc,
int X,
int Y,
int oldp
);
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern bool LineTo(
IntPtr hdc,
int nXEnd,
int nYEnd
);
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern IntPtr CreatePen(
PenStyles fnPenStyle,
int nWidth,
int crColor
);
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject
);
[System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]
private static extern IntPtr SelectObject(
IntPtr hdc,
IntPtr hgdiobj
);
public static int RGB(int R,int G,int B)
{
return (R|(G<<8)|(B<<16));
}
}
}