将BITMAP C ++启动画面代码转换为PNG图像

时间:2016-12-12 08:30:55

标签: c++ bitmap gdi+ alphablending libpng

这里我的代码我想将PNG图像显示为启动画面,

并且我不知道我将使用libpng或gdi +或任何其他C ++代码,它们通常可以通过C ++代码将PNG图像显示为启动画面

loadersplash.cpp

    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include "resource.h"
    #include "loadersplash.h"

    #include <objidl.h>
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment (lib, "gdiplus.lib")
    /*----------------------------------*/
    // Construction/Destruction
    /*----------------------------------*/

    #ifdef __LOADER_SPLASH
    SPLASH::SPLASH()
    {
        hSplashWnd = hParentWindow = NULL;
        SHOWING = FALSE;
    }

    SPLASH::~SPLASH()
    {
        Hide();
        DestroyWindow(hSplashWnd);
        hSplashWnd = NULL;
    }

    void SPLASH::Init(HWND hWnd, HINSTANCE hInst, int resid)
    {
        hParentWindow = hWnd;
        hSplashWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "STATIC", NULL, WS_POPUP | SS_BITMAP, 0, 0, 400, 300, hWnd, NULL, hInst, NULL);
        if (hSplashWnd){
            SendMessage(hSplashWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadBitmap(hInst, MAKEINTRESOURCE(resid)));
            this->SHOWING = TRUE;
        }
        this->SHOWING = FALSE;
    }

    void SPLASH::Show()
    {
        int x, y;
        int pwidth, pheight;
        int tx, ty;
        HDWP windefer;
        RECT rect, prect;
        GetClientRect(hSplashWnd, &rect);
        x = rect.right; y = rect.bottom;
        GetWindowRect(this->hParentWindow, &prect);
        pwidth = prect.right - prect.left;
        pheight = prect.bottom - prect.top;
        tx = (pwidth / 2) - (x / 2);
        ty = (pheight / 2) - (y / 2);
        tx += prect.left;
        ty += prect.top;

        windefer = BeginDeferWindowPos(1);
        DeferWindowPos(windefer, hSplashWnd, HWND_NOTOPMOST, tx, ty, 50, 50, SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOZORDER);
        EndDeferWindowPos(windefer);

        ShowWindow(hSplashWnd, SW_SHOWNORMAL);
        UpdateWindow(hSplashWnd);
        this->SHOWING = TRUE;
    }

    void SPLASH::Hide()
    {
        ShowWindow(hSplashWnd, SW_HIDE);
        this->SHOWING = FALSE;
    }

    void SPLASH::SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash)
    {
        // get the size of the bitmap
        BITMAP bm;
        GetObject(hbmpSplash, sizeof(bm), &bm);
        SIZE sizeSplash = { bm.bmWidth, bm.bmHeight };

        // get the primary monitor's info
        POINT ptZero = { 0 };
        HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
        MONITORINFO monitorinfo = { 0 };
        monitorinfo.cbSize = sizeof(monitorinfo);
        GetMonitorInfo(hmonPrimary, &monitorinfo);

        // center the splash screen in the middle of the primary work area
        const RECT & rcWork = monitorinfo.rcWork;
        POINT ptOrigin;
        ptOrigin.x = 0;
        ptOrigin.y = rcWork.top + (rcWork.bottom - rcWork.top - sizeSplash.cy) / 2;

        // create a memory DC holding the splash bitmap
        HDC hdcScreen = GetDC(NULL);
        HDC hdcMem = CreateCompatibleDC(hdcScreen);
        HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpSplash);

        // use the source image's alpha channel for blending
        BLENDFUNCTION blend = { 0 };
        blend.BlendOp = AC_SRC_OVER;
        blend.SourceConstantAlpha = 255;
        blend.AlphaFormat = AC_SRC_ALPHA;

        // paint the window (in the right location) with the alpha-blended bitmap
        UpdateLayeredWindow(hwndSplash, hdcScreen, &ptOrigin, &sizeSplash,
            hdcMem, &ptZero, RGB(0, 0, 0), &blend, ULW_ALPHA);

        // delete temporary objects
        SelectObject(hdcMem, hbmpOld);
        DeleteDC(hdcMem);
        ReleaseDC(NULL, hdcScreen);
    }
    #endif //__LOADER_SPLASH

loadersplash.h

// SPLASH.h: interface for the SPLASH class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SPLASH_H__41182F11_BB6F_11D6_B0F5_00B0D01AD687__INCLUDED_)
#define AFX_SPLASH_H__41182F11_BB6F_11D6_B0F5_00B0D01AD687__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef __LOADER_SPLASH

class SPLASH
{
public:
    void Hide();
    void Show();
    void Init(HWND hWnd, HINSTANCE hInst, int resid);
    BOOL SHOWING;
    void SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash);

    SPLASH();
    virtual ~SPLASH();

private:
    UINT TimerID;
    HWND hParentWindow;
    HWND hSplashWnd;

};

//class CPNGTestDlg : public CDialog
//{
//  BOOL            LoadPNG();
//};

#endif //__LOADER_SPLASH

#endif // !defined(AFX_SPLASH_H__41182F11_BB6F_11D6_B0F5_00B0D01AD687__INCLUDED_)

0 个答案:

没有答案