知道了。您不能直接创建对象,但d2d1.dll
包含一个调用的导出,该导出D2D1CreateFactory
返回一个ID2DFactory
接口。使用它,您可以在没有托管 C++ 的情况下实现 Direct2D 包装器。
public static class Direct2D1Util {
public const string IID_ID2D1Factory =
"06152247-6f50-465a-9245-118bfd3b6007";
[DllImport("d2d1.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
private static extern object D2D1CreateFactory(D2D1_FACTORY_TYPE factoryType,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
D2D1_FACTORY_OPTIONS pFactoryOptions);
public static ID2D1Factory CreateFactory() {
D2D1_FACTORY_OPTIONS opts = new D2D1_FACTORY_OPTIONS();
object factory = D2D1CreateFactory(
D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED,
new Guid(IID_ID2D1Factory), opts);
return (ID2D1Factory)factory;
}
}
我不会包括所有类型,因为它们都定义在 中d2d1.h
,但这里是如何使用代码来获取工厂:
ID2DFactory factory = Direct2D1Util.CreateFactory();
确保您的ID2DFactory
界面具有适当的Guid
和InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
属性。