wxWidgets里的图片显示

l360220954 2012-05-24 01:15:19
大家好,我要做一个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);
}
...全文
770 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Foreveroriginal 2014-06-13
  • 打赏
  • 举报
回复
图片加载 wxBitmap m_bitmap(wxT("C:\\image.png"),wxBITMAP_TYPE_PNG); 直接加载就可以了,不过要指明图片的具体路径,要记得是\\,不是\
Foreveroriginal 2014-06-13
  • 打赏
  • 举报
回复
两张图片放在同一个区域 第一张图片 wxClientDC dc(this); dc.DrawBitmap(bitmap1,0,0,true); 第二张图片 放在事件处理函数里面,你要确保你的事件已经触发了。 wxClientDC dc(this); dc.Clear();//对第一张图片清空 dc.DrawBitmap(bitmap2,0,0,true);
wshcdr 2013-04-11
  • 打赏
  • 举报
回复
关注一下该问题
kese 2012-05-25
  • 打赏
  • 举报
回复

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

使用wxStaticBitmap控件来显示图像是最合适的。
l360220954 2012-05-25
  • 打赏
  • 举报
回复
没人理我,5555

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧