简体   繁体   English

在wxWidgets中添加图像

[英]Add Image in wxWidgets

i just started to learn wxWidgets and I'm trying to create some kind of Image Opener. 我刚刚开始学习wxWidgets,并且正在尝试创建某种类型的Image Opener。 But i cant understand how to add an image... i seen some stuff about wxImage, wxBitMap,wxStaticBitmap, but i couldn't understand any of them or the difference between them. 但是我不明白如何添加图像...我看到了一些有关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 !"));
}

can someone show an example of adding an image on wxwidgets ? 有人可以显示在wxwidgets上添加图像的示例吗? or adding it to my code . 或将其添加到我的代码中。 thanks for help ! 感谢帮助 !

wxImage is a portable representation of an image, wxBitmap is a platform-specific but more efficient equivalent. wxImage是图像的可移植表示形式, wxBitmap是特定于平台但效率更高的等效形式。 Generally speaking, wxImage is convenient for working with image data, but it needs to be converted to wxBitmap to be displayed. 一般来说, wxImage便于处理图像数据,但需要将其转换为wxBitmap才能显示。

Both of these classes are just abstract images, while wxStaticBitmap is a control showing an image, ie something you can really see on screen. 这两个类都是抽象图像,而wxStaticBitmap是显示图像的控件,即您可以在屏幕上真正看到的东西。

The image sample (under samples directory of your wxWidgets distribution) shows how to use the different classes and draw images directly. image样本(位于wxWidgets发行版的samples目录下)显示了如何使用不同的类并直接绘制图像。

wxImage and wxBitmap are almost same and conversion between them is easy. wxImage和wxBitmap几乎相同,它们之间的转换很容易。 wxStaticBitmap is used to display a wxBitmap.it can be done by following example(StaticBitmap1 is your wxStaticBitmap and btmp is your wxBitmap): wxStaticBitmap用于显示wxBitmap,可以通过以下示例完成(StaticBitmap1是您的wxStaticBitmap,而btmp是您的wxBitmap):

StaticBitmap1->SetBitmap(btmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM