您好我试图创建一个表单,允许用户单击图片框将图片导入图片框以及其他功能。问题在于导入,我的一个团队成员完成了导入代码,但我们无法使用混合托管和非托管代码。我们都不知道有关托管代码或无人代码进入此项目的任何内容,因此我们不知道如何使以下工作。
下面我们有两种方法,一种是将WChar转换为String,另一种是打开浏览器框来选择图像。后者使用一个图片类,它包含几个变量,如路径和名称。图片类适用于其他功能,无法进行分支。 GetLogo()的类似版本在不在visual studio(unmamanged)时可以正常工作。
#pragma unmanaged
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <iomanip>
using namespace std;
#pragma managed
#include "Picture.h"
#include <list>
#using<system.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
static string importFiles = "Import Pictures";
static wstring importLogo = L"Import Logo";
System::String^ convertWCharToString(wchar_t buffer[])
{
// Convert the wchar_t string to a char* string. Record
//.the length of the original string and add 1 to it to
//.account for the terminating null character.
size_t origsize = wcslen(buffer) + 1;
size_t convertedChars = 0;
// Allocate two bytes in the multibyte output string for every wide
// character in the input string (including a wide character
// null). Because a multibyte character can be one or two bytes,
// you should allot two bytes for each character. Having extra
// space for the new string is not an error, but having
// insufficient space is a potential security problem.
const size_t newsize = origsize*2;
// The new string will contain a converted copy of the original
// string plus the type of string appended to it.
char *nstring = new char[newsize];
// Put a copy of the converted string into nstring
wcstombs_s(&convertedChars, nstring, newsize, buffer, _TRUNCATE);
String^ strBuf = gcnew System::String(nstring) ;
return strBuf;
}
Picture^ GetLogo()
{
const int BUFSIZE = 1024;
wchar_t buffer[BUFSIZE] = {0};
//Define attributes of the OPEN FILE NAME
OPENFILENAME ofns = {0};
ofns.lStructSize = sizeof( ofns );
ofns.lpstrFile = buffer;
ofns.nMaxFile = BUFSIZE;
ofns.lpstrTitle = importLogo.c_str();
ofns.Flags = OFN_EXPLORER |
OFN_ENABLESIZING;
GetOpenFileName( & ofns );
wchar_t szFiles[1024], szFileTok[260];
wchar_t szDir[1024];
Picture^ file;
wchar_t *p;
lstrcpy(szFiles, buffer);
lstrcpy(szDir, buffer);
p = buffer;
p += lstrlen(p) + 1;
convertWCharToString(buffer);
String^ strBuf = gcnew System::String(buffer) ;
String^ strDir = gcnew System::String(szDir) ;
file = gcnew Picture(strBuf, strDir, false);
return file;
}
Picture.h
#pragma once
//This is used to hold the name of the picture, the path to it, and if it is picked in the viewer
#include <string>
ref class Picture
{
public:
Picture(){}; //default constructor
Picture(System::String^ newName, System::String^ newPath, bool newPicked)
{
strName = newName;
strPath = newPath;
boolPicked = newPicked;
}
void setName(System::String^ newName)
{
strName = newName;
}
void setPath(System::String^ newPath)
{
strPath = newPath;
}
void setPicked(bool newPicked)
{
boolPicked = newPicked;
}
System::String^ getName()
{
return strName;
}
System::String^ getPath()
{
return strPath;
}
bool getPicked()
{
return boolPicked;
}
private:
System::String^ strName;
System::String^ strPath;
bool boolPicked;
};
#pragma endregion
以下是调用getLogo
的函数示例private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
Picture^ newLogo = GetLogo();
logoPictureBox->Image = dynamic_cast<Bitmap^> (Image::FromFile(newLogo->getPath(), true));
}
以下是当前错误:
error LNK2028: unresolved token (0A000531) "extern "C" int __stdcall GetOpenFileNameW(struct tagOFNW *)" (?GetOpenFileNameW@@$$J14YGHPAUtagOFNW@@@Z) referenced in function "class Picture ^ __clrcall GetLogo(void)" (?GetLogo@@$$FYMP$AAVPicture@@XZ)
如果您需要更多代码,请告诉我们。 谢谢你的时间。