在WXWIDGETS中添加图像

Add Image in wxWidgets

本文关键字:图像 添加 WXWIDGETS      更新时间:2023-10-16

我刚刚开始学习wxwidgets,我正在尝试创建某种图像开启器。但是我不明白如何添加图像...我看到了一些有关wximage,wxbitmap,wxstaticbitmap的东西,但我不明白它们中的任何一个或它们之间的区别。

#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/wfstream.h>
class MyApp : public wxApp
{
public:
   virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
   MyFrame (const wxString& event);
   void OnOpen(wxCommandEvent& event);
   void OnQuit(wxCommandEvent& event);
private:
   DECLARE_EVENT_TABLE();
};
DECLARE_APP(MyApp);
IMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
   MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
   frame->Show(true);
   return true;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
   EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
   EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
void MyFrame::OnOpen(wxCommandEvent& event)
{
   wxFileDialog openFileDialog(this, ("Open JPEG file"), "", "", "JPEG files (*.jpg)|*jpg", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
   if(openFileDialog.ShowModal() == wxID_CANCEL)
      return;
   wxFileInputStream input_stream(openFileDialog.GetPath());
   if(!input_stream.IsOk())
   {
      wxLogError("Cannot Open File '%s'.", openFileDialog.GetPath());
      return;
   }
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
   Close(true);
}
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{    
   wxMenuBar * menuBar = new wxMenuBar;
   wxMenu * exitMenu = new wxMenu;
   wxMenu * openMenu = new wxMenu;
   exitMenu->Append(wxID_EXIT);
   openMenu->Append(wxID_OPEN);
   menuBar->Append(openMenu, "&Open");
   menuBar->Append(exitMenu, "&Exit");
   SetMenuBar(menuBar);
   CreateStatusBar(2);
   SetStatusText(wxT("Image Opener !"));
}

有人可以显示在WXWIDGETS上添加图像的示例吗?或将其添加到我的代码中。感谢您的帮助!

wxImage是图像的便携式表示形式, wxBitmap是一种平台特定但更有效的等价。一般而言,wxImage对于使用图像数据很方便,但需要将其转换为wxBitmap才能显示。

这两个类都是抽象的图像,而wxStaticBitmap是显示图像的控件,即您可以在屏幕上真正看到的东西。

image示例(在您的wxwidgets发行的样本中)显示了如何使用不同类并直接绘制图像。

wximage和wxbitmap几乎相同,它们之间的转换很容易。wxstaticbitmap用于显示WXBITMAP。它可以通过以下示例完成(staticBitMap1是您的wxstaticbitmap,btmp是您的wxbitmap):

StaticBitmap1->SetBitmap(btmp);