读书人

wxWidgets里的图片显示解决思路

发布时间: 2013-10-21 17:02:52 作者: rapoo

wxWidgets里的图片显示
大家好,我要做一个wxWidgets的示例程序。需求是这样的:当程序开始的时候显示出一张图片,当点击那上面的按钮的时候,会显示另外一张图片。请问我该怎么做呢。帖出来的是我现在的程序,跟踪的时候发现加载图片的操作是成功的,但是图片不能显示出来,请大家给我指点啦!


#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#ifdef __WXMSW__
#include "wx/ownerdrw.h"
#endif

#include "wx/log.h"

#include "wx/sizer.h"
#include "wx/menuitem.h"
#include "wx/checklst.h"

#if !wxUSE_CHECKLISTBOX
#error "This sample can't be built without wxUSE_CHECKLISTBOX"
#endif // wxUSE_CHECKLISTBOX


// Define a new application type
class CheckListBoxApp: public wxApp
{
public:
bool OnInit();
};


// Define a new frame type
class CheckListBoxFrame : public wxFrame
{
public:
// ctor & dtor
CheckListBoxFrame(wxFrame *frame, const wxChar *title);
void OnQuit(wxCommandEvent& event);
void OnPaint(wxPaintEvent& WXUNUSED(event));
virtual ~CheckListBoxFrame(){};

private:
wxBitmap m_bmp;

DECLARE_EVENT_TABLE()
};

enum {
wxID_Quit = 10
};

BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
EVT_PAINT(CheckListBoxFrame::OnPaint)
// EVT_BUTTON( wxID_Quit, CheckListBoxFrame::OnQuit)
END_EVENT_TABLE()


//IMPLEMENT_DYNAMIC_CLASS(CheckListBoxFrame,wxImageHandler)
IMPLEMENT_APP(CheckListBoxApp)


// init our app: create windows
bool CheckListBoxApp::OnInit(void)
{
printf( "lxx**********CheckListBoxApp::OnInit(void)*******\r\n" );
CheckListBoxFrame *pFrame = new CheckListBoxFrame
(
NULL,
_T("check")
);//wxWidgets Checklistbox Sample
SetTopWindow(pFrame);
//pFrame->Show(true);

return true;
}

// main frame constructor
CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
const wxChar *title)
: wxFrame(frame, wxID_ANY, title)


{

//添加jpg和bmp文件支持
wxImage::AddHandler(new wxJPEGHandler);
wxImage::AddHandler(new wxPNGHandler);


printf( "lxx**********CheckListBoxFrame::CheckListBoxFrame()*******\r\n" );
wxPanel *panel = new wxPanel(this, wxID_ANY);

wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"),
wxPoint(20, 20));
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(CheckListBoxFrame::OnQuit));
// button->SetFocus();
Centre();

m_bmp = wxBitmap(_T("backgrnd.png"), wxBITMAP_TYPE_PNG);
if(m_bmp.Ok()) printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add
else
{
//图片加载失败
printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add
}
SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
printf("%d%d\n",m_bmp.GetWidth(),m_bmp.GetHeight());
printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add

//***************************************************
wxPaintDC dc(this);
dc.DrawBitmap(m_bmp, 0, 0,1);
printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add
//*****************************************************
//wxButton* button = new wxButton(frame,wxID_OK,wxT("OK"),wxPoint(10,10),wxDefaultSize);

Show(true);
}

void CheckListBoxFrame::OnQuit(wxCommandEvent& event)
{
printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add
Close(true);
// wxPaintDC dc(this);
// dc.DrawBitmap(m_bmp, 0, 0, true);
}


void CheckListBoxFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
{
printf("lxx add***%s(%d)of%s*****\r\n",__FUNCTION__,__LINE__,__FILE__); //lxx add
wxPaintDC dc(this);
dc.DrawBitmap(m_bmp, 0, 0, true);
}

[解决办法]

wxClientDC用来在控件上显示图像drawbitmap
wxPaintDC用来在控件的onpaint事件上显示图像

使用wxStaticBitmap控件来显示图像是最合适的。

读书人网 >C++

热点推荐