Xefvan 2016-09-01 13:46 采纳率: 0%
浏览 1133

新手求教,关于C++里Windows api 编程问题

#include <Windows.h>
#include <time.h>
#include <iostream>
using namespace std;

const string APPTITLE = "Game Loop";

HWND window;
HDC device;
bool gameover = false;

void DrawGame(char *filename, int x, int y)
{
    HBITMAP image = (HBITMAP)LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    BITMAP bm;
    GetObject(image, sizeof(BITMAP), &bm);

    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage, image);

    BitBlt(device, x, y, bm.bmWidth, bm.bmHeight, hdcImage, 0, 0, SRCCOPY);

    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);

}

bool Game_Init()
{
    srand(time(NULL));
    return 1;
}

void Game_Run()
{
    if (gameover == true)
        return;

    RECT rect;
    GetClientRect(window, &rect);
    int x = rand() % (rect.right - rect.left);
    int y = rand() % (rect.bottom - rect.top);

    DrawGame("c.bmp", x, y);
}

void Game_End()
{
    ReleaseDC(window, device);
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        gameover = true;
        PostQuitMessage(0);
        break;
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE.c_str();
    wc.hIconSm = NULL;

    return RegisterClassEx(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    window = CreateWindow(
        APPTITLE.c_str(),
        APPTITLE.c_str(),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        640,
        480,
        NULL,
        NULL,
        hInstance,
        NULL);

    if (window == NULL)
        return 0;

    ShowWindow(window, nCmdShow);
    UpdateWindow(window);

    device = GetDC(window);

    return 1;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevhInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;

    MyRegisterClass(hInstance);

    if (!InitInstance(hInstance, nCmdShow))
        return 0;

    if (!Game_Init())
        return 0;

    while (!gameover)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        Game_Run();
    }

    Game_End();
    return 1;
}

代码成功在VS2013运行,代码是绘制图片
请问如何改上述代码,使得它只绘制一个能够在窗口中到处移动的位图,注意图不要飞出窗口边界

  • 写回答

1条回答 默认 最新

  • androiosymbian 2016-09-01 14:33
    关注

    你随机的时候判断边界不久完了。
    还有LoadImage做一次就好了,每次draw的时候传入句柄就好了

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试