Dan*_*ker 22
如果您有一个现有的本机C++应用程序并且想要避免使用过多的CLR来"污染"它,那么您可以/clr
仅为一个特定文件打开标志并使用标准C++标头为其提供接口.我用一些旧的代码完成了这个.在标题中我有:
void SaveIconAsPng(void *hIcon, const wchar_t *pstrFileName);
Run Code Online (Sandbox Code Playgroud)
因此,程序的其余部分有一个简单的API,它可以传递HICON和目标文件路径.
然后我有一个单独的源文件,这是唯一一个已/clr
打开的文件:
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
using namespace System::Drawing::Drawing2D;
#include <vcclr.h>
#include <wchar.h>
void SaveIconAsPng(void *hIcon, const wchar_t *pstrFileName)
{
try
{
Bitmap bitmap(16, 16, PixelFormat::Format32bppArgb);
Graphics ^graphics = Graphics::FromImage(%bitmap);
graphics->SmoothingMode = SmoothingMode::None;
Icon ^icon = Icon::FromHandle(IntPtr(hIcon));
graphics->DrawIcon(icon, Rectangle(0, 0, 15, 15));
graphics->Flush();
bitmap.Save(gcnew String(pstrFileName), ImageFormat::Png);
}
catch (Exception ^x)
{
pin_ptr<const wchar_t> unmngStr = PtrToStringChars(x->Message);
throw widestring_error(unmngStr); // custom exception type based on std::exception
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以从我毛茸茸的旧C++程序中将HICON转换为PNG文件,但是我已经从其余的代码中分离出了.NET框架的使用 - 所以如果我以后需要移植,我可以轻松地交换一个不同的实施.
您可以进一步将这一点放在一个单独的DLL中,并将CLR相关的代码放在一个单独的DLL中,尽管除非您希望能够单独修补它,否则几乎没有附加价值.
归档时间: |
|
查看次数: |
7686 次 |
最近记录: |