c ++ WxWidgets的示例代码将图像粘贴到画布上

问题描述 投票:0回答:1

我正在尝试使用wxWidgets和wxFormBuilber制作基本的GUI,以显示Sprite表中的国际象棋游戏板和棋子。我真的很感谢一些示例代码:

  1. 从文件上传图像
  2. 在特定位置上画图
c++ image wxwidgets
1个回答
2
投票

这是我能想到的最简单的例子。它使用来自维基百科的set of pieces(在Creative Commons license下获得许可)。此示例仅绘制黑色块,但是您应该能够通过一些简单的修改就能弄清楚如何绘制白色块。

enter image description here

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

class MyFrame: public wxFrame
{
    public:
        MyFrame();

    private:
        void OnPaint(wxPaintEvent& event);

        wxPanel* m_board;
        wxBitmap m_blackRook;
        wxBitmap m_blackKnight;
        wxBitmap m_blackBishop;
        wxBitmap m_blackQueen;
        wxBitmap m_blackKing;
        wxBitmap m_blackPawn;
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "Chess", wxDefaultPosition, wxSize(1000, 1000))
{
    m_board = new wxPanel(this, wxID_ANY);
    m_board->Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);

    // Load the image.
    wxImage im("c:\\640px-Chess_Pieces_Sprite.svg.png", wxBITMAP_TYPE_PNG);

    // Extract the images of the pieces from the larger image.
    wxBitmap b(im);
    m_blackKing = b.GetSubBitmap(wxRect(5,113,100,100));
    m_blackQueen = b.GetSubBitmap(wxRect(110,113,100,100));
    m_blackBishop = b.GetSubBitmap(wxRect(215,113,100,100));
    m_blackKnight = b.GetSubBitmap(wxRect(323,113,100,100));
    m_blackRook = b.GetSubBitmap(wxRect(433,113,100,100));
    m_blackPawn = b.GetSubBitmap(wxRect(535,113,100,100));
}

void MyFrame::OnPaint(wxPaintEvent&)
{
    wxPaintDC dc(m_board);
    dc.Clear();

    // Draw thie light squares
    dc.SetPen(wxColour(209,139,71));
    dc.SetBrush(wxColour(209,139,71));
    dc.DrawRectangle(0,0,800,800);

    // Draw thie dark squares
    dc.SetPen(wxColour(255,206,158));
    dc.SetBrush(wxColour(255,206,158));
    for ( int i = 0 ; i< 8 ; ++i )
    {
        for ( int j = i%2 ; j< 8 ; j+=2 )
        {
            dc.DrawRectangle(i*100,j*100,100,100);
        }
    }

    // Draw thie black pieces
    dc.DrawBitmap(m_blackRook, 0, 0, true);
    dc.DrawBitmap(m_blackKnight, 100, 0, true);
    dc.DrawBitmap(m_blackBishop, 200, 0, true);
    dc.DrawBitmap(m_blackQueen, 300, 0, true);
    dc.DrawBitmap(m_blackKing, 400, 0, true);
    dc.DrawBitmap(m_blackBishop, 500, 0, true);
    dc.DrawBitmap(m_blackKnight, 600, 0, true);
    dc.DrawBitmap(m_blackRook, 700, 0, true);

    for ( int i = 0 ; i < 8 ; ++i )
    {
        dc.DrawBitmap(m_blackPawn, 100*i, 100, true);
    }
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

在Windows上,应用程序将如下所示:

enter image description here

这里展示的重要项目是

  1. 加载图像。
  2. 为各个片段提取子位图。
  3. 在板的油漆处理机中绘制零件。

显然,您应该在第38行更改png文件的位置。如果要获取few extra steps,则可以将png文件嵌入到您的应用程序中。

我试图使示例尽可能简单,因此有许多地方可以改进。例如。我为每种类型的片段使用了单独的位图。如果需要,可以改用wxImageList 。此外,最好在绘画处理程序中使用wxAutoBufferedPaintDC

© www.soinside.com 2019 - 2024. All rights reserved.