目前共有5篇帖子。
【代碼】怪物編輯器中,怪物圖像動畫顯示的相關代碼
1樓 巨大八爪鱼 2016-1-7 17:15
【運行效果】


【相關代碼】
void Character::DisplayInPictureCtrl(HWND hPictureCtrl, UINT flags)
{
    RECT rect;
    GetClientRect(hPictureCtrl, &rect);
    HDC hdc = GetDC(hPictureCtrl);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmp = (HBITMAP)SendMessage(hPictureCtrl, STM_GETIMAGE, IMAGE_BITMAP, NULL);
    if (hbmp == NULL)
    {
        // When no bitmap is associated with the picture control, create one, according to the DC from the picture control
        hbmp = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);
        SendMessage(hPictureCtrl, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
    }
    ReleaseDC(hPictureCtrl, hdc);
    DeleteDC(hdc);

    SelectObject(hdcMem, hbmp);
    Graphics graphics(hdcMem);
    DrawIn(graphics, rect, flags);
    DeleteDC(hdcMem);
    InvalidateRect(hPictureCtrl, &rect, false);
}
2樓 巨大八爪鱼 2016-1-7 17:16
void Character::DrawIn(Graphics &graphics, RECT rect, UINT flags)
{
    UINT rectWidth = rect.right - rect.left;
    UINT rectHeight = rect.bottom - rect.top;

    if (flags & CHD_FILLBACKGROUND)
    {
        Gdiplus::SolidBrush brush(backgroundColor);
        graphics.FillRectangle(&brush, rect.left, rect.top, rectWidth, rectHeight);
    }
    else if (flags & CHD_USEFLOORPATTERN)
    {
        Bitmap bmpFloorPattern(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
        TextureBrush floorPatternBrush(&bmpFloorPattern);
        graphics.FillRectangle(&floorPatternBrush, rect.left, rect.top, rectWidth, rectHeight);
    }

    if (rectWidth >= width && rectHeight >= height)
    {
        /* When the image is smaller than the picture frame */
        int horizontalPadding = (rectWidth - width) / 2;
        int verticalPadding = (rectHeight - height) / 2;
        Draw(graphics, rect.left + horizontalPadding, rect.top + verticalPadding);
    }
    else
    {
        /* When the image is bigger than the picture frame */
        Rect availableRegion, destRect;
        availableRegion.X = rect.left + 1;
        availableRegion.Y = rect.top + 1;
        availableRegion.Width = rectWidth - 2;
        availableRegion.Height = rectHeight - 2;

        double containerRatio = (double)availableRegion.Width / (double)availableRegion.Height;
        double pictureRatio = (double)width / (double)height;
        if (pictureRatio == containerRatio)
            destRect = availableRegion;
        else if (pictureRatio > containerRatio)
        {
            destRect.Width = availableRegion.Width;
            destRect.Height = (INT)(availableRegion.Width / pictureRatio);
            destRect.X = availableRegion.X;
            destRect.Y = availableRegion.Y + availableRegion.Height / 2 - destRect.Height / 2;
        }
        else if (pictureRatio < containerRatio)
        {
            destRect.Width = (INT)(availableRegion.Height * pictureRatio);
            destRect.Height = availableRegion.Height;
            destRect.X = availableRegion.X + availableRegion.Width / 2 - destRect.Width / 2;
            destRect.Y = availableRegion.Y;
        }
        
        graphics.DrawImage(image, destRect, column * width, row * height, width, height, UnitPixel);
    }
}
3樓 巨大八爪鱼 2016-1-7 17:17
/* Draw the current image at a certain position */
void Character::Draw(Graphics &graphics, int x, int y)
{
    graphics.DrawImage(image, x, y, column * width, row * height, width, height, UnitPixel);
}
4樓 巨大八爪鱼 2016-1-7 17:18
【補充】PNG素材加載部分的代碼
bool Character::Open(wstring filename)
{
    if (_waccess(filename.c_str(), 0) == -1)
        return false;
    fileName = filename;
    image = Image::FromFile(filename.c_str());
    fileWidth = image->GetWidth();
    fileHeight = image->GetHeight();
    width = fileWidth / IMG_COUNT_H;
    height = fileHeight / IMG_COUNT_V;
    return true;
}
5樓 巨大八爪鱼 2016-1-7 17:19
Character類的結構:
#pragma once

using namespace Gdiplus;
using namespace std;

#define CHD_FILLBACKGROUND _BV(0)
#define CHD_USEFLOORPATTERN _BV(1)

class Character
{
protected:
    UINT fileWidth;
    UINT fileHeight;
    UINT height;
    UINT width;
public:
    Character(void);
    ~Character(void);
    Color backgroundColor;
    int column;
    wstring fileName;
    Image *image;
    int row;
   
    void AnimationIncrement(void);
    void Close(void);
    void DisplayInPictureCtrl(HWND hPictureCtrl, UINT flags);
    void Draw(Graphics &graphics, int x = 0, int y = 0);
    void Draw(Graphics &graphics, int x, int y, int horizontalPadding, int verticalPadding);
    void DrawIn(Graphics &graphics, int x, int y, int width, int height, UINT flags);
    void DrawIn(Graphics &graphics, RECT rect, UINT flags);
    UINT GetHeight(void);
    UINT GetWidth(void);
    bool Open(LPTSTR filename);
    bool Open(wstring filename);
    void SetBackgroundColor(COLORREF color);
    void SetBackgroundColor(int sysColor);
    void SetImage(int row, int column = 0);
};

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
 
 
©2010-2024 Arslanbar [手機版] [桌面版]
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。