【发布时间】:2012-12-14 00:38:30
【问题描述】:
我需要转换:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646839(v=vs.85).aspx
lpstrFileTitle
Type: LPTSTR
The file name and extension (without path information) of the selected file. This member can be NULL.
尽管在 MSVC++ 2012 Express 中它说的是 LPSTR。
到
http://msdn.microsoft.com/en-us/library/windows/desktop/bb172802(v=vs.85).aspx
pSrcFile [in]
Type: LPCTSTR
Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
我将非常感激。 :)
char szFileName[MAX_PATH] = {0};
char szFileTitleName[MAX_PATH] = {0};
HRESULT hr = S_OK;
RtlZeroMemory(&gc.ofn, sizeof(gc.ofn));
gc.ofn.lStructSize = sizeof(gc.ofn);
gc.ofn.hwndOwner = hWnd;
gc.ofn.lpstrFilter = "All Image Files\0" "*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico\0"
"Windows Bitmap\0" "*.bmp;*.dib\0"
"High Definition Photo\0" "*.wdp;*.mdp;*.hdp\0"
"Graphics Interchange Format\0" "*.gif\0"
"Portable Network Graphics\0" "*.png\0"
"JPEG File Interchange Format\0" "*.jpg;*.jpeg\0"
"Tiff File\0" "*.tif\0"
"Icon\0" "*.ico\0"
"All Files\0" "*.*\0"
"\0";
gc.ofn.nMaxFileTitle = MAX_PATH;
gc.ofn.lpstrFileTitle = gc.szFileTitleName; // its a char
gc.ofn.lpstrFile = szFileName;
gc.ofn.nMaxFile = MAX_PATH;
gc.ofn.lpstrTitle = "Open Image";
gc.ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
if(GetOpenFileName(&gc.ofn)) {
gc.render_on = true;
}
.
D3DXCreateTextureFromFileEx (d3dDevice, gc.szFileTitleName , D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
colorkey, &init_map->image_info, NULL, &init_map->texture_buffer)
这将有一个空白图像。我在 GetOpenFile 之后用一个消息框对此进行了测试,它返回很好。
MessageBoxA ( NULL , gc.ofn.lpstrFileTitle , "" , 0 );
但在 D3DXCreateTextureFromFileEx 之前,它搞砸了。
只需将字符存储到 gc.szFileTitleName 中。不知道为什么其他方式超出范围......
【问题讨论】:
-
删除所有
(LPTSTR)演员表,它们不应该被需要并且可能隐藏错误... -
你为什么要明确地调用
MessageBoxA?如果你打电话给MessageBox,会发生什么? -
@K-ballo 仍然没有工作。似乎,如果我在 GetOpenFileName 之后立即执行消息框,它将显示正确的文件名。但是如果我在 D3DXCreateTextureFromFileEx 之前再做一次,那一切都是胡言乱语。也许我的其他代码有问题。
-
@K-ballo 第二次还是胡言乱语。
-
哦,好吧,是的,肯定有问题。听起来好像
szFileTitleName正在被删除(超出范围)。当它不再存在时,您会尝试访问它。