Win32API类

因为要使用SetROP2函数获得橡皮线效果,系统中使用了较多的API函数,除了SetROP2外,还包括CreatePen, CreateSolidBrush, GetStockObject, SelectObject, DeleteObject, MoveToEx,LineTo,Ellipse和Arc等,以及一个LPPOINT 结构。为了管理这些API函数,创建了一个Win32API类。关于API函数的声明和调用,请参见第3章的内容。需要注意的是,由于用到互操作,必须导入System.Runtime.InteropServices名字空间。Win32API类中用到的API函数的意义如表3-1所示。因为MoveToEx 等函数中要用到LPPOINT类型的参数,所以需要首先创建一个该类型的结构。

表3-1 Win32API类中声明的API函数的意义

Document Image
\[\]
code.vc#.net
public class Win32API
{
    public Win32API()
    {
    }
     [StructLayout(LayoutKind.Sequential)]public struct LPPOINT
    {
        long x;
        long y;
     }
     [DIlImportAttribute("gdi32.dll")]
    public static extern bool SetROP2(IntPtr hdc,int nDrawMode);
     [DIlImport("gdi32.dll")]
    public static extern bool GetROP2(IntPtr hdc);
     [DIIImport("gdi32.dll")]
    public static extern long CreatePen(int nPenStyle,
        int nWidth,int crColor);
     [DIlImport("gdi32.dll")]
    public static extern long CreateSolidBrush(int crColor);
     [DIlImport("gdi32.dll")]
    public static extern long GetStockObject(int nIndex);
     [DIlImport("gdi32.dll")]
    public static extern long SelectObject(IntPtr hdc,
        long hObject);
     [DIlImport("gdi32.dll")]
    public static extern long DeleteObjec(long hObject);
     [DIlImport("gdi32.dll")]
    public static extern bool MoveToEx(IntPtr hdc,
        int x,int y,LPPOINT lpPoint);
     [DIlImport("gdi32.dll")]
    public static extem bool LineTo(IntPtr hde
        int x,int y );
     [DIlImport("gdi32.dll")]
    public static extern bool Rectangle(IntPtr hdc,
        int X1, int YI,int X2,int Y2);
     [DIlImport("gdi32.dll")]
    public static extemn bool Ellipse(IntPtr hdc, int X1,
        int Y1, int X2,int Y2);
     [DIlImport("gdi32.dll")]
    public static extern bool Arc
         (IntPtr hdc,int X1,int Y1,
        int X2,int Y2,int X3,
        int Y3,int X4,int Y4);
}