-1

我正在尝试编写一个使用图像来显示旋转结果的老虎机 Win32 应用程序。我知道如何在正常的 LRESULT CALLBACK 框架上显示图像,但是在对话框上显示图像时我迷失了。谁能帮助我解释(深入)我将如何显示图像?对此,我真的非常感激。

我当前的对话框回调:

BOOL CALLBACK DlgProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_INITDIALOG:         //dialog created
            g_hbmObject = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_OBJECT));
                                    //initialize slotmachine class
            Machine.Initialize(time(0));
            if(g_hbmObject == NULL) //test if object is loaded correctly
                std::cerr << "Could not load ball..";
        break;
        case WM_COMMAND:            //switch command
            switch(LOWORD(wParam))
            {
                case IDC_SPIN:      //slot machine spins
                                    //put spin function, decide what to display
                                    //do i put the display image command here? or where?
                break;
                case IDC_EXIT:      //exit button
                    DeleteObject(g_hbmObject);
                    EndDialog(hwnd, 0);
                break;
            }
        break;
        case WM_CLOSE:
        case WM_DESTROY:            //case program is exited
            DeleteObject(g_hbmObject);
            PostQuitMessage(0);
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
4

1 回答 1

1

下面的代码注册了一个“REEL”窗口类:一个显示老虎机旋转卷轴的控件。您可以使用以下资源语句在对话框上创建它的多个实例:

CONTROL         "",IDC_REEL1,"REEL",0,14,50,40,40
CONTROL         "",IDC_REEL2,"REEL",0,70,50,40,40

卷轴无休止地旋转,但您可以轻松添加私人消息来启动和停止它。

正如评论中所解释的,它需要一个位图来表示具有资源 ID 的卷轴IDB_REEL

HBITMAP g_hbmpReel;

LRESULT CALLBACK ReelWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    const int ReelTimerId = 5;

    switch (message)
    {
    case WM_CREATE:
        SetTimer(hWnd, ReelTimerId, 10, 0);
        break;
    case WM_DESTROY:
        KillTimer(hWnd, ReelTimerId);
        break;
    case WM_SIZE:
        SetWindowPos(hWnd, 0, 0, 0, 40, 40, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
        break;
    case WM_TIMER:
        if (wParam == ReelTimerId)
        {
            int offset = GetWindowLong(hWnd, 0);
            offset = (offset + 5) % 120;
            SetWindowLong(hWnd, 0, offset);
            InvalidateRect(hWnd, 0, FALSE);
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            HDC hdcReel = CreateCompatibleDC(hdc);
            HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcReel, g_hbmpReel);
            int offset = GetWindowLong(hWnd, 0);
            BitBlt(hdc, 0, 0, 40, 40, hdcReel, 0, offset, SRCCOPY);
            SelectObject(hdcReel, hbmpOld);
            DeleteDC(hdcReel);
            EndPaint(hWnd, &ps);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

ATOM RegisterReel(HINSTANCE hInstance)
{
    WNDCLASSEX wcex = {0};

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.lpfnWndProc    = ReelWndProc;
    // Window data used to hold the position of the reel
    wcex.cbWndExtra     = sizeof(int);
    wcex.hInstance      = hInstance;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszClassName  = L"REEL";

    // IDB_REEL is a 40x160 bitmap representing a reel with THREE 40x40 symbols.
    // The bottom 40x40 square should be the same as the topmost.
    g_hbmpReel = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_REEL));

    return RegisterClassEx(&wcex);
}
于 2012-10-20T19:39:08.860 回答