1. Introduction
In earlier Windows programs, you can use GDI (Graphics Device Interface, Graphics device interface) to draw graphics, text, and images in a form, but it has limited functionality, especially in image processing. GDI + is a new version of GDI, it not only adds a lot of new features to GDI, but also optimizes the existing GDI functions, and constructs a series of related classes for two-dimensional vector graphics, text, image processing, region, path and graph data matrix provided by the developers. The Graphics class graphics is a core class in the GDI + interface, and many drawing operations can be done with it.
Compared to GDI, GDI + adds new features such as gradient brushes, spline curves, persistent path objects, matrix and matrix transformations, alpha blending, color correction, anti-aliasing, and metadata. However, GDI + does not support and (with), or (or), and XOR (ISO or) raster operations (ROP) and hardware acceleration in GDI. Among them, XOR raster operation is the most important method to realize the dynamic positioning of the rubber strip technology, second, the image processing speed in GDI + is not more advantageous than GDI. For this purpose, this paper discusses the methods and techniques of mixed programming of GDI + and GDI in managed environment through several instances of managed C + +.
2. Managed C + + and GDI
In Visual C + +. NET 2003, programmers can use MFC and managed C + + (Managed Extensions for c++,c++ Managed Extensions) to develop graphics and image programs programmatically. MFC is a set of Microsoft base class libraries that encapsulate Windows APIs using object-oriented technology. Therefore, the development of graphics and image programs in MFC can be programmed either by using the MFC class CDC or directly using functions and structures in the GDI API.
Managed C + + is built on C + + to develop for Visual C + + programmers. NET Framework applications. In addition to maintaining the full functionality of standard C + +, it is also available through the. NET Framework (. NET Framework) to create objects that automate memory management as well as with other. NET language interoperability. Because the managed environment differs from the unmanaged environment, the GDI API cannot be invoked directly in managed C + + as MFC does. However, the graphics class [4] in GDI + provides some methods for interacting with GDI, such as GETHDC and RELEASEHDC, to get or release the device environment handle associated with the graphics object, respectively.
Because the GDI API does not use managed code, it uses a data type different from the data type used in Managed C + +, and it is not a COM object, so using GDI in managed C + + is done through platform invoke (PInvoke).
3. platform invoke and data marshaling
platform invoke [5] is a service that enables managed code to invoke unmanaged functions implemented in a DLL and to specify the Runtime::interopservices namespace when used.
3.1 General methods of invoking GDI API functions
Invokes the GDI API (GDI32) in managed C + +. DLL) is typically three parts by identifying functions in the DLL, creating prototypes and function calls in managed code. Where the function call is the same as in general managed C + +, this is not discussed here.
In managed C + +, the identity of a DLL function is manipulated by the DllImport property, which includes fields such as commonly used entrypoint, CharSet, ExactSpelling, and CallingConvention. The EntryPoint field is used to specify the name of the DLL entry point to invoke. The CharSet field is used to specify how to control the name corruption and marshal string parameters. The ExactSpelling field is used to specify whether to search the unmanaged DLL for the function or method name specified by the entry point. The CallingConvention field is used to specify the calling convention for the entry point, which defaults to WINAPI.
It is necessary to note that the above fields are not necessarily specified in the identity of the DLL function, and you can change the default behavior of the DllImport property by setting one or more fields. For example:
using namespace System::Runtime::InteropServices;
typedef void* HDC;
[DllImport("gdi32", EntryPoint="LineTo")]
extern "C" bool LineTo(HDC hDC, int nXEnd, int nYEnd);