作者共发了5篇帖子。 内容转换:不转换▼
 
点击 回复
439 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)
 

本帖信息

点击数:439 回复数:4
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-1-7 17:19
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。