using namespace System::Runtime::InteropServices; typedef void* HDC; [DllImport("gdi32", EntryPoint="LineTo")] extern "C" bool LineTo(HDC hDC, int nXEnd, int nYEnd); |
namespace GDI32API // 自定义的命名空间 { using namespace System; using namespace System::Runtime::InteropServices; typedef void* HDC; [DllImport("gdi32", EntryPoint="LineTo")] extern "C" bool LineTo(HDC hDC, int nXEnd, int nYEnd); } |
Graphics *g = this->panel1->CreateGraphics(); // 创建与panel1控件相关联的Graphics IntPtr hdc = g->GetHdc(); GDI32API::LineTo( (GDI32API::HDC)hdc,100, 200 ); g->ReleaseHdc( hdc ); |
public __gc class GDI32API { public: typedef void* HDC; [DllImport("gdi32", EntryPoint="LineTo")] static bool LineTo(HDC hDC, int nXEnd, int nYEnd); }; |
wtypes.h | C++ | 托管C++ | .NET类名 | 说明 |
GDI句柄 | void * | void * | IntPtr, UIntPtr | 32 位 |
BYTE | unsigned char | unsigned char | Byte | 8 位 |
SHORT | short | short | Int16 | 16 位 |
WORD | unsigned | short unsigned | short UInt16 | 16 位 |
INT | int | int | Int32 | 32 位 |
UINT | unsigned int | unsigned int | UInt32 | 32 位 |
LONG | long | long | Int32 | 32 位 |
BOOL | long | bool | Boolean | 32 位 |
DWORD | unsigned long | unsigned long | UInt32 | 32 位 |
ULONG | unsigned long | unsigned long | UInt32 | 32 位 |
CHAR | char | char | Char | 用 ANSI 修饰 |
LPSTR | char * | String * [in], StringBuilder * [in, out] | String [in], StringBuilder [in, out] | 用 ANSI 修饰 |
LPCSTR | const char * | String * | String | 用 ANSI 修饰 |
LPWSTR | wchar_t * | String * [in], StringBuilder * [in, out] | String [in], StringBuilder [in, out] | 用 Unicode 修饰 |
LPCWSTR | const wchar_t * | String * | String | 用 Unicode 修饰 |
FLOAT | float | float | Single | 32 位 |
DOUBLE | double | double | Double | 64 位 |
namespace GDI32API { using namespace System; using namespace System::Runtime::InteropServices; typedef void* HDC; [StructLayout(LayoutKind::Sequential)] public __value struct RECT { public: long left; // long或Int32 long top; long right; long bottom; }; [DllImport("gdi32", EntryPoint="GetClipBox")] extern "C" int GetClipBox(HDC hDC, RECT* rect); } |
[DllImport("gdi32", EntryPoint="TextOut")] extern "C" bool TextOut(HDC hDC, int x, int y, [MarshalAs(UnmanagedType::LPWStr)] String *str, int nNum); |
HRESULT New1(int ar[10]); HRESULT New2(double ar[10][20]); HRESULT New3(LPWSTR ar[10]); |
void New1([MarshalAs(UnmanagedType::LPArray, SizeConst=10)] int ar __gc[]); void New2([MarshalAs(UnmanagedType::LPArray, SizeConst=200)] double ar __gc[]); void New2([MarshalAs(UnmanagedType::LPArray, ArraySubType=UnmanagedType::LPWStr, SizeConst=10)] String[] ar); |
namespace GDI32 { using namespace System; using namespace System::Runtime::InteropServices; typedef void* HDC; typedef void* HPEN; [StructLayout(LayoutKind::Sequential)] public __value struct POINT { public: long x; // long或Int32 long y; }; [DllImport("gdi32", EntryPoint="SetROP2")] extern "C" int SetROP2(HDC hDC, int fnDrawMode); // 设置光栅操作模式 [DllImport("gdi32", EntryPoint="CreatePen")] extern "C" HPEN CreatePen(int fnPenStyle, int nWidth, unsigned long crColor); // 创建画笔 [DllImport("gdi32", EntryPoint="SelectObject")] extern "C" void* SelectObject(HDC hDC, void* hGdiobj); // 选入GDI属性对象 [DllImport("gdi32", EntryPoint="LineTo")] extern "C" bool LineTo(HDC hDC, int nXEnd, int nYEnd); // 画线 [DllImport("gdi32", EntryPoint="MoveToEx")] extern "C" bool MoveTo(HDC hDC, int x, int y, POINT* pt); // 移动当前位置 } |
private: System::Void On_MouseMove(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { …… Graphics *g = this->panel1->CreateGraphics(); // 创建与panel1控件相关联的Graphics IntPtr hdc = g->GetHdc(); GDI32::HPEN hPen = GDI32::CreatePen( 0, 0, 0xA0A0A0 ); // 创建灰色画笔 GDI32::SelectObject( (GDI32::HDC)hdc, hPen ); // 选入画笔 GDI32::SetROP2( (GDI32::HDC)hdc, 7 ); // 7表示XORPEN模式 GDI32::MoveTo( (GDI32::HDC)hdc, pt.X, pt.Y, NULL ); GDI32::LineTo( (GDI32::HDC)hdc, ptPrev.X, ptPrev.Y ); ptPrev = Point( e->X, e->Y ); GDI32::MoveTo( (GDI32::HDC)hdc, pt.X, pt.Y, NULL ); GDI32::LineTo( (GDI32::HDC)hdc, ptPrev.X, ptPrev.Y ); g->ReleaseHdc( hdc ); } |
图1 GDI+和GDI混合编程实例 |
extern "C" __declspec(dllexport) void DrawGDIXorSolidLine( HDC hDC, DWORD color, int nWidth, int x1, int y1, int x2, int y2 ) { HPEN pen = ::CreatePen( 0, nWidth, color ); HPEN oldPen = (HPEN)::SelectObject( hDC, pen ); int nOldDrawMode = ::SetROP2( hDC, R2_XORPEN ); ::MoveToEx( hDC, x1, y1, NULL ); ::LineTo( hDC, x2, y2 ); ::SelectObject( hDC, oldPen ); ::SetROP2( hDC, nOldDrawMode ); } |
namespace MFCGDI { using namespace System; using namespace System::Runtime::InteropServices; typedef void* HDC; [DllImport("mfcgdidll", EntryPoint="DrawGDIXorSolidLine")] extern "C" void DrawGDIXorSolidLine( HDC hdc, unsigned long color, int nWidth, int x1, int y1, int x2, int y2); } |
private: System::Void On_MouseMove(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { …… Graphics *g = this->panel1->CreateGraphics(); // 创建与panel1控件相关联的Graphics IntPtr hdc = g->GetHdc(); MFCGDI::DrawGDIXorSolidLine( (MFCGDI::HDC)hdc, 0xA0A0A0, 1, pt.X, pt.Y, ptPrev.X, ptPrev.Y ); ptPrev = Point( e->X, e->Y ); MFCGDI::DrawGDIXorSolidLine( (MFCGDI::HDC)hdc, 0xA0A0A0, 1, pt.X, pt.Y, ptPrev.X, ptPrev.Y ); g->ReleaseHdc( hdc ); } |