⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddex4.cpp

📁 Windows CE .Net 下面 Direct DRAW编程的经典实例。对于初学Windows 平台下Direct DRAW技术的程序员颇具借鉴意义!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }
}




//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The Main Window Procedure
//-----------------------------------------------------------------------------
long FAR PASCAL
WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
#ifdef UNDER_CE
        case WM_ACTIVATE:
#else
        case WM_ACTIVATEAPP:
#endif
            // Pause if minimized or not the top window
            g_bActive = (wParam == WA_ACTIVE) || (wParam == WA_CLICKACTIVE);
            return 0L;

        case WM_DESTROY:
            // Clean up and close the app
            ReleaseAllObjects();
            PostQuitMessage(0);
            return 0L;

        case WM_KEYDOWN:
            // Handle any non-accelerated key commands
            switch (wParam)
            {
                case VK_ESCAPE:
                case VK_F12:
                    PostMessage(hWnd, WM_CLOSE, 0, 0);
                    return 0L;
            }
            break;

        case WM_SETCURSOR:
            // Turn off the cursor since this is a full-screen app
            SetCursor(NULL);
            return TRUE;

        case WM_TIMER:
	        // Shoot another frame.
	        UpdateFrame();
	        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}




//-----------------------------------------------------------------------------
// Name: InitApp()
// Desc: Do work required for every instance of the application:
//          Create the window, initialize data
//-----------------------------------------------------------------------------
static HRESULT
InitApp(int nCmdShow)
{
    HWND                        hWnd;
    WNDCLASS                    wc;
    DDSURFACEDESC2              ddsd;
    DDSCAPS2                    ddscaps;
    HRESULT                     hRet;
    LPDIRECTDRAW                pDD;

    // Set up and register window class
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = NAME;
    RegisterClass(&wc);

    // Create a window
    hWnd = CreateWindowEx(WS_EX_TOPMOST,
                          NAME,
                          TITLE,
                          WS_POPUP,
                          0,
                          0,
                          GetSystemMetrics(SM_CXSCREEN),
                          GetSystemMetrics(SM_CYSCREEN),
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
    if (!hWnd)
        return FALSE;
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    SetFocus(hWnd);

    ///////////////////////////////////////////////////////////////////////////
    // Create the main DirectDraw object
    ///////////////////////////////////////////////////////////////////////////
    if (FAILED(hRet = DirectDrawCreate(NULL, &pDD, NULL)))
        return InitFail(hWnd, hRet, TEXT("DirectDrawCreate FAILED"));

    // Fetch DirectDraw4 interface and release IDirectDraw.
    if (FAILED(hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *) & g_pDD)))
        return InitFail(hWnd, hRet, TEXT("QueryInterface FAILED"));
    pDD->Release();
    pDD = NULL;

    // Get exclusive mode
    if (FAILED(hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
        return InitFail(hWnd, hRet, TEXT("SetCooperativeLevel FAILED"));

    // Set the video mode to 640x480x8

#ifndef UNDER_CE
    if (FAILED(hRet = g_pDD->SetDisplayMode(640, 480, 8, 0, 0)))
        return InitFail(hWnd, hRet, TEXT("SetDisplayMode FAILED"));
#endif

    // Create the primary surface with 1 back buffer
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
                          DDSCAPS_FLIP |
                          DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 1;
    if (FAILED(hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL)))
    {
        if (hRet == DDERR_NOFLIPHW)
            return InitFail(hWnd, hRet, TEXT("******** Display driver doesn't support flipping surfaces. ********"));
  
        return InitFail(hWnd, hRet, TEXT("CreateSurface FAILED"));
    }

    // Get a pointer to the back buffer
    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
    if (FAILED(hRet = g_pDDSPrimary->GetAttachedSurface(&ddscaps, &g_pDDSBack)))
        return InitFail(hWnd, hRet, TEXT("GetAttachedSurface FAILED"));

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
    if (FAILED(hRet = g_pDDSBack->GetSurfaceDesc(&ddsd)))
    {
        return InitFail(hWnd, hRet, TEXT("GetSurfaceDesc FAILED"));
    }
    g_dwWidth = ddsd.dwWidth;
    g_dwHeight = ddsd.dwHeight;

    // Load the background bitmap, scaling to fit dimensions of back buffer
    g_pDDSBackground = DDLoadBitmapScaled(hInstance, g_pDD, szBackground, g_dwWidth, g_dwHeight);
    if (g_pDDSBackground == NULL)
        return InitFail(hWnd, hRet, TEXT("DDLoadBitmap FAILED"));

    // Load the sprite (donut) bitmap
    g_pDDSSprite = DDLoadBitmap(hInstance, g_pDD, szSprite);
    if (g_pDDSSprite == NULL)
        return InitFail(hWnd, hRet, TEXT("DDLoadBitmap FAILED"));

    // Set the color key for this bitmap (black)
    DDSetColorKey(g_pDDSSprite, RGB(0, 0, 0));

    // Create a timer to flip the pages
    if (TIMER_ID != SetTimer(hWnd, TIMER_ID, TIMER_RATE, NULL))
        return InitFail(hWnd, hRet, TEXT("SetTimer FAILED"));


    return DD_OK;
}

//-----------------------------------------------------------------------------
// Name: DDLoadBitmapScaled()
// Desc: Create a DirectDrawSurface from a bitmap resource, scaling the bitmap
//       to the specified width and height.
//-----------------------------------------------------------------------------
extern "C" IDirectDrawSurface4 *
DDLoadBitmapScaled(HINSTANCE hInstance, IDirectDraw4 * pdd, LPCTSTR szBitmap, 
    DWORD dwWidth, DWORD dwHeight)
{
    HRESULT hr;
    HBITMAP hbm;
    DDSURFACEDESC2 ddsd;
    IDirectDrawSurface4 * pdds;

    //
    // Get a handle to the bitmap.
    //
    hbm = DDGetBitmapHandle(hInstance,szBitmap);
    if (hbm == NULL) {
      OutputDebugString(TEXT("DDUTIL: Unable to obtain handle to bitmap.\n"));
      return NULL;
    }

    //
    // Create a DirectDrawSurface for this bitmap
    //
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    ddsd.dwWidth = dwWidth;
    ddsd.dwHeight = dwHeight;
    if (FAILED(pdd->CreateSurface(&ddsd, &pdds, NULL)))
        return NULL;

    hr = DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
    if (FAILED(hr)) {
      OutputDebugString(TEXT("DDUTIL: Unable to copy bitmap bits.\n"));
      pdds->Release();
      pdds = NULL;
    }

    DeleteObject(hbm);
    return pdds;
}

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Initialization, message loop
//-----------------------------------------------------------------------------
int PASCAL
WinMain(HINSTANCE hCurInstance,
        HINSTANCE hPrevInstance,
#ifdef UNDER_CE
        LPWSTR lpCmdLine,
#else
        LPSTR lpCmdLine,
#endif
        int nCmdShow)
{
    MSG                         msg;

    hInstance = hCurInstance;

    if (FAILED(InitApp(nCmdShow)))
        return FALSE;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -