目前共有5篇帖子。 內容轉換:不轉換▼
 
點擊 回復
291 4
【代码】怪物编辑器中,怪物图像动画显示的相关代码
一派護法 十九級
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);
};

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:291 回複數:4
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-1-7 17:19
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。