-1

I'm setting up a simple window in c++ Win32 which will show GIF animation as background. And the file is in directory of executable+gifs\test.gif. And how to split executable name then append other path???? How can I do it?

I've tried with giving full path of gif and it worked, but it won't work on another computer.

#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;
}

I expect a gif showing, but nothing. ;(

TheGamerCoder
  • 91
  • 1
  • 11
  • Possible duplicate of [How to get the application executable name in WindowsC++/CLI?](https://stackoverflow.com/questions/124886/how-to-get-the-application-executable-name-in-windowsc-cli) – zett42 Jul 20 '19 at 19:50
  • No. My question is near to that, but I'm asked it for another result and for getting gif Executable Path.Split(EXECUTABLE NAME).append(other path)). – TheGamerCoder Jul 20 '19 at 20:37
  • Then your question should be "how to split a path", which has already been answered many times on SO. – zett42 Jul 20 '19 at 21:34
  • O yes. I didn't see that. I am not want to make duplicate – TheGamerCoder Jul 21 '19 at 06:31

2 Answers2

1

You need to have the timer cause a WM_PAINT message (say, by calling InvalidateRect()) and draw the current frame in response to that message.

Also, respond to WM_ERASEBKGND and don't erase the background (return nonzero).

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
1

Do not load files using relative paths. Use absolute paths instead. Use GetModuleFileName() to get the full path to your EXE, then strip off the filename portion and append the relative path for your 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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770