首页  编辑  

API绘制异或直线

Tags: /C#/图形图像/   Date Created:

using System . Runtime . InteropServices;

protected const int R2_BLACK = 1 ;

protected const int R2_NOTMERGEPEN = 2 ;

protected const int R2_MASKNOTPEN = 3 ;

protected const int R2_NOTCOPYPEN = 4 ;

protected const int R2_MASKPENNOT = 5 ;

protected const int R2_NOT = 6 ;

protected const int R2_XORPEN = 7 ;

protected const int R2_NOTMASKPEN = 8 ;

protected const int R2_MASKPEN = 9 ;

protected const int R2_NOTXORPEN = 10 ;

protected const int R2_NOP = 11 ;

protected const int R2_MERGENOTPEN = 12 ;

protected const int R2_COPYPEN = 13 ;

protected const int R2_MERGEPENNOT = 14 ;

protected const int R2_MERGEPEN = 15 ;

protected const int R2_WHITE = 16 ;

protected const int R2_LAST = 16 ;

protected const int PS_SOLID = 0 ;

[ DllImport ( "User32.dll" )]

protected static extern IntPtr GetDC( IntPtr hWnd);

[ DllImport ( "User32.dll" )]

protected static extern int ReleaseDC( IntPtr hWnd, IntPtr hDC);

[ DllImport ( "Gdi32.dll" )]

protected static extern int SetROP2( IntPtr hdc, int fnDrawMode);

[ DllImport ( "Gdi32.dll" )]

protected static extern int MoveToEx( IntPtr hdc, int x, int y, IntPtr lpPoint);

[ DllImport ( "Gdi32.dll" )]

protected static extern int LineTo( IntPtr hdc, int nXEnd, int nYEnd);

[ DllImport ( "Gdi32.dll" )]

protected static extern IntPtr CreatePen( int fnPenStyle, int nWidth, int crColor);

[ DllImport ( "Gdi32.dll" )]

protected static extern IntPtr SelectObject( IntPtr hdc, IntPtr hgdiobj);

[ DllImport ( "Gdi32.dll" )]

protected static extern int DeleteObject( IntPtr hObject);

private void button1_Click( object sender , EventArgs e)

{

    IntPtr hdc = GetDC(Handle);

    try

   {

        IntPtr hPen = CreatePen(PS_SOLID, 1 , 0x00ffffff );

        IntPtr hPenOld = SelectObject(hdc, hPen);

        int prevMix = SetROP2(hdc, R2_XORPEN);

       MoveToEx(hdc, 1 , 1 , IntPtr . Zero);

       LineTo(hdc, 16 , 100 );

       SetROP2(hdc, prevMix);

       SelectObject(hdc, hPenOld);

       DeleteObject(hPen);

   }

    finally

   {

       ReleaseDC(Handle, hdc);

   }

}