c++ - 在windows程序上绘图

标签 c++ visual-c++

好的,下面是: 我正在为学校制作一个基本的乒乓球游戏,我可以设置任何东西。但是在程序初始化(启动)之后,我似乎无法更新窗口。我试过先绘制一个后备缓冲区,但它什么也没做。代码在下面所以请看一下,提前谢谢你!!

//=============================================================================
//Seth Dark @2011
//Pong v1.0
//=============================================================================

//=============================================================================
//to do list
// - get the ball moving
// - figure out the AI
// - figure out the ball vs paddle
// Version 2
// - count time or points
// - hiscore bord
//=============================================================================

// Pong.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include "Resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                     // current instance
TCHAR szTitle[MAX_LOADSTRING];       // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
BackBuffer* gBackBuffer = 0;         // The backbuffer we will render

HDC    hdc;                          // ------------------------------ gamble

const int gClientWidth = 800;
const int gClientHeight = 600;

Vec2 playerPos(20.0f, 250.0f);
Vec2 aiPos(780.0f,250.0f);
Vec2 ballPos(400.0f, 250.0f);

// Forward declarations of functions included in this code module:
ATOM             MyRegisterClass(HINSTANCE hInstance);
BOOL             InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

//---------------------------------------------
//INT RUN
//---------------------------------------------
int APIENTRY _tWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR    lpCmdLine,
    int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    HDC bbDC = gBackBuffer->getDC();
    //Clear backbuffer
    HBRUSH oldBrush = (HBRUSH)SelectObject(bbDC, GetStockObject(BLACK_BRUSH));
    Rectangle(bbDC,0 ,0 ,800, 600);

    //Draw player
    SelectObject(hdc,GetStockObject(WHITE_BRUSH)); // drawing the thing
    Rectangle(bbDC,
        (int)playerPos.x -10,
        (int)playerPos.y - 50,
        (int)playerPos.x + 10,
        (int)playerPos.y + 50); 

    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_PONG, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PONG));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//      Creation of the window upon wich we will draw
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style         = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc   = WndProc;
    wcex.cbClsExtra    = 0;
    wcex.cbWndExtra    = 20;
    wcex.hInstance     = hInstance;
    wcex.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PONG));
    wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wcex.lpszMenuName  = MAKEINTRESOURCE(IDC_PONG);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm       = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    wcex.hbrBackground = (HBRUSH)GetStockObject (BLACK_BRUSH);

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    hInst = hInstance; // Store instance handle in our global variable

    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, 819, 600, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;

    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...

        //Draw player rectangle get class look page 237 - 
        SelectObject(hdc,GetStockObject(WHITE_BRUSH));
        Rectangle(hdc,
            (int)playerPos.x -10,
            (int)playerPos.y - 50,
            (int)playerPos.x + 10,
            (int)playerPos.y + 50); 

        //Draw AI rectangle get class look page 237 - AI
        Rectangle(hdc,
            (int)aiPos.x -10,
            (int)aiPos.y - 50,
            (int)aiPos.x + 10,
            (int)aiPos.y + 50);  

        //Draw the ball 
        Ellipse(hdc,
            (int)ballPos.x -20,
            (int)ballPos.y - 20,
            (int)ballPos.x + 20,
            (int)ballPos.y + 20);

        EndPaint(hWnd, &ps);
        break;

    case WM_KEYDOWN:
    {
        switch(wParam)
        {
        case 'Z':
            playerPos.y -= 5;
            break;
        case 'S':
            playerPos.y += 5;
            break;
        }

        // up, down, enter to play or restart

        break;
        return 0;
    }

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

我希望这是足够的信息!从星期天开始,这件事就让我发疯。任何我不能画画的帮助都会被应用,我已经尝试将它放在运行和绘图以及其他一些东西中

最佳答案

WM_PAINT 会在需要更新区域时发送到窗口。
当您按下一个键时,您只会更新对象状态。

您可以将 WM_PAINT 代码复制到一个单独的函数中,并在按下某个键时调用它,或者
调用RedrawWindow ,这将发送绘画消息。

关于c++ - 在windows程序上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5251095/

相关文章:

c++ - 关于不可变[string]对象的问题和验证C++

c++ - Intellisense 与编译器 : "A namespace with this name does not exist" vs "not a class or struct name" 不一致

c - 如何将第三方的外部库 (.lib) 链接到 UEFI 应用程序中? (由 EDK2 提供)

c++ - 不良顶点数据的常见陷阱/原因?

c++ - 拓扑排序的 C++ 实现

c++ - ODR 使用的空类的优化

c++ - VC++ 中的动态数组大小和动态数组分配器

c++ - 什么是 IStreamPtr?

visual-c++ - MFC中如何将图片添加到CListCtrl

c++ - 数组实例化方法的区别