开发者_如何转开发I need to iterate through all the resources in a project and basically output their names. I have this done in VB. But I can't figure out what the equivalent of My.Resources.ResourceManager is in VC++.
Here's the VB code.
Dim objResourceManager As Resources.ResourceManager = My.Resources.ResourceManager
Dim objResourceSet As Resources.ResourceSet = objResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim iterator As IDictionaryEnumerator = objResourceSet.GetEnumerator()
Private Sub go()
Dim s As String = iterator.Key
Debug.WriteLine(s)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If iterator.MoveNext Then
go()
Else
iterator.Reset()
If iterator.MoveNext Then
go()
Else
Throw New Exception("No elements to display")
End If
End If
End Sub
And this is how far I am in VC++.
private:
Resources::ResourceManager^ rmgnr;
Resources::ResourceSet^ rSet;
public:
Form1(void)
{
rmgnr = gcnew System::Resources::ResourceManager(L"Resources ProjectCPP",Reflection::Assembly::GetExecutingAssembly());
//This is the problem as I can't find the equivalent in c++
rSet = rmgnr->GetResourceSet(CultureInfo::CurrentCulture,true,true);
Please help me figure this out.
I think you just want:
rmgnr = gcnew System::Resources::ResourceManager(GetType());
You can use something like the following for unmanaged C++:
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(resourceId), type);
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
LPVOID memRes = LockResource(hRes);
DWORD sizeRes = SizeofResource(hInstance, hResInfo);
You will need to change the type and resourceId to match your resource. Not sure if its an image or icon or what kind of resource, but you would use something like:
FindResource(hInstance, MAKEINTRESOURCE(bitmapId), _T("PNG"));
For Managed C++, try something like the following:
Bitmap *MyBitmap;
String *Msg;
Reflection::Assembly *MyAssembly;
IO::Stream *ResourceStream;
MyAssembly = System::Reflection::Assembly::GetExecutingAssembly();
ResourceStream = MyAssembly->GetManifestResourceStream(ImageName);
if (ResourceStream != NULL)
{
MyBitmap = new Bitmap(ResourceStream);
Msg = String::Format("GetIcon: {0}, OK", ImageName);
}
else
Msg = String::Format("GetIcon: {0}, Failed", ImageName);
// MyBitmap countains your resource
You will need to replace ImageName with the name of your resource you are trying to grab. Again, I'm assuming its an image resource you are trying to grab.
精彩评论