在 wxWidgets 中添加图像
Add Image in wxWidgets
我刚开始学习 wxWidgets,我正在尝试创建某种 Image Opener。
但是我不明白如何添加图像...
我看到了一些关于 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.it 可以通过以下示例完成(StaticBitmap1 是您的 wxStaticBitmap,btmp 是您的 wxBitmap):
StaticBitmap1->SetBitmap(btmp);
我刚开始学习 wxWidgets,我正在尝试创建某种 Image Opener。 但是我不明白如何添加图像... 我看到了一些关于 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.it 可以通过以下示例完成(StaticBitmap1 是您的 wxStaticBitmap,btmp 是您的 wxBitmap):
StaticBitmap1->SetBitmap(btmp);