如何在c ++中的可执行文件附近的文件夹中打开图像
How to open image in folder near executable in c++
我正在用 c++ Win32 设置一个简单的 window,它将 GIF 动画显示为背景。文件在executable+gifs\test.gif目录下。
以及如何拆分可执行文件名称然后附加其他路径????
我该怎么做?
我试过提供 gif 的完整路径并且它有效,但它在另一台计算机上不起作用。
#define DRAW_ANIM 1
static HWND hWnd;
static HDC hMWDC;
static Graphics* pGphcs = NULL;
static Image* pImg = NULL;
static unsigned int nFrm = 0, nFrmCnt = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
hMWDC = GetDC(hwnd);
pGphcs = new Graphics(hMWDC);
pImg = new Image(L"gifs\test.gif");
if (pImg) {
nFrmCnt = pImg->GetFrameCount(&FrameDimensionTime);
SetTimer(hwnd, DRAW_ANIM, 100, NULL);
}
break;
case WM_TIMER:
if (wParam == DRAW_ANIM)
{
pImg->SelectActiveFrame(&FrameDimensionTime, nFrm);
Rect DRC(0, 0, pImg->GetWidth(), pImg->GetHeight());
pGphcs->Clear(Color(128, 128, 128));
pGphcs->DrawImage(pImg, DRC);
if (nFrm < (nFrmCnt - 1)) nFrm++; else nFrm = 0;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
delete pGphcs;
delete pImg;
ReleaseDC(hwnd, hMWDC);
KillTimer(hwnd, DRAW_ANIM);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
我希望显示 gif,但没有显示。 ;(
您需要让计时器触发 WM_PAINT message (say, by calling InvalidateRect()) 并绘制当前帧以响应该消息。
此外,响应 WM_ERASEBKGND 并且不要删除背景(return 非零)。
不要使用相对路径加载文件。请改用绝对路径。使用 GetModuleFileName()
获取 EXE 的完整路径,然后去掉文件名部分并附加 GIF 的相对路径。
#include <shlwapi.h>
WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
PathRemoveFileSpecW(path);
PathAppendW(path, L"gifs\test.gif");
pImg = new Image(path);
我正在用 c++ Win32 设置一个简单的 window,它将 GIF 动画显示为背景。文件在executable+gifs\test.gif目录下。 以及如何拆分可执行文件名称然后附加其他路径???? 我该怎么做?
我试过提供 gif 的完整路径并且它有效,但它在另一台计算机上不起作用。
#define DRAW_ANIM 1
static HWND hWnd;
static HDC hMWDC;
static Graphics* pGphcs = NULL;
static Image* pImg = NULL;
static unsigned int nFrm = 0, nFrmCnt = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
hMWDC = GetDC(hwnd);
pGphcs = new Graphics(hMWDC);
pImg = new Image(L"gifs\test.gif");
if (pImg) {
nFrmCnt = pImg->GetFrameCount(&FrameDimensionTime);
SetTimer(hwnd, DRAW_ANIM, 100, NULL);
}
break;
case WM_TIMER:
if (wParam == DRAW_ANIM)
{
pImg->SelectActiveFrame(&FrameDimensionTime, nFrm);
Rect DRC(0, 0, pImg->GetWidth(), pImg->GetHeight());
pGphcs->Clear(Color(128, 128, 128));
pGphcs->DrawImage(pImg, DRC);
if (nFrm < (nFrmCnt - 1)) nFrm++; else nFrm = 0;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
delete pGphcs;
delete pImg;
ReleaseDC(hwnd, hMWDC);
KillTimer(hwnd, DRAW_ANIM);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
我希望显示 gif,但没有显示。 ;(
您需要让计时器触发 WM_PAINT message (say, by calling InvalidateRect()) 并绘制当前帧以响应该消息。
此外,响应 WM_ERASEBKGND 并且不要删除背景(return 非零)。
不要使用相对路径加载文件。请改用绝对路径。使用 GetModuleFileName()
获取 EXE 的完整路径,然后去掉文件名部分并附加 GIF 的相对路径。
#include <shlwapi.h>
WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
PathRemoveFileSpecW(path);
PathAppendW(path, L"gifs\test.gif");
pImg = new Image(path);